简体   繁体   English

如何在AndroidManifest.xml中调试应用程序类问题?

[英]How to debug application class issues in AndroidManifest.xml?

I have an issue with my AndroidManifest regarding my application class. 我的AndroidManifest遇到我的应用程序类问题。 I've set the application class in the manifest, but during runtime null pointer exceptions are thrown since the application context is null, eg 我已经在清单中设置了应用程序类,但是在运行时会抛出空指针异常,因为应用程序上下文为空,例如

 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)

That's the application class 那是应用程序类

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());
    }
}

I've defined the application class in the manifest as follows 我在清单中定义了应用程序类,如下所示

<?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>

In addition, Android Studio does not recognise the class (the warning is class or interface expected ). 此外,Android Studio无法识别该类(警告为class or interface expected )。 I've added the full package to the name and class, put a '.' 我已经将完整的程序包添加到名称和类中,并添加了“。”。 in front of the class and nothing helps. 在全班同学面前,没有任何帮助。

Does anybody know what's wrong with the definition or how I could debug this? 有人知道定义有什么问题吗,或者我该如何调试呢?

TIA TIA

PS: There are a few question threads about this on stack overflow, mostly dealing with spelling issues or missing name attributes in the manifest (which don't apply in this case). PS:在堆栈溢出时有一些与此相关的问题线程,主要是处理清单中的拼写问题或名称属性丢失(在这种情况下不适用)。

mIstance is never initialized in your Application subclass. mIstance从未在您的Application子类中初始化。 You PrometheusApplication 's onCreate should look like: PrometheusApplicationonCreate应该如下所示:

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

and android:name="PrometheusApplication" should be android:name=".PrometheusApplication" . android:name="PrometheusApplication"应该是android:name=".PrometheusApplication"

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM