Reputation: 33
I want to pass a String from Activity to ListActivity in Android. When I use normal Activty to Activity String pass method it gives NullPointerException
. Here is what I tried to do.
Activity
Intent intent = new Intent(CheckAvailability.this , ListAtmActivity.class);
intent.putExtra("key", b_name);
startActivity(intent);
And I tried to get this from my ListActivity as this.
String brName=getIntent().getExtras().getString("key");
This working fine for Activity. But It gives NUllPointerException
when it use in ListActivity.
Please help me.
Upvotes: 0
Views: 468
Reputation: 897
May be your variable b_name is null, To avoid NUllPointerException
you can declare this variable like as
String b_name="";
If you variable is null then it will give NUllPointerException
Upvotes: 2
Reputation: 22291
Please Use below Code For Get Value from Intent in ListActivity, it will solve your problen.
Bundle bdl=getIntent().getExtras();
String mKey=bdl.getString("KEY");
Upvotes: 1
Reputation: 11191
Your code should work fine. As an alternative you could also use:
String brName=getIntent().getStringExtra("key");
Upvotes: 0
Reputation: 233
try using Bundle, that will sort out your problem
Bundle d=new Bundle();
d.putString("KEY",Your_String);
intent.putExtras(d);
startActivity(intent);
and now in the ListActivity
String Your_String_Variable = getIntent().getStringExtra("KEY");
Upvotes: 4