【问题】
android中,代码:
String keyCommType = getString(R.string.key_switch_communication_type);
出错:
The method getString(int) is undefined for the type AppConfig |
【解决过程】
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