无法提供结果ResultInfo

时间:2013-12-26 09:49:30

标签: java android nullpointerexception

有很多人在stackoverflow上遇到过相同的错误,但我无法在这些帖子中找到任何相关的解决方案。我的MainActivity正在使用startActivityForResult()开始一个新活动(SecondActivity);然后SecondActivity用onBackPressed()返回一些数据;和putExtra();

从MainActivity.java中提取:

public void addNewNote(View v){

    Intent intent = new Intent(this, SecondActivity.class);
    intent.putExtra("Source", "NEW");
    startActivityForResult(intent, 1); //1 is the result code

}

protected void onActivityResult(int requestCode, int resultCode, Intent data) {

    Log.v("TAG", data.getStringExtra("Note"));
      if (requestCode == 1) {

         if(resultCode == RESULT_OK){        
             listItems.add(data.getStringExtra("Note"));
             Log.v("TAG", data.getStringExtra("Note"));
             adapter.notifyDataSetChanged();
             listView.invalidateViews();
         }
         if (resultCode == RESULT_CANCELED) {   

         }
      }

}

从SecondActivity.java中提取:

@Override
public void onBackPressed() {

    super.onBackPressed();
    if (mainTextField.getText() != null){
        Intent returnIntent = new Intent();
        returnIntent.putExtra("Note",mainTextField.getText());
        setResult(RESULT_OK, returnIntent);     
        finish();
    } else {
        Intent returnIntent = new Intent();
        setResult(RESULT_CANCELED, returnIntent);        
        finish();
    }

}

logcat的:

Logcat

2 个答案:

答案 0 :(得分:17)

试试这个 -

protected void onActivityResult(int requestCode, int resultCode, Intent data) 
{
      if (requestCode == 1 && data != null) 
      {
         Log.v("TAG", data.getStringExtra("Note"));
         if(resultCode == RESULT_OK)
         {        
             listItems.add(data.getStringExtra("Note"));
             Log.v("TAG", data.getStringExtra("Note"));
             adapter.notifyDataSetChanged();
             listView.invalidateViews();
         }
         if (resultCode == RESULT_CANCELED) 
         {   

         }
      }
}

答案 1 :(得分:1)

我的问题是我使用了 getIntent(),从而获得了我当前活动的意图。

当我切换到 data.getStringExtra()时,它工作正常。业余错误......