我在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" >
答案 0 :(得分:3)
像这样改变
public class ContactManagerApplication extends Application {
public static Context context = null;
@Override
public void onCreate() {
super.onCreate();
Log.d("@gaurav", getApplicationContext()+"");
context = getApplicationContext();
}
答案 1 :(得分:1)
使用ContactManagerApplication.this
代替getApplicationContext()
。
答案 2 :(得分:0)
您不需要构建Application
类的构造函数。它是由android框架自动调用的,你必须覆盖onCreate()
类的Application
方法。
getApplicationContext()
返回应用程序的Context
,它由android框架分配,而你已经将它放在你自己的构造函数中,所以android从不调用应用程序预定义的构造函数,所以{{ 1}}当时应用程序类的对象是context
,导致null
而NullPointerException
类正在尝试访问ContextWrapper
的上下文。
答案 3 :(得分: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();
}
}
答案 4 :(得分: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();
}
但这只是在这是被调用的类时。如果这不是您正在调用的类并且在被调用的类中,那么您可以将上下文作为实例变量并从那里读取它,或者您可以将上下文作为参数发送到此类的构造函数中然后使用它。但如果是这种情况,那么如果没有直接调用,我就不会看到这个类的重点是活动的扩展。