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

【已解决】Android中无法通过string的id获得字符串:The method getString(int) is undefined for the type xxx

Android crifan 5221浏览 0评论

【问题】

android中,代码:

String keyCommType = getString(R.string.key_switch_communication_type);

出错:

The method getString(int) is undefined for the type AppConfig

 

The method getString(int) is undefined for the type

【解决过程】

1.搜:

The method getString(int) is undefined for the type  

参考:

java – The method getString(int) is undefined for the type Apps – Stack Overflow

去试试:

先获得context,再去获得resources,然后再去获得string:

String keyCommType = AppContext.getInstance().getResources().getString(R.string.key_switch_communication_type);

结果就可以了。

 

【总结】

如果直接使用getString无法从string的id获得string的话,那需要

先确保获得当前的context,再去在context下获得resources,然后再去从string id获得string就可以了:

String keyCommType = AppContext.getInstance().getResources().getString(R.string.key_switch_communication_type);

转载请注明:在路上 » 【已解决】Android中无法通过string的id获得字符串:The method getString(int) is undefined for the type xxx

发表我的评论
取消评论

表情

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

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