I'm trying to pass a string between two activities. I've done this in other projects using the same method, but for some reason I'm getting a NullPointerException when I call intent.getStringExtra(String). I have also tried creating a Bundle for the extras via
Bundle b = getIntent().getExtras();
but that also returned null. Below is the code that I am currently trying to use.
Activity A:
Intent myIntent = null;
String select = "";
if (selection.equals("Chandelle")) {
myIntent = new Intent(Commercial.this, Chandelle.class);
select = "Chandelle";
} else if (selection.equals("Eights on Pylons")) {
myIntent = new Intent(Commercial.this, EightsOnPylons.class);
select = "Eights on Pylons";
}
// Start the activity
if (myIntent != null) {
myIntent.putExtra("selection", select);
Log.d("*** OUTBOUND INTENT: ", "" + myIntent.getExtras().get("selection"));
startActivity(myIntent);
}
Here's the code in activity B that tries to pull the extra:
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Intent i = getIntent();
if (i == null)
Log.d("***DEBUG****", "Intent was null");
else
Log.d("**** DEBUG ***", "Intent OK");
String MANEUVER_ID = i.getStringExtra("selection"); //Exception points to this line
Log.d("*** DEBUG", rec + " " + MANEUVER_ID);
I've tried pretty much every alternative way of passing extras, but they all seem to behave this way. What am I missing?
Add .ToString() to myIntent.putExtra("selection", select); so that it is myIntent.putExtra("selection", select.ToString());
Luckily I found this post. I've been struggling with this for hours and in the end I realized that I'm sending my intent to the tabHost as well. ;-) For those who are still having problems with this just get the extras from the tabHost activity itself and pass it along to the child tabs.
String userID;
.
.
Bundle getuserID = getIntent().getExtras();
if (getuserID != null) {
userID = getuserID.getString("userID");
}
...
intent = new Intent().setClass(this, Games.class);
intent.putExtra("userID", userID);
spec = tabHost.newTabSpec("games").setIndicator("Games",
res.getDrawable(R.drawable.tab_games))
.setContent(intent);
tabHost.addTab(spec);
Try the code below. I have added an override to your code, which will do the trick:
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Intent i = getIntent();
if (i == null)
Log.d("***DEBUG****", "Intent was null");
else
Log.d("**** DEBUG ***", "Intent OK");
String MANEUVER_ID = i.getStringExtra("selection"); //Exception points to this line
Log.d("*** DEBUG", rec + " " + MANEUVER_ID);
}
Look at your code, I think it may happen when you did put anything in Activity 1. Because you use "if...else if", and you don't have "else" for other cases. If that happens then you will get null value in the Activity 2. Check again this case.
If you still have not put anything in the bundle, it will always return null!
You can create and set the bundle by your own :
Intent i = new Intent( ... );
i.putExtras(new Bundle());
Bundle bundle= i.getExtras(); // (not null anymore)
Or, put a dummy value just to force the initialization (I prefer the first solution):
Intent i = new Intent( ... );
i.putExtra("dummy", true);
Bundle bundle= i.getExtras(); // (not null anymore)
My problem was resolved when I figured out not to startAcitvity() before I used putExtra().
Newbie mistake but I hope somebody will find it useful.
I changed this
Intent i1 = new Intent(MainActivity.this,Second.class);
startActivity(i1);
String abc = e1.getText().toString();
i1.putExtra("user",abc);
to this
Intent i1 = new Intent(MainActivity.this,Second.class);
String abc = e1.getText().toString();
i1.putExtra("user",abc);
startActivity(i1);
In activity B you at the following function:
protected String getStringExtra(Bundle savedInstanceState, String id) {
String l;
l = (savedInstanceState == null) ? null : (String) savedInstanceState
.getSerializable(id);
if (l == null) {
Bundle extras = getIntent().getExtras();
l = extras != null ? extras.getString(id) : null;
}
return l;
}
You are using it in the following way in activity B:
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
String MANEUVER_ID = getStringExtra(savedInstanceState, "selection");
}
Related
I want to pass a specific String from a fragment to an activity to use it there.
When I am using Intent and Bundle, the value is null. I have tried several methods but can't seem to make it correctly. Can someone help, please?
In my Fragment :
Intent testintent = new Intent(getActivity(), MapActivity.class);
Bundle bundle = new Bundle();
bundle.putString("testing", "This is a test");
testintent.putExtras(bundle);`
And in my Activity :
String message = getIntent().getStringExtra("testing");
System.out.println("Value is : " + message);`
And this is what I am getting as a result in my console :
I/System.out: Value is : null
In the Fragment :
val intent = Intent(this, MapsActivity::class.java)
intent.putExtra("testing", "This is a test")
startActivity(intent)
In the receiving Activity :
val extras = intent.extras
if (extras != null) {
val receivingString = extras.getString("testing")
System.out.println("Value is : " + receivingString)
} else {
}
Using jave:
Intent intent = new Intent(getActivity(), MapActivity.class);
intent.putExtra("testing", "This is a test");
getActivity.startActivity(intent);
I have a List Activity that when the user presses a line item, it passes the ID of the record to the next activity.
Intent intent = new Intent(CustomerListActivity.this, CustomerEditActivity.class);
intent.putExtra("CUSTOMER_ID", id);
startActivity(intent);
I can see the data in the intent when debugging on that activity; however, when I get into the next activity, the data is not coming up with the below code.
Intent i = getIntent();
Bundle b = i.getExtras();
String s = b.getString("CUSTOMER_ID");
I have debugged and poked around in the variables window, but I do not see the Customer_ID=# value as I do on the previous activity.
You should call String s = b.getStringExtra("CUSTOMER_ID");
Try this.It will work.Paste it on your next activity where you want to get data from intent.
String s= getIntent().getExtras().get("CUSTOMER_ID")+"";
This is how you can do it in a right way
//main activity
Intent intent = new Intent(getActivity(), TargetActivity.class);
intent.putExtra("ParamKey", "key_value");
getActivity().startActivity(intent);
****
//TargetActivity
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.target_activity_layout);
Bundle extras = getIntent().getExtras();
if (extras != null) {
if (extras.get("ParamKey") != null)
paramValue= extras.getString("ParamKey", "default_value");
}
You will have to call getStringExtra() method in New Activity to retrieve the value from Intent.
For example :
String custIdInNewAct= getIntent().getStringExtra("CUSTOMER_ID");
Bundle contains data sent by the calling activity so
Bundle b = getIntent().getExtras();
String s = b.getString("CUSTOMER_ID")`;
Currently I have an ArrayList that I wanted to pass to another activity. I've already applied getParcelableArrayListbut it pass null to another activity. I've check the array for null, it's not. In the current activity before I pass it at least.
This is my code:
RecyclerViewOffline.java
Toast.makeText(context,"Clicked", Toast.LENGTH_SHORT).show();
Intent intent = new Intent(view.getContext(),OfflineActivity.class);
Log.d("Contexts Test : ", fileContents.get(holder.getLayoutPosition()).getTitle());
Bundle bundle = new Bundle();
//bundle.putParcelableArrayList("link",fileContents);
bundle.putParcelableArrayList("ArrayList",fileContents);
bundle.putInt("position",holder.getLayoutPosition());
intent.putExtra("bundle",bundle);
view.getContext().startActivity(intent);
My receiving end:
OfflineActivity.java
Bundle extras = getIntent().getBundleExtra("bundle");
if(extras != null) {
this.fileContents = extras.getParcelableArrayList("ArrayList");
layoutPosition = extras.getInt("position");
Log.d("Contexts Test Offline: ",""+ fileContents.get(layoutPosition).getTitle());
}
else{}
As I can see
You didnt add the arraylist in the Parcelable in and out method and contructor.
This will solve your issue.
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
}
}
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
}
}
}
check below code :
if(getIntent().hasExtra("Action"))
//if you have passed "Action" from activity
else
//if you did not pass "Action" from activity
Try this...
Bundle extras = getIntent().getExtras();
if(extras != null){
if (extras.getString("Action").toString().equals("A") || extras.getString("Action").toString() == "A") {
//DO SOMETHING
}
}
Hope this solves
Bundle extras = getIntent().getExtras();
if (extras != null && extras.getString("Action").equals("A")) {
//DO SOMETHING
}
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.
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");
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.
I am learning android and curious to know as if startActivityForResult() contains the properties of startActivity() too, i.e. can it be used to send data like startActivity() beside receiving data from called activity?
Here is the code:
SendData Activity:
Intent data= new Intent(SendData.this, RecieveData.class);
Bundle check = new Bundle();
check.putString("UmerData", cheese);
medt.setText(cheese);
data.putExtras(check);
startActivityForResult(data, 5);
Should receive data in this activity (RecieveData Activity)
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.recievedata);
Initializek();
Bundle got = getIntent().getExtras();
String ss= got.getString("UmerData");
if(getIntent()!=null && ss!=null ) {
rt1.setText("Is not Null");
}
}
Your help will be really appreciated !
Thanks
Yes, startActivity & startActivityForResult, both take intent as param. You can bundle launch data inside intent and pass it over to target activity.
When you use startActivityForResult(), you have to also create a onActivityResult() method within the Activity that called startActivityForResult(). onActivityResult() is where you can access the Intent stored by the Activity you start for a result.
In the Activity that is then started, you have to call setResult() to store the Intent that you store the data in.
Read over this: http://developer.android.com/reference/android/app/Activity.html#StartingActivities
edit: Misread your question. For passing a Bundle of options through, I would use this overload:
startActivity(Intent, int, Bundle)
I'm working in Xamarin Android so the code is C# but I had the same problem except I'm sending between separate apps. I eventually got it working so here is my example.
var intent = new Intent();
intent.SetComponent(new ComponentName("com.company.packageName", "com.company.packageName.activityName"));
intent.PutExtra(Intent.ExtraText, message);
StartActivityForResult(intent, 1);
// in app being started
protected override void OnResume()
{
base.OnResume();
Intent intent = Intent; // Equivalent to getIntent()
if (intent != null)
{
string str = intent.GetStringExtra(Intent.ExtraText);
if (str != null)
{
// Do stuff with str
}
else
{
//Show Error
}
}
else
{
//Show Error
}
}