2015-10-27 30 views
0

我試圖從在先前的視圖設置先前分類訪問整數的ArrayList但它似乎並沒有被正確保存的ArrayList,當我去到新的觀點,我認爲它會刪除任何在那裏?在其他視圖中無法訪問ArrayList? Android的空指針異常

無論如何訪問已設置的「lightInfo」?

ConnectTest聲明

public class ConnectTest extends MainActivity { 
    TextView out; 
    private static final int REQUEST_ENABLE_BT = 1; 
    private BluetoothAdapter btAdapter = null; 
    private BluetoothSocket btSocket = null; 
    private OutputStream outStream = null; 

    // Well known SPP UUID 
    private static final UUID MY_UUID = 
      UUID.fromString("00001101-0000-1000-8000-00805F9B34FB"); 

    // Insert your server's MAC address 
    private static String address = "A4:5E:60:EB:3C:E8"; 
    ArrayList<Integer> temp = ((MainActivity) getApplicationContext()).lightInfo; 

呼叫來臨時創建的錯誤:

String finalMessage = ""; 
     for (int i = 0; i < temp.size(); i++) { 
      finalMessage = finalMessage + temp.get(i) + "\n"; 
     } 

     String message = finalMessage; 

MainActivity聲明

public class MainActivity extends AppCompatActivity { 


    private final static int REQUEST_ENABLE_BT = 1; 
    UUID myUUID = UUID.randomUUID(); 
    private static final int DISCOVER_DURATION = 300; 
    private static final int REQUEST_BLU = 1; 
    ArrayAdapter<String> mArrayAdapter; 
    ArrayList<Double> currentBodyTemp; 
    BluetoothAdapter mBluetoothAdapter = BluetoothAdapter.getDefaultAdapter(); 
    boolean targetTimeZoneIsAhead = false; 
    boolean targetTimeZoneIsBehind = false; 

    int currentWakeTime = 420; 
    int currentSleepTime = 1320; 

    int targetWakeTime = 0; 
    int targetSleepTime = 0; 
    int daysLeft = 0; 
    int timeDifference = 0; 

    public ArrayList<Integer> lightInfo; 

錯誤消息:

10-27 12:59:56.141 15803-15803/com.teamfara.circadianrhythmmonitor4 D/AndroidRuntime: Shutting down VM 
10-27 12:59:56.151 15803-15803/com.teamfara.circadianrhythmmonitor4 E/AndroidRuntime: FATAL EXCEPTION: main 
10-27 12:59:56.151 15803-15803/com.teamfara.circadianrhythmmonitor4 E/AndroidRuntime: Process: com.teamfara.circadianrhythmmonitor4, PID: 15803 
10-27 12:59:56.151 15803-15803/com.teamfara.circadianrhythmmonitor4 E/AndroidRuntime: java.lang.RuntimeException: Unable to instantiate activity ComponentInfo{com.teamfara.circadianrhythmmonitor4/com.teamfara.circadianrhythmmonitor4.ConnectTest}: java.lang.NullPointerException: Attempt to invoke virtual method 'android.content.Context android.content.Context.getApplicationContext()' on a null object reference 
10-27 12:59:56.151 15803-15803/com.teamfara.circadianrhythmmonitor4 E/AndroidRuntime:  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2964) 
10-27 12:59:56.151 15803-15803/com.teamfara.circadianrhythmmonitor4 E/AndroidRuntime:  at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3218) 
10-27 12:59:56.151 15803-15803/com.teamfara.circadianrhythmmonitor4 E/AndroidRuntime:  at android.app.ActivityThread.access$1000(ActivityThread.java:198) 
10-27 12:59:56.151 15803-15803/com.teamfara.circadianrhythmmonitor4 E/AndroidRuntime:  at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1676) 
10-27 12:59:56.151 15803-15803/com.teamfara.circadianrhythmmonitor4 E/AndroidRuntime:  at android.os.Handler.dispatchMessage(Handler.java:102) 
10-27 12:59:56.151 15803-15803/com.teamfara.circadianrhythmmonitor4 E/AndroidRuntime:  at android.os.Looper.loop(Looper.java:145) 
10-27 12:59:56.151 15803-15803/com.teamfara.circadianrhythmmonitor4 E/AndroidRuntime:  at android.app.ActivityThread.main(ActivityThread.java:6837) 
10-27 12:59:56.151 15803-15803/com.teamfara.circadianrhythmmonitor4 E/AndroidRuntime:  at java.lang.reflect.Method.invoke(Native Method) 
10-27 12:59:56.151 15803-15803/com.teamfara.circadianrhythmmonitor4 E/AndroidRuntime:  at java.lang.reflect.Method.invoke(Method.java:372) 
10-27 12:59:56.151 15803-15803/com.teamfara.circadianrhythmmonitor4 E/AndroidRuntime:  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1404) 
10-27 12:59:56.151 15803-15803/com.teamfara.circadianrhythmmonitor4 E/AndroidRuntime:  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1199) 
10-27 12:59:56.151 15803-15803/com.teamfara.circadianrhythmmonitor4 E/AndroidRuntime: Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'android.content.Context android.content.Context.getApplicationContext()' on a null object reference 
10-27 12:59:56.151 15803-15803/com.teamfara.circadianrhythmmonitor4 E/AndroidRuntime:  at android.content.ContextWrapper.getApplicationContext(ContextWrapper.java:111) 
10-27 12:59:56.151 15803-15803/com.teamfara.circadianrhythmmonitor4 E/AndroidRuntime:  at com.teamfara.circadianrhythmmonitor4.ConnectTest.<init>(ConnectTest.java:34) 
10-27 12:59:56.151 15803-15803/com.teamfara.circadianrhythmmonitor4 E/AndroidRuntime:  at java.lang.reflect.Constructor.newInstance(Native Method) 
10-27 12:59:56.151 15803-15803/com.teamfara.circadianrhythmmonitor4 E/AndroidRuntime:  at java.lang.Class.newInstance(Class.java:1684) 
10-27 12:59:56.151 15803-15803/com.teamfara.circadianrhythmmonitor4 E/AndroidRuntime:  at android.app.Instrumentation.newActivity(Instrumentation.java:1080) 
10-27 12:59:56.151 15803-15803/com.teamfara.circadianrhythmmonitor4 E/AndroidRuntime:  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2954) 
10-27 12:59:56.151 15803-15803/com.teamfara.circadianrhythmmonitor4 E/AndroidRuntime:  at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3218)  
10-27 12:59:56.151 15803-15803/com.teamfara.circadianrhythmmonitor4 E/AndroidRuntime:  at android.app.ActivityThread.access$1000(ActivityThread.java:198)  
10-27 12:59:56.151 15803-15803/com.teamfara.circadianrhythmmonitor4 E/AndroidRuntime:  at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1676)  
10-27 12:59:56.151 15803-15803/com.teamfara.circadianrhythmmonitor4 E/AndroidRuntime:  at android.os.Handler.dispatchMessage(Handler.java:102)  
10-27 12:59:56.151 15803-15803/com.teamfara.circadianrhythmmonitor4 E/AndroidRuntime:  at android.os.Looper.loop(Looper.java:145)  
10-27 12:59:56.151 15803-15803/com.teamfara.circadianrhythmmonitor4 E/AndroidRuntime:  at android.app.ActivityThread.main(ActivityThread.java:6837)  
10-27 12:59:56.151 15803-15803/com.teamfara.circadianrhythmmonitor4 E/AndroidRuntime:  at java.lang.reflect.Method.invoke(Native Method)  
10-27 12:59:56.151 15803-15803/com.teamfara.circadianrhythmmonitor4 E/AndroidRuntime:  at java.lang.reflect.Method.invoke(Method.java:372)  
10-27 12:59:56.151 15803-15803/com.teamfara.circadianrhythmmonitor4 E/AndroidRuntime:  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1404)  
10-27 12:59:56.151 15803-15803/com.teamfara.circadianrhythmmonitor4 E/AndroidRuntime:  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1199)  

謝謝!

回答

0

您的應用程序崩潰,因爲你是在試圖訪問getApplicationContext在您的活動,而在全球範圍,這後者沒有經歷其生命週期。由於您的ConnectTest擴展了您的MainActivity,因此您沒有任何角色可以訪問任何非私人MainActivity的成員。您只需標記爲publicprotected。即使默認訪問說明符是好的,如果兩個類在同一個包中。看一下here