2017-01-13 10 views
0

我试图从UpdateLocation活动中获取ScanLocate活动中的数组列表。为什么在将一个字符串的ArrayList从一个活动传递给另一个活动时会出现空指针异常?

我使用startActivityForResult方法调用scan方法,填充的ArrayList wifiList,然后我要到ArrayList发送到Update Location类。

我开始通过调用startActivityForResultUpdate 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 not working

How to pass an ArrayList to the StartActivityForResult activity

+0

你调试这与调试器工具? – AxelH

+0

你可以使用'readings.putStringArrayList(key,wifiList)''不需要捆绑。 – Gudin

+0

'意图读数'。不要使用'getIntent()',而是使用'readings'。 – greenapps

回答

2

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

result = readings.getStringArrayListExtra(ScanLocate.key); 
相关问题