2

我在 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" >
4

5 回答 5

3

像这样改变它

public class ContactManagerApplication extends Application {
    public static Context context = null;

     @Override
    public void onCreate() {
        super.onCreate();
        Log.d("@gaurav", getApplicationContext()+"");
        context = getApplicationContext();

    }
于 2012-12-22T13:21:17.420 回答
1

使用ContactManagerApplication.this而不是getApplicationContext().

于 2012-12-22T13:22:00.727 回答
0

您不需要创建类的构造函数Application。它是由android框架自动调用的,你必须重写类onCreate()的方法Application

getApplicationContext()应用程序的返回Context,它是由android框架分配的,而你把它放在你自己的构造函数中,所以android从来没有调用应用程序预定义的构造函数,所以context应用程序类的对象是null那个时候的,这导致了NullPointerExceptionwhile ContextWrapperclass is try访问Application' 上下文。

于 2012-12-22T13:19:36.320 回答
0

您的 Application 类生命周期与 Activity 的生命周期非常相似。您不需要关心构造函数,而是覆盖已知onCreate()的对象设置:

public class ContactManagerApplication extends Application {
    public static Context context = null;

    @Override
    public void onCreate() {
        super.onCreate();

        Log.d("@gaurav", getApplicationContext()+"");
        context = getApplicationContext();
    }
}
于 2012-12-22T13:21:27.140 回答
0

通常你有更多这样的东西:

@Override
public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.your_layout);
        //After this is when you normally would call the getContext
        Context myContext = this.getApplicationContext();
}

但这仅当这是被调用的类时。如果这不是您正在调用的类并且在被调用的类中,那么您可以将上下文作为实例变量并从那里读取它,或者您可以将上下文作为参数发送到此类的构造函数中然后使用它。但如果是这种情况,那么如果不直接调用这个类,我就不认为这个类是活动的扩展。

于 2012-12-22T13:23:50.420 回答