gpt4 book ai didi

android - 应用程序子类中的 NullPointerException?

转载 作者:搜寻专家 更新时间:2023-11-01 08:06:15 25 4
gpt4 key购买 nike

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

最佳答案

改成这样

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

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

}

关于android - 应用程序子类中的 NullPointerException?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14003137/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com