我的 AndroidManifest 存在关于我的应用程序类的问题。我已在 list 中设置了应用程序类,但在运行时,由于应用程序上下文为空,因此会引发空指针异常,例如
Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'android.content.Context com.example.prometheus.PrometheusApplication.getApplicationContext()' on a null object reference
at com.example.prometheus.tasks.NativeTasks.getPath(NativeTasks.java:119)
at com.example.prometheus.tasks.NativeTasks.chmod(NativeTasks.java:71)
at com.example.prometheus.tasks.FileTask.install(FileTask.java:55)
at com.example.prometheus.MainActivity.onCreate(MainActivity.java:29)
at android.app.Activity.performCreate(Activity.java:5990)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1106)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2278)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2387)
at android.app.ActivityThread.access$800(ActivityThread.java:151)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1303)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:135)
at android.app.ActivityThread.main(ActivityThread.java:5254)
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(Method.java:372)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:903)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:698)
这就是应用程序类
package com.example.prometheus
import android.app.Application;
import android.content.SharedPreferences;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager;
import android.preference.PreferenceManager;
import android.util.Log;
public class PrometheusApplication extends Application {
private static PrometheusApplication mInstance;
public static PrometheusApplication getInstance(){
return mInstance;
}
public void onCreate() { super.onCreate(); }
public SharedPreferences getApplicationPreferences() {
return PreferenceManager.getDefaultSharedPreferences(getInstance());
}
}
我在 list 中定义了应用程序类,如下
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.prometheus">
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:name="PrometheusApplication"
android:theme="@style/AppTheme">
<activity
android:name=".MainActivity"
android:label="@string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
此外,Android Studio 无法识别该类(警告是需要类或接口(interface)
)。我已将完整的包添加到名称和类中,并添加“.”在全类同学面前却毫无帮助。
有人知道定义有什么问题或者我如何调试它吗?
TIA
P.S.:在堆栈溢出上有一些关于此问题的问题线程,主要处理拼写问题或 list 中缺少名称属性(在本例中不适用)。
最佳答案
mIstance
永远不会在您的应用程序子类中初始化。 PrometheusApplication
的 onCreate
应该如下所示:
public void onCreate() {
super.onCreate();
mInstance = this;
}
和android:name="PrometheusApplication"
应该是android:name=".PrometheusApplication"
。
关于java - 如何调试AndroidManifest.xml中的应用程序类问题?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31670539/