有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

java如何调试AndroidManifest中的应用程序类问题。xml?

关于我的应用程序类,我的AndroidManifest有一个问题。我已经在清单中设置了应用程序类,但是在运行时会抛出null指针异常,因为应用程序上下文为null,例如

 Caused by: java.lang.NullPointerException: Attempt to invoke virtual method '安卓.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 安卓.app.Activity.performCreate(Activity.java:5990)
            at 安卓.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1106)
            at 安卓.app.ActivityThread.performLaunchActivity(ActivityThread.java:2278)
            at 安卓.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2387)
            at 安卓.app.ActivityThread.access$800(ActivityThread.java:151)
            at 安卓.app.ActivityThread$H.handleMessage(ActivityThread.java:1303)
            at 安卓.os.Handler.dispatchMessage(Handler.java:102)
            at 安卓.os.Looper.loop(Looper.java:135)
            at 安卓.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.安卓.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:903)
            at com.安卓.internal.os.ZygoteInit.main(ZygoteInit.java:698)

这是应用程序类

package com.example.prometheus

import 安卓.app.Application;
import 安卓.content.SharedPreferences;
import 安卓.content.pm.PackageInfo;
import 安卓.content.pm.PackageManager;
import 安卓.preference.PreferenceManager;
import 安卓.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());
    }
}

我在清单中定义了应用程序类,如下所示

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:安卓="http://schemas.安卓.com/apk/res/安卓"
    package="com.example.prometheus">

    <application
        安卓:allowBackup="true"
        安卓:icon="@mipmap/ic_launcher"
        安卓:label="@string/app_name"
        安卓:name="PrometheusApplication"
        安卓:theme="@style/AppTheme">
        <activity
            安卓:name=".MainActivity"
            安卓:label="@string/app_name" >
            <intent-filter>
                <action 安卓:name="安卓.intent.action.MAIN" />
                <category 安卓:name="安卓.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
    </application>
</manifest>

此外,Android Studio无法识别该类(警告为class or interface expected)。我已经在名称和类中添加了完整的包,放了一个“.”在全班同学面前,没有任何帮助

有人知道这个定义有什么问题吗?或者我如何调试它

短暂性脑缺血发作

注意:有一些关于堆栈溢出的问题线程,主要处理拼写问题或清单中缺少名称属性(在本例中不适用)


共 (1) 个答案

  1. # 1 楼答案

    mIstance从未在应用程序子类中初始化。您的PrometheusApplicationonCreate应该如下所示:

     public void onCreate() { 
       super.onCreate();
       mInstance = this;
     }
    

    android:name="PrometheusApplication"应该是android:name=".PrometheusApplication"