This question already has an answer here:
Android Intent.getStringExtra() returns null
(1 answer)
Closed 1 year ago.
I'm having a weird problem when I try to send strings with intents when switching activities.
Here is what I do in class 1:
Intent myIntent = new Intent(SearchText.this, Search.class);
myIntent.putExtra("searchx", spinnerselected);
myIntent.putExtra("SearchText", input);
startActivity(myIntent);
Class 2:
Intent myIntent = getIntent();
searchText=myIntent.getStringExtra("SearchText");
spinnerselectednumber=Integer.parseInt(myIntent.getStringExtra("searchx"));
And using the debugger in the second class, its clear that there is a value in
searchx.
Though the line myIntent.getStringExtra("searchx") returns null .
Why is this?
Try to add .ToString() to myIntent.putExtra("searchx", spinnerselected); so that it is myIntent.putExtra("searchx", spinnerselected.ToString()); This always works for me
Was spinnerSelected a String?
From the Javadoc for Intent
public String getStringExtra(String name)
Since: API Level 1
Description: Retrieve extended data from the intent.
Parameters:
name The name of the desired item.
Returns: The value of an item that previously added with putExtra() or null if no String value was found.
There seems to be many ways to retrieve "extras" - whatever the type of spinnerSelected was, try to retrieve it using the appropriate method.
Eg if it was an int:
public int getIntExtra(String name, int defaultValue)
This code should work:
Bundle extras = getIntent().getExtras();
return extras != null ? extras.getString("searchx")
: "nothing passed in";
Related
i'm new in developing android in xamarin i just want to ask in how to pass data through other activity using intent ? This thing work (https://developer.xamarin.com/recipes/android/fundamentals/activity/pass_data_between_activity/) but i want to collect first all the data in 2 activity before they show the summary in my 3rd activity.(by the way i'm creating a registration app thanks for the future answers :) )
On the first activity you create the second activity by an intent and use the method PutExtra to pass the data you want with a relevant key name that you will need after starting the new activity to retrieve the data.
var secondActivity = new Intent (this, typeof(SecondActivity));
secondActivity.PutExtra ("Data", "Sample Data");
StartActivity(secondActivity);
On second activity OnCreate retrieve the data using the key name it was stored with and the correct method relevant to the data type passed. In this example as is a string by calling Intent.GetStringExtra.
string text = Intent.GetStringExtra ("Data") ?? "Data not available";
You can repeat 1 & 2 for the summary activity.
you can pass whole object and desterilize that in another activity like this
//To pass:
intent.putExtra("yourKey", item);
// To retrieve object in second Activity
getIntent().getSerializableExtra("yourKey");
For only single value you can use
//Method 1
string code = Intent.GetStringExtra("id") ?? string.Empty;
string name = Intent.GetStringExtra("Name") ?? string.Empty;
//OR
//Method 2
string Id = Intent.GetStringExtra("id") ?? string.Empty;
Item item = new Item();
item = itemRepo.Find(Convert.ToInt32(id));
I'm a noob to android development and I am attempting to pass a variable between two classes. According to my debugger, the value is being set in the first class, but for whatever reason it is returning null in the second class. I initially tried passing the value by making the variable public and static and then I attempted placing the value in the Extras. Both methods returned null in the second Activity. I have clean the project with no success. It seems as if it just won't won't allow the value of the string to be reset. Any help resolving this is greatly appreciated.
My Code
CLASS A
case R.id.profile:
selection = (Integer)v.getTag();
Log.e("Selection", String.valueOf(selection));
if(LandingActivity.user_isClient){
adapter_email = (listData.get(selection)).get("Email"); //<--debugger showing value as set here
Log.e("Adapter Email", adapter_email);
Intent myIntent = new Intent("com.tpssquared.kuttime.PROFILE_BARBER_VIEW");
myIntent.putExtra("ADAPTER_EMAIL", adapter_email); //<--debugger showing value as set here too
c.startActivity(myIntent);
}else{
adapter_email = (listData.get(selection)).get("Client_Email"); //<--debugger showing value as set here
Log.e("Adapter Email", adapter_email);
Intent myIntent = new Intent("com.tpssquared.kuttime.PROFILE_CLIENT_VIEW");
myIntent.putExtra("ADAPTER_EMAIL", adapter_email); //<--debugger showing value as set here too
c.startActivity(myIntent);
}
break;
CLASS B
barber_email_string ="";//Deubugger showing value as null and not "". WHY?
try{
if(barber_email_string.equals(null)||barber_email_string.equals("")){
barber_email_string = getIntent().getStringExtra("ADAPTER_EMAIL");
if(barber_email_string.equals(null)||barber_email_string.equals("")){
barber_email_string = Uri.encode(MyAppointments_ListAdapter.adapter_email);
Log.e("BPV email",barber_email_string);
}
}
Log.e("BPV email",barber_email_string);
}catch(Exception e){
e.printStackTrace();
Log.e("On Create", e.toString());
}
You aren't passing a Bundle to Activity B, you're passing a String extra. So when you call getExtras(), there's nothing there. You can simply call getStringExtra() on the Intent itself to get your string.
barber_email_string = getIntent().getStringExtra("ADAPTER_EMAIL");
Alternatively, you could create a new Bundle, package the string into that, and pass it with putExtras(myBundle), but that's overkill for a single string.
I'm making an Android quiz and one activity stores every question. Now the score displays on that activity and I would like it if it that score goes to a new activity.. and displays..
code in the quiz:
Intent theIntent = new Intent(this, Score.class);
theIntent.putExtra("somename", score);
startActivity(theIntent);
code in the score:
int i = getIntent().getIntExtra("somename");
I get an error on this word.. "getIntExtra" above in the score activity
getIntExtra() takes a second parameter (default value in case the extra can't be found):
http://developer.android.com/reference/android/content/Intent.html#getIntExtra(java.lang.String, int)
int i = getIntent().getIntExtra("somename", 0);
would do the job
You must provide a default value in the case that the integer doesn't exist in the intent extras.
Javadoc:
public int getIntExtra (String name, int defaultValue)
Retrieve extended data from the intent.
Parameters:
name: The name of the desired item.
defaultValue: the value to be returned if no value of the desired type is stored with the given name.
Returns
the value of an item that previously added with putExtra() or the default value if none was found.
I've used putExtra() to pass some data from one activity to other. I want to create a Parcelable instance but I am getting a null object.
Here is the code of first activity:
i = new Intent(Activity1.this, Activity2.class);
i.putExtra(com.login_app.Activity1.extra, "100");
startActivity(i);
Here is the code of second activity:
Intent inew = getIntent();
Bundle icicle1 = inew.getExtras();
// this is just a debug code
System.out.println(
icicle1.getSerializable(com.login_app.Activity1.extra).toString());
Parcelable p = inew.getParcelableExtra(com.login_app.Activity1.extra);
Here object p is a null object.
Please tell me if I am wrong or I need to add something else. I want this Parcelable object to be flattened into a Parcel object.
That's because you've put String, but trying to get Parcelable. You should use getStringExtra instead.
Also, from Bundle documentation of [getParcelable()][1] (this function is used to actually get extra from Intent's bundle):
Returns the value associated with the given key, or null if no mapping of the desired type exists for the given key or a null value is explicitly associated with the key.
So you basically get null because you have type mismatch.
cancel(getIntent().getExtras().getInt(“notificationID”));
why we use of dot operator in between these methods? as cancel(int) method takes only one integer parameter.it has 3 methods as parametr.....what exactly the code will do..?
This is a short way to write:
Intent intent = getIntent();
Bundle bundle = intent.getExtras(); // or getIntent().getExtras();
int i = bundle.getInt(“notificationID”); // or getIntent().getExtras().getInt(“notificationID”);
cancel(i); // or cancel(getIntent().getExtras().getInt(“notificationID”));
What you do is to invoke methods on the return value of each method.
You should try going through the concepts of object oriented programming first.
To answer your question, getIntent() returns an object of type intent. We call the getExtras() on the Intent object which returns an object of type Bundle. Then we call getInt() on the Bundle object to finally get the int we want to pass to the cancel() method.
The statement is equivalent to :
Intent i = getIntent();
Bundle b = i.getExtras();
int id = b.getInt("notificationID");
cancel(id);
If we don't need any of the intermediate objects, we can write the whole thing in a single line.
Hope that helps.
cancel(getIntent().getExtras().getInt(“notificationID”));.. even here cancel is getting only 1 arguement... because.. getIntent()= returns an intent intent.getExtras = returns the values it stores if extras has some object then .getInt(“notificationID”) = returns an Int value.. So finally only thing remaining is integer...