我试图从UpdateLocation
活动中获取ScanLocate
活动中的数组列表。为什么在将一个字符串的ArrayList从一个活动传递给另一个活动时会出现空指针异常?
我使用startActivityForResult
方法调用scan
方法,填充的ArrayList wifiList
,然后我要到ArrayList发送到Update Location
类。
我开始通过调用startActivityForResult
在Update Location
:
private void getScan(){
//Create an intent to start ScanLocate
final Intent i = new Intent(this, ScanLocate.class);
//Start scanLocate with request code
startActivityForResult(i, REQUEST_READINGS);
}
接下来,在ScanLocate
我创建了sendData
方法(注意:检查确认ArrayList的数据是完整的,在这一点上):
private void sendData(){
//create a new intent as container for the result
final Intent readings = new Intent(this, UpdateLocation.class);
//check that data is in wifiList
for(String s:wifiList){
Log.v(TAG,"List Items: " + s);
}
//create bundle for string array
Bundle b = new Bundle();
b.putStringArrayList(key, wifiList);
//add readings to send to updateLoc
readings.putExtras(b);
//set code to indicate success and attach Intent
setResult(RESULT_OK, readings);
//call finish to return
finish();
}
最后一部分回到UpdateLocation
与onActivityResult
:
@Override
public void onActivityResult(int requestCode, int resultCode, Intent readings){
super.onActivityResult(requestCode, resultCode, readings);
//check if request code matches the one set above
if(requestCode == REQUEST_READINGS){
//check if sendData() in ScanLocate was successful
if(resultCode == RESULT_OK){
//get the readings from the Intent
Log.v(TAG, "HERE");
result = getIntent().getExtras().getStringArrayList(ScanLocate.key);
Log.v(TAG, "HERE2");
for(String s : result) {
Log.v(TAG, "Location 5: " + s);
}
}else{
//ScanLocate was unsuccessful
}
}
}
然后显示第一个"Here"
然后下一行,getStringArrayList()
抛出空指针异常。
我已经浏览了文档,并在此处查看了以前的问题,但无法看到发生了什么问题。
任何意见,将不胜感激,谢谢。
前面的问题:
startactivityforResult not working
How to pass an ArrayList to the StartActivityForResult activity
你调试这与调试器工具? – AxelH
你可以使用'readings.putStringArrayList(key,wifiList)''不需要捆绑。 – Gudin
'意图读数'。不要使用'getIntent()',而是使用'readings'。 – greenapps