简体   繁体   中英

How to debug application class issues in AndroidManifest.xml?

I have an issue with my AndroidManifest regarding my application class. 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 ). 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

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).

mIstance is never initialized in your Application subclass. You PrometheusApplication 's onCreate should look like:

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

and android:name="PrometheusApplication" should be android:name=".PrometheusApplication" .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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