java – Failure delivering result ResultInfo-ThrowExceptions

Exception or error:

There are many people who have encountered the same error on stackoverflow, but I haven’t been able to find any relevant resolution in those posts. My MainActivity is starting a new activity (SecondActivity) with startActivityForResult(); SecondActivity then returns some data with onBackPressed(); and putExtra();

Extract from 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) {   

         }
      }

}

Extract from 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

How to solve:

Try this-

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) 
         {   

         }
      }
}

###

The problem with me was that I used getIntent(), thus getting the intent of the current activity I was on.

When I switched to data.getStringExtra() it worked. Amateur mistake…

Leave a Reply

Your email address will not be published. Required fields are marked *