为什么将字符串的 ArrayList 从一个 activity 传递到另一个时出现空指针异常?
Why am I getting a null pointer exception when passing an ArrayList of strings from one activity to another?
我正在尝试从 UpdateLocation
activity.
中获取 ScanLocate
activity 中的数组列表
我正在使用 startActivityForResult
方法调用填充 ArrayList wifiList
的 scan
方法,然后我想将 ArrayList 发送到 Update Location
class。
我首先在 Update Location
中调用 startActivityForResult
:
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
不需要调用getIntent()
,使用方法提供的参数:
result = readings.getStringArrayListExtra(ScanLocate.key);
我正在尝试从 UpdateLocation
activity.
ScanLocate
activity 中的数组列表
我正在使用 startActivityForResult
方法调用填充 ArrayList wifiList
的 scan
方法,然后我想将 ArrayList 发送到 Update Location
class。
我首先在 Update Location
中调用 startActivityForResult
:
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
不需要调用getIntent()
,使用方法提供的参数:
result = readings.getStringArrayListExtra(ScanLocate.key);