我的应用程序类看起来与此类似,
public class MyApplication extends Application {
private static final String TAG = MyApplication.class.getSimpleName();
public static Context mContext;
public MyApplication() {
Log.i(TAG, "Inside my application.. " + this);
Log.i(TAG, "Value of application context is : " + this.getApplicationContext());
mContext = this;
}
}
我得到 NPE,当我做上面这样的事情时,我的应用程序崩溃了,
12-15 12:03:57.886 8174 8174 D AndroidRuntime: Shutting down VM
12-15 12:03:57.890 8174 8174 E AndroidRuntime: FATAL EXCEPTION: main
12-15 12:03:57.890 8174 8174 E AndroidRuntime: Process: com.example.gm, PID: 8174
12-15 12:03:57.890 8174 8174 E AndroidRuntime: java.lang.RuntimeException: Unable to instantiate application com.example.gm.MyApplication: java.lang.NullPointerException: Attempt to invoke virtual method 'android.content.Context android.content.Context.getApplicationContext()' on a null object reference
12-15 12:03:57.890 8174 8174 E AndroidRuntime: at android.app.LoadedApk.makeApplication(LoadedApk.java:1087)
12-15 12:03:57.890 8174 8174 E AndroidRuntime: at android.app.ActivityThread.handleBindApplication(ActivityThread.java:5905)
12-15 12:03:57.890 8174 8174 E AndroidRuntime: at android.app.ActivityThread.access$1100(ActivityThread.java:207)
12-15 12:03:57.890 8174 8174 E AndroidRuntime: at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1660)
12-15 12:03:57.890 8174 8174 E AndroidRuntime: at android.os.Handler.dispatchMessage(Handler.java:106)
12-15 12:03:57.890 8174 8174 E AndroidRuntime: at android.os.Looper.loop(Looper.java:193)
12-15 12:03:57.890 8174 8174 E AndroidRuntime: at android.app.ActivityThread.main(ActivityThread.java:6734)
12-15 12:03:57.890 8174 8174 E AndroidRuntime: at java.lang.reflect.Method.invoke(Native Method)
12-15 12:03:57.890 8174 8174 E AndroidRuntime: at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:493)
12-15 12:03:57.890 8174 8174 E AndroidRuntime: at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:858)
12-15 12:03:57.890 8174 8174 E AndroidRuntime: Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'android.content.Context android.content.Context.getApplicationContext()' on a null object reference
12-15 12:03:57.890 8174 8174 E AndroidRuntime: at android.content.ContextWrapper.getApplicationContext(ContextWrapper.java:116)
12-15 12:03:57.890 8174 8174 E AndroidRuntime: at com.example.gm.MyApplication.<init>(MyApplication.java:12)
12-15 12:03:57.890 8174 8174 E AndroidRuntime: at java.lang.Class.newInstance(Native Method)
12-15 12:03:57.890 8174 8174 E AndroidRuntime: at android.app.AppComponentFactory.instantiateApplication(AppComponentFactory.java:50)
12-15 12:03:57.890 8174 8174 E AndroidRuntime: at androidx.core.app.CoreComponentFactory.instantiateApplication(CoreComponentFactory.java:49)
12-15 12:03:57.890 8174 8174 E AndroidRuntime: at android.app.Instrumentation.newApplication(Instrumentation.java:1120)
12-15 12:03:57.890 8174 8174 E AndroidRuntime: at android.app.LoadedApk.makeApplication(LoadedApk.java:1079)
12-15 12:03:57.890 8174 8174 E AndroidRuntime: ... 9 more
而在看起来像这样的类的纯 java 代码中,
public class Student {
int age;
String name;
String badgeid;
public Student(int age, String name) {
this.age = age;
this.name = name;
this.badgeid = this.age + this.name;
}
}
当我做this.age
和this.name
时不会导致 NPE.上面的应用程序代码段与下面的应用程序片段有何不同?
上面的应用程序片段与下面的应用程序片段有何不同?
Student
没有超类(除了Object
,默认值(。您正在引用您在Student
上自己定义的字段。
Application
具有超类,如您在文档中看到的那样。因此,让我们为您的Student
添加一个超类:
public class Base {
String something;
public void onCreate() {
something = "a value";
}
}
public class Student extends Base {
int age;
String name;
String badgeid;
int value;
public Student(int age, String name) {
this.age = age;
this.name = name;
this.badgeid = this.age + this.name;
this.value = something.length;
}
}
在这里,您将崩溃并显示NullPointerException
,因为在您尝试调用something.length
时something
null
。在调用onCreate()
之前,超类不会将something
设置为值。您引用something
太早了。
同样,在适当的时间之前,不能调用Context
公开的方法。在Application
、Activity
和Service
的情况下,那是在你的onCreate()
调用super.onCreate()
之后。
你能在onCreate((方法中做到这一点吗,它应该可以工作,因为你在构造函数中这样做可能是由于这个问题。