最新消息:20210816 当前crifan.com域名已被污染,为防止失联,请关注(页面右下角的)公众号

【已解决】Android中使用getResources去尝试解析xml时出错:The method getResources() is undefined for the type

Android crifan 4971浏览 0评论

【问题】

折腾:

【未解决】Android中解析/res/raw文件夹下面的xml文件

期间,使用代码:

InputStream is = getResources().openRawResource(R.raw.myxml);

去解析xml文件,结果代码提示语法错误:

The method getResources() is undefined for the type xxx

如图:

The method getResources is undefined for the type

【解决过程】

1.感觉是当前的类中,无法使用到对应的getResources。

2.不过后来发现之前自己在:

【记录】android中获得res/xml下面的关于USB过滤配置的资源文件信息

中就是可以正常的通过getResources去获得raw下面的xml的。

3.参考:

Android getResource() undefined error – Stack Overflow

知道了,getResources是context的函数。

所以需要先找到对应的context,然后才能调用getResources。

4.此处,之前是已有函数可以获得当前的context的,所以加上后,即可:

InputStream is = AppContext.getInstance().getResources().openRawResource(R.raw.myxml);

即可正常使用getResources()了:

after got context then can use getResources

 

【总结】

getResources是必须处在当前的context才可以调用的。

如果不是,则需要先获得当前的context后,才可以继续调用getResources。

转载请注明:在路上 » 【已解决】Android中使用getResources去尝试解析xml时出错:The method getResources() is undefined for the type

发表我的评论
取消评论

表情

Hi,您需要填写昵称和邮箱!

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址
83 queries in 0.163 seconds, using 22.12MB memory