Reputation: 1187
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 pass the data like this from A to C:
Intent intent = new Intent(this, C.class);
intent.putExtra("Action", "A");
startActivity(intent);
Then in C's onCreate method I have something like this:
Bundle extras = getIntent().getExtras();
if (extras.getString("Action").equals("A")) {
//DO SOMETHING
}
Then I from B to C I have
Intent intent = new Intent(this, C.class);
startActivity(intent);
And then I get the NullPointerException, I am guessing this is because i have not specifed a String "Action" when going from B to C.
Now I could just add one line for B in this case but, if there were for more activities or a large project this would not be good, because for each activity going to one I would need this.
How can I have it so I don't get this exception without adding an "Action" sting to activity B?
Thanks for the help in advance.
EDIT
If I have this from A to C
Intent intent = new Intent(this, C.class);
intent.putExtra("Action", "A");
intent.putExtra("Action2", "A");
startActivity(intent);
And this from B to C
Intent intent = new Intent(this, C.class);
intent.putExtra("Action", "B");
startActivity(intent);
And then this in onCreate of C it fails when going from B to C:
Bundle extras = getIntent().getExtras();
if (extras != null) {
if (extras.getString("Action").equals("A")) {
//DO SOMETHING
}
else if (extras.getString("Action2").equals("A")) {
//DO Stuuf
}
}
Upvotes: 0
Views: 1888
Reputation: 2609
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
}
}
}
Upvotes: 2
Reputation: 373
You are starting the Activity C twice( whenever you call startActivity(intent) ). First time from A and second time from B. Both times you are checking this
Bundle extras = getIntent().getExtras();
if (extras.getString("Action").equals("A")) {
//DO SOMETHING
}
Which is bound to give you a null pointer exception. Put up a break point in your IDE and verify it. why don't you use some static variable to share the info between the activities in case you don't want to instantiate the class C more than once.
Upvotes: 0
Reputation: 1224
NullPointerException
This will Happen because When you are moving from Activity A to C you are passing intent with value and in activity C you are getting that value using Bundle. But When you Move From Activity B to C you are not passing values through intent and in Activity C you have Written this
Bundle extras = getIntent().getExtras();
if (extras.getString("Action").equals("A")) {
//DO SOMETHING
}
at this tme the extra is null So you will get Error.
DO like this
Bundle extras = getIntent().getExtras();
if(extras != null)//This one will check for null
if (extras.getString("Action").equals("A")) {
//DO SOMETHING
}
EDIT For passing Multiple values from activity
Intent intent= new Intent(this,NextActivity.class);
Bundle extra = new Bundle();
extra.putString("Action1","Value1");
extra.putString("Action2","Value2");
intent.putExtras(extra);
startActivity(intent);
In NextActivity
Intent i = getIntent();
Bundle extras = i.getExtras();
String strAction1 = extras.getString("Action1");
String strAction2 = extras.getString("Action2");
Upvotes: 0
Reputation: 830
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.
Upvotes: 0
Reputation: 441
Hope this solves
Bundle extras = getIntent().getExtras();
if (extras != null && extras.getString("Action").equals("A")) {
//DO SOMETHING
}
Upvotes: 0
Reputation: 1187
Try this...
Bundle extras = getIntent().getExtras();
if(extras != null){
if (extras.getString("Action").toString().equals("A") || extras.getString("Action").toString() == "A") {
//DO SOMETHING
}
}
Upvotes: 0
Reputation: 35549
check below code :
if(getIntent().hasExtra("Action"))
//if you have passed "Action" from activity
else
//if you did not pass "Action" from activity
Upvotes: 0