我正在尝试引用一些我存储在xml中的字符串。我收到了这个错误:
Caused by: java.lang.NullPointerException: Attempt to invoke virtual method
'android.content.res.Resourcesandroid.content.Context.getResources()'
on a null object reference at
android.content.ContextWrapper.getResources(ContextWrapper.java:86)
在我使用getString
来引用xml变量的行上。
我看到错误的行:
private String CLIENT_ID = getString(R.string.MY_CLIENT_ID);
完整错误:
com.android.example.helloworld E/AndroidRuntime﹕ FATAL EXCEPTION: main
Process: com.android.example.helloworld, PID: 8408
java.lang.RuntimeException: Unable to instantiate activity ComponentInfo{com.android.example.helloworld/com.android.example.helloworld.MainActivity}: java.lang.NullPointerException: Attempt to invoke virtual method 'android.content.res.Resources android.content.Context.getResources()' on a null object reference
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2236)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2387)
at android.app.ActivityThread.access$800(ActivityThread.java:151)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1303)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:135)
at android.app.ActivityThread.main(ActivityThread.java:5254)
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(Method.java:372)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:903)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:698)
Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'android.content.res.Resources android.content.Context.getResources()' on a null object reference
at android.content.ContextWrapper.getResources(ContextWrapper.java:86)
at android.view.ContextThemeWrapper.getResources(ContextThemeWrapper.java:74)
at android.content.Context.getString(Context.java:377)
at com.android.example.helloworld.MainActivity.<init>(MainActivity.java:55)
at java.lang.reflect.Constructor.newInstance(Native Method)
at java.lang.Class.newInstance(Class.java:1606)
at android.app.Instrumentation.newActivity(Instrumentation.java:1066)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2226)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2387)
at android.app.ActivityThread.access$800(ActivityThread.java:151)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1303)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:135)
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(Method.java:372)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:903)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:698)
我的/res/values/variables.xml文件:
<string name="MY_CLIENT_ID">foo1</string>
<string name="MY_CLIENT_SECRET">bar1</string>
我的AndroidManifest.xml文件
<uses-permission android:name="android.permission.INTERNET" />
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="Hello!"
android:theme="@style/AppTheme" >
<meta-data
android:name="CLIENT_ID"
android:value="@string/MY_CLIENT_ID" />
<meta-data
android:name="CLIENT_SECRET"
android:value="@string/MY_CLIENT_SECRET" />
<activity
android:name="com.android.example.helloworld.MainActivity"
android:label="Hello!" >
<intent-filter android:label="@string/app_name" >
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
我的MainActivity.java文件的部分代码:
public class MainActivity extends ActionBarActivity implements myListener {
/* One of the very first lines that are executed throw the nullpointerexception */
private String CLIENT_ID = getString(R.string.MY_CLIENT_ID);
private String CLIENT_SECRET = getString(R.string.MY_CLIENT_SECRET);
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
temp = (TextView) findViewById(R.id.temp);
obj = new foo(CLIENT_ID, CLIENT_SECRET, this);
obj.bar()
}
}
这是我到目前为止所做的。
1)我使用http://developer.android.com/guide/topics/resources/string-resource.html作为参考。
2)如果我在开头硬编码值并运行应用程序就可以了。当我尝试从xml引用值时,我只得到所述错误。
3)我在google和stackexchange上搜索了很多关于这个主题的内容,并尝试了在stackexchange链接上建议的几种方法,例如:
创建并调用如下方法。我在这个方法中得到了同样的错误。
public String getStringResourceByName(String resourceName) {
String packageName = "com.android.example.helloworld";
Log.i("-- package name --",packageName);
int resId = this.getResources().getIdentifier(resourceName, "string", packageName);
String resName = getString(resId);
Log.i("-- resource name -- ",resName);
return resName;
}
在上述方法中特别使用MainActivity.this
public String getStringResourceByName(String resourceName) {
String packageName = "com.android.example.helloworld";
Log.i("-- package name --",packageName);
int resId = MainActivity.this.getResources().getIdentifier(resourceName, "string", packageName);
String resName = getString(resId);
Log.i("-- resource name -- ",resName);
return resName;
}
所有这些更改都会给我带来同样的错误。
有人可以指导我在这里缺少什么吗?我希望我提供了足够的数据。
答案 0 :(得分:3)
private String CLIENT_ID = getString(R.string.MY_CLIENT_ID);
如果在类初始化时初始化字段,则在getString()
之前调用onCreate()
,因此Context尚未初始化。更好地使用它
public void onCreate(final Bundle bundle) {
// Other code
CLIENT_ID = getString(R.string.MY_CLIENT_ID);
}
此外,我强烈建议不要缓存字符串,因为如果语言发生了变化,你可能会得到错误的翻译(这个问题已经发生在我身上,所以我从经验中说出来)。而是在需要时使用getString()
。
答案 1 :(得分:0)
您需要Context
才能访问您的应用资源。您首先尝试获取资源而没有有效的上下文。将您的来电转移到onCreate()
方法,首先创建Activity
并获得上下文:
private String CLIENT_ID;
private String CLIENT_SECRET;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
temp = (TextView) findViewById(R.id.temp);
CLIENT_ID = getString(R.string.MY_CLIENT_ID);
CLIENT_SECRET = getString(R.string.MY_CLIENT_SECRET);
obj = new foo(CLIENT_ID, CLIENT_SECRET, this);
obj.bar()
}