为什么在应用程序的构造函数中调用 android.content.Context.getApplicationContext() 时会出现 NPE?

Why do I get NPE when I invoke android.content.Context.getApplicationContext() within Application's constructor?

我的申请 class 看起来与此类似,

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.access00(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 代码中 class 看起来像这样,

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.agethis.name 时不会导致 NPE。上面的应用程序片段与下面的有什么不同?

你能在 onCreate() 方法中执行此操作吗,它应该可以工作,因为你在构造函数中执行此操作可能是问题所在。

How do the above application snippet is something different than the below?

Student 没有超类(默认的 Object 除外)。您正在引用自己在 Student.

上定义的字段

Application 有超类,如您在 the documentation 中所见。那么,让我们为您的 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.lengthsomethingnull。在调用 onCreate() 之前,超类不会将 something 设置为一个值。您引用 something 太早了。

同样,在适当的时间之前,您不能调用 Context 公开的方法。对于 ApplicationActivityService,这是在您的 onCreate() 调用 super.onCreate().

之后