Skip to content Skip to sidebar Skip to footer

Android - Pass Data Between Two Activities Get NullPointerException

I have 3 activities we will call them A, B and C. A and B both have intents that send the view to C. And design on which one C came from different things happen on C's activity. I

Solution 1:

Change the code in C class like this for checking the bundle is null or not.

Bundle extras = getIntent().getExtras();
if(extras != null){
   if(extras.getString("Action") != null)
      if (extras.getString("Action").equals("A")) {
        //DO SOMETHING
      }
   }  
 if(extras.getString("Action2") != null)
      if (extras.getString("Action2").equals("A2")) {
        //DO SOMETHING
      }
   }  
}

Solution 2:

check below code :

if(getIntent().hasExtra("Action"))
    //if you have passed "Action" from activity
else
   //if you did not pass "Action" from activity

Solution 3:

Try this...

Bundle extras = getIntent().getExtras();
if(extras != null){
if (extras.getString("Action").toString().equals("A") || extras.getString("Action").toString() == "A") {
//DO SOMETHING
}
}

Solution 4:

Hope this solves

Bundle extras = getIntent().getExtras();
if (extras != null && extras.getString("Action").equals("A")) {
//DO SOMETHING
}

Solution 5:

you are always creating new Intent object when ever you are moving to new activity. Hence this issue. Try to copy required data in new intent and access, them.


Post a Comment for "Android - Pass Data Between Two Activities Get NullPointerException"