我正在尝试从上一个视图中设置的前一个类访问整数ArrayList,但是当我转到新视图时它似乎没有正确保存arrayList我认为它删除了那里的任何东西?
是否有访问已经设置的“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;
调用创建错误的temp:
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 :(得分:0)
您不能简单地对任何对象进行类型转换并期望它能够正常工作。请使用正确的数据传递机制。试试这个:
答案 1 :(得分:0)
您的应用程序崩溃了,因为您正在尝试访问活动全局范围内的getApplicationContext
,而后者未经历其生命周期。由于您的ConnectTest
扩展了MainActivity
,因此您无需任何演员即可访问MainActivity
的任何 非私有 成员。您只需标记为public
或protected
。如果两个类在同一个包中,那么即使是默认访问说明符也是如此。看看here