我的应用程序类看起来与此类似,
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。上面的应用程序 fragment 与下面的有何不同?
最佳答案
How do the above application snippet is something different than the below?
Student
没有父类(super class)(默认情况下 Object
除外)。您正在引用您自己在 Student
上定义的字段。
Application
具有父类(super class),如 the documentation 中所示。 。因此,让我们为您的 Student
添加一个父类(super class):
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()
之前,父类(super class)不会将 something
设置为值。您太早引用了某些内容
。
同样,在适当的时间之前,您无法调用 Context
公开的方法。对于 Application
、Activity
和 Service
而言,即在 onCreate()
调用 之后super.onCreate()
.
关于java - 当我在 Application 的构造函数中调用 android.content.Context.getApplicationContext() 时,为什么会出现 NPE?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59346227/