Search code examples
androidandroid-intenthashmaponactivityresult

Failure Delivering Results with HashMap between 2 activities


Background:

  • There are two activities
  • 1x (MainActivity), 1x (SubActivity - Activity with a ListView)
  • Main Activity extends FragmentActivity and the Infos will be used in a Fragment
  • SubActivity will open when a MenuItem of MainActivity will be clicked.
  • Using a HashMap for the ListView

My Goal:

  • The HashMap values of the clicked Item should be send back to the MainActivity and SubActivity (with the ListView) should be closed.

Problem:

java.lang.RunTimeException: Failure delivering results ResultInfo{who=null, request=1, 
result=0, data=Intent { (has extras) }} to activity.

I think I made a huge mistake but I couldn't solve it.


switch case from onOptionsItemSelected and onActivityResult are in the MainActivity:

  • PICKED has the value 1;

    switch (item.getItemId()) {

    case R.id.icon:
        Intent myIntent = new Intent(MainActivity.this, HeroListActivity.class);
        MainActivity.this.startActivityForResult(myIntent, PICKED);
        return true;
    default:
        return super.onOptionsItemSelected(item);
    } 
    
    
    
    protected void onActivityResult(int requestCode, int resultCode, Intent data){
            super.onActivityResult(requestCode, resultCode, data);
            if (requestCode == 1) {
                Intent intent = getIntent();
                HashMap<String, String> aList = (HashMap<String, String>)intent.getSerializableExtra("map");
                Log.d("HashMap", aList.get("Name"));
            }
    

    }


onItemClick in the SubActivity with the ListView:

  • onItemClick is working and I get the correct data from the HashMap when I use Log.d.

    HashMap<String, String> o = (HashMap<String, String>) adapter.getItemAtPosition(position);
    Log.d("onItemClick", ""+o);
    
    Intent intent = new Intent();
    
    intent.putExtra("map", o);
    setResult(RESULTS_OK, intent);
    finish();
    

I hope someone can help me and thanks in advance.


Solution

  • You should replace this line in your onActivityResult() method

     Intent intent = getIntent();
    

    with this:

     Intent intent = data;
    

    data is the name of a parameter you are passing in - that's where your extras reside. getIntent() call returns a null intent in this instance.