0

I am sending data in hashmap back to parent activity from child acitvity

here is my code Child activity

    private void sendDataToPrevPg(HashMap<String, String> hm) {     
    // Send to previous activity page
    Bundle bndle = new Bundle();
    bndle.putSerializable("stockList", (Serializable) hm);

    Intent intent = new Intent();
    intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
    intent.putExtra("stkList",bndle);
    setResult(RESULT_OK, intent);           
    this.finish();      
}

In parent

@SuppressWarnings("unchecked")
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {

    if (resultCode == RESULT_OK ) {         
        if (data.hasExtra("stkList")) {             

            Bundle wrapper = getIntent().getBundleExtra("stkList");
            HashMap<String, String> myClass3 = (HashMap<String, String>) wrapper.getSerializable("stkList");
            System.out.println("...serialized data4.."+myClass3);
        }
    }
}

showing error at line

 HashMap<String, String> myClass3 = (HashMap<String, String>)       wrapper.getSerializable("stkList");

Logcat:: Caused by: java.lang.NullPointerException

how to send data in hashmap? to parent activity

Aditi K
  • 1,534
  • 5
  • 22
  • 43

7 Answers7

3

I think you just simply missed the key "stkList" when you retrieved the HashMap from the bundle. It should be "stockList" instead.

Change this:

HashMap<String, String> myClass3 = (HashMap<String, String>) wrapper.getSerializable("stkList");

to this:

HashMap<String, String> myClass3 = (HashMap<String, String>) wrapper.getSerializable("stockList");
Zsolt Boldizsar
  • 2,447
  • 2
  • 27
  • 37
2
private void sendDataToPrevPg(HashMap<String, String> hm) {     
      // Send to previous activity page
     Intent intent = new Intent();
     intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
     intent.putExtra("stkList",bndle);
     setResult(RESULT_OK, intent);           
     this.finish();      
    }  

In parent Activity You can Receive HashMap value :

@Override
 protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    if (resultCode == RESULT_OK ) {         
    if (data.hasExtra("stkList")) {                      
        HashMap<String, String> myClass3 = (HashMap<String, String>) data.getSerializable("stkList");
        System.out.println("...serialized data4.."+myClass3);
        }
    }
  }
albertTaberner
  • 1,969
  • 1
  • 26
  • 35
1

Here I am showing sample code for your reference. I just tried this code, it works fine for me. Check this :

MainActivity :

    final HashMap<Integer, String> hashMap = new HashMap<Integer, String>();
    hashMap.put(1, "Hi");

    button.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View v) {
            // TODO Auto-generated method stub              

            Intent intent = new Intent(MainActivity.this, SecondActivity.class);
            intent.putExtra("h", hashMap);
            startActivity(intent);

        }
    });

SecondActivity :

Toast.makeText(SecondActivity.this, "Hi " + getIntent().getSerializableExtra("h").toString(), Toast.LENGTH_SHORT).show();

VVB
  • 7,363
  • 7
  • 49
  • 83
0

try this

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

    if (resultCode == RESULT_OK ) {         
        if (data.hasExtra("stkList")) {             
            Bundle wrapper = getIntent().getBundleExtra("stkList");
            HashMap<String, String> myClass3 = (HashMap<String, String>) wrapper.getSerializable("stockList");
            System.out.println("...serialized data4.."+myClass3);
        }
    }
}
CodePro_NotYet
  • 621
  • 6
  • 17
Sainath Patwary karnate
  • 3,165
  • 1
  • 16
  • 18
0

You put the wrong key. Change the error line to

HashMap<String, String> myClass3 = (HashMap<String, String>) wrapper.getSerializable("stockList");
CodePro_NotYet
  • 621
  • 6
  • 17
0

Issue is in this line of code

HashMap<String, String> myClass3 = (HashMap<String, String>) wrapper.getSerializable("stkList");

it should be stockList instead of stkList as below

HashMap<String, String> myClass3 = (HashMap<String, String>) wrapper.getSerializable("stockList");
Rajen Raiyarela
  • 5,526
  • 4
  • 21
  • 41
  • I done that but problem is still thr and list is not empty i check at child acitivity – Aditi K Jul 15 '14 at 06:19
  • do you mean you are still getting NullPointerException at above line of code even after changing the string key from stkList to stockList? – Rajen Raiyarela Jul 15 '14 at 06:30
  • 1
    can you pl try data.getBundleExtra("stkList") instead of getIntent().getBundleExtra("stkList") then in next line check if (wrapper!=null) then go in next lines else print the toast. it seems the wrapper (bundle) itself is null. – Rajen Raiyarela Jul 15 '14 at 06:35
  • wrapper is not null bcz i checked for null condition its working fine – Aditi K Jul 15 '14 at 06:40
  • Got it...Workig now Bundle wrapper = data.getBundleExtra("stkList"); HashMap myClass3 = (HashMap)wrapper.getSerializable("stockList"); – Aditi K Jul 15 '14 at 06:45
  • just edit your question with comments what changes you did and got it working so can help others in case they come up to this page. – Rajen Raiyarela Jul 15 '14 at 06:51
0

used data instead getIntent()

protected void onActivityResult(int requestCode, int resultCode, Intent data) {     
    if (resultCode == RESULT_OK ) {         
        if (data.hasExtra("stkList")) {                             
            Bundle wrapper = data.getBundleExtra("stkList");----data instead getIntent()
            if(wrapper!=null){
                HashMap<String, String> stockDtls = (HashMap<String, String>)wrapper.getSerializable("stockList");
            }
        }

    }
}
Aditi K
  • 1,534
  • 5
  • 22
  • 43