我在 Application 子类中收到 nullpointerexception。我想要一个在整个应用程序中都可以使用的上下文,所以我调用了 getApplicationContext(),但这会导致异常 下面是 logcat 消息:
12-22 17:15:35.639: E/AndroidRuntime(572): FATAL EXCEPTION: main
12-22 17:15:35.639: E/AndroidRuntime(572): java.lang.RuntimeException: Unable to instantiate application com.gaurav.contactmanager.model.ContactManagerApplication: java.lang.NullPointerException
12-22 17:15:35.639: E/AndroidRuntime(572): at android.app.LoadedApk.makeApplication(LoadedApk.java:466)
12-22 17:15:35.639: E/AndroidRuntime(572): at android.app.ActivityThread.handleBindApplication(ActivityThread.java:3260)
12-22 17:15:35.639: E/AndroidRuntime(572): at android.app.ActivityThread.access$2200(ActivityThread.java:117)
12-22 17:15:35.639: E/AndroidRuntime(572): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:969)
12-22 17:15:35.639: E/AndroidRuntime(572): at android.os.Handler.dispatchMessage(Handler.java:99)
12-22 17:15:35.639: E/AndroidRuntime(572): at android.os.Looper.loop(Looper.java:123)
12-22 17:15:35.639: E/AndroidRuntime(572): at android.app.ActivityThread.main(ActivityThread.java:3683)
12-22 17:15:35.639: E/AndroidRuntime(572): at java.lang.reflect.Method.invokeNative(Native Method)
12-22 17:15:35.639: E/AndroidRuntime(572): at java.lang.reflect.Method.invoke(Method.java:507)
12-22 17:15:35.639: E/AndroidRuntime(572): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)
12-22 17:15:35.639: E/AndroidRuntime(572): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
12-22 17:15:35.639: E/AndroidRuntime(572): at dalvik.system.NativeStart.main(Native Method)
12-22 17:15:35.639: E/AndroidRuntime(572): Caused by: java.lang.NullPointerException
12-22 17:15:35.639: E/AndroidRuntime(572): at android.content.ContextWrapper.getApplicationContext(ContextWrapper.java:100)
12-22 17:15:35.639: E/AndroidRuntime(572): at com.gaurav.contactmanager.model.ContactManagerApplication.<init>(ContactManagerApplication.java:10)
12-22 17:15:35.639: E/AndroidRuntime(572): at java.lang.Class.newInstanceImpl(Native Method)
12-22 17:15:35.639: E/AndroidRuntime(572): at java.lang.Class.newInstance(Class.java:1409)
12-22 17:15:35.639: E/AndroidRuntime(572): at android.app.Instrumentation.newApplication(Instrumentation.java:957)
12-22 17:15:35.639: E/AndroidRuntime(572): at android.app.Instrumentation.newApplication(Instrumentation.java:942)
12-22 17:15:35.639: E/AndroidRuntime(572): at android.app.LoadedApk.makeApplication(LoadedApk.java:461)
12-22 17:15:35.639: E/AndroidRuntime(572): ... 11 more
下面是源代码:
package com.gaurav.contactmanager.model;
import android.app.Application;
import android.content.Context;
import android.util.Log;
public class ContactManagerApplication extends Application {
public static Context context = null;
public ContactManagerApplication() {
Log.d("@gaurav", getApplicationContext()+"");
context = getApplicationContext();
}
}
和 android-manifest 声明如下:
<application
android:name=".model.ContactManagerApplication"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >