2014-11-22 106 views
0

我正在創建一個Android應用程序,並希望在Application類中存儲一個變量。該變量表示哪個活動當前處於焦點狀態。應用程序類空指針異常

我的目標是在onResume()中設置這個變量並將其重置爲onPause()。這兩種方法都被稱爲應用程序初始化。每當我運行我的代碼時,該軟件就會在空指針溢出的基礎上崩潰,並嘗試修改Application類中的變量。據我所知,即使我已經測試了代碼並知道它稱爲應用程序類'onCreate(),但在我看來,應用程序類似於null

我正確地認爲它連接到應用程序類未完全創建?或者它與我的所有實例有關?我對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的。

回答

0

我沒有看到這條線的點:

private MyApplicationClass app = (MyApplicationClass)getApplication(); 

該變量初始化可能通過getApplication()返回的引用初始化之前,因此保持爲空。

而不是使用它,用局部變量替換每個訪問app

例如:

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

合理的邏輯,這正是問題是,謝謝!這是我將來在使用該課程時需要注意的事項。 – clb9355 2014-11-22 04:47:10