2015-07-28 43 views
0

我的AndroidManifest有关于我的应用程序类的问题。我已经在清单中设置了应用程序类,但是在运行期间,由于应用程序上下文为空,所以会抛出空指针异常。如何在AndroidManifest.xml中调试应用程序类问题?

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

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

<?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不承认阶级(警告是class or interface expected)。我已经将完整的软件包添加到了名称和类中,并添加了'。'在班级面前,没有什么帮助。

有没有人知道定义有什么问题,或者我该如何调试?

TIA

P.S:有对堆栈溢出有关这几个问题线程,多处理拼写问题或丢失的名字在清单(不适用于这种情况)的属性。

回答

4

mIstance永远不会在您的应用程序子类中初始化。你PrometheusApplicationonCreate应该是这样的:

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

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