0

我正在尝试ScanLocate从活动中获取活动中的数组列表UpdateLocation

我正在使用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();
}

最后一部分又回到UpdateLocationonActivityResult

    @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 不工作

如何将 ArrayList 传递给 StartActivityForResult 活动

4

1 回答 1

2

不需要调用getIntent(),使用方法提供的参数:

result = readings.getStringArrayListExtra(ScanLocate.key);
于 2017-01-13T13:32:29.200 回答