应用程序类空指针异常

时间:2014-11-22 04:35:29

标签: java android nullpointerexception

我正在创建一个Android应用,并希望在Application类中存储一个变量。该变量表示当前焦点是哪个活动。

我的目标是在onResume()中设置此变量,并在onPause()中重置该变量。这两种方法都被称为app初始化。每当我运行我的代码时,软件就会在Null Pointer Excepetion的基础上崩溃,在我尝试修改Application类中的变量的行。据我所知,在我看来应用程序类似null,即使我已经测试了代码并知道它调用了Application类' onCreate()

我是否正确假设它已连接到未完全创建的应用程序类?或者它是否与我的实例化有关?我以前是Android的新手,之前没有处理过Application类 - 据我所知,我已经正确地遵循了指南和教程。我希望这是一个简单的错误。

非常感谢任何建议!

MainActivity.java

public class MainActivity extends FragmentActivity implements ActionBar.TabListener {

    //[... other initialisations...]

    // Tells software what window is in focus.
    // Reference to Application class
    private MyApplicationClass app = (MyApplicationClass)getApplication();
    private static final int outOfFocus = 0;
    private static final int inFocus = 1;

    @Override
    protected void onResume() {
        super.onResume();
        //[... other code...]
        app.setActivityFocusIndicator(inFocus); //Crashes here
    }

    @Override
    protected void onPause() {
        super.onPause();
        //[... other code...]
        app.setActivityFocusIndicator(outOfFocus); //Crashes here
    }
}

MyApplicationClass.java

public class MyApplicationClass extends Application {

    private int activityFocusIndicator;
    /* activityFocusIndicator is here to indicate which activity is in focus.
     * 0 = Nothing in focus (written when pausing.)
     * 1 = MainActivity
     * 2 = ...
     * ...
     */

    public void setActivityFocusIndicator(int activityFocusIndicator) {
        this.activityFocusIndicator = activityFocusIndicator;
    }

    public int getActivityFocusIndicator() {
        return activityFocusIndicator;
    }

    @Override
    public void onCreate() {
        super.onCreate();
        Log.d("App", "App was created."); // This line is reached.
    }
}

清单文件

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.example.someApp"
    android:versionCode="1"
    android:versionName="1.0" >

    <uses-feature android:name="android.hardware.bluetooth_le"
                  android:required="true"/>

    <uses-sdk
        android:minSdkVersion="18"
        android:targetSdkVersion="19" />

    <uses-permission android:name="android.permission.BLUETOOTH"/>
    <uses-permission android:name="android.permission.BLUETOOTH_ADMIN"/>

    <application
        android:allowBackup="true"
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/TabTheme"
        android:name="com.example.someName.MyApplicationClass" >
        <activity
            android:name="com.example.someName.MainActivity"
            android:label="@string/app_name"
            android:screenOrientation="portrait"
            android:launchMode="singleInstance" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
        <activity
            ...
        </activity>
        <!-- ... [other activities declared]... -->
        <service
            android:name=".BluetoothService"/>
    </application>
</manifest>

如果被问到,我可以发布LogCat。

1 个答案:

答案 0 :(得分:0)

我不明白这一点:

private MyApplicationClass app = (MyApplicationClass)getApplication();

此变量可能在getApplication()返回的引用初始化之前初始化,因此保持为空。

不是使用它,而是使用局部变量将每个访问替换为app

例如:

protected void onResume() {
    super.onResume();
    //[... other code...]
    MyApplicationClass app = (MyApplicationClass)getApplication();
    app.setActivityFocusIndicator(inFocus);
}