I want to simply start an activity and send some data to my second activity , this is my code :
Activiy main:
val intent = Intent(this#MainActivity, Cards::class.java)
intent.putExtra("title", catItem.name)
intent.putExtra("catId", catItem.id)
startActivity(intent)
catItem is not null and every item in it has value , i've debugged and I'm sure about it .
the second activity that I need to get data :
val bl:Bundle=intent.extras
catId=bl.getString("catId")
title=bl.getString("title")
it crashes on the second line :
bl.getString("catId") must not be null
I debugged and the bundle is completely empty .
what is wrong with this code ?
To retrieve data on second activity you just need to access directly intent's extra data as follows:
val catId = intent.getStringExtra("catId")
Also, be sure that "catId" type is String (an ID usually is an Integer or Long), because if it is not an String, you will get the same error.
According to your activity start code(just copy of your code):
val intent = Intent(this#MainActivity, Cards::class.java)
intent.putExtra("title", catItem.name)
intent.putExtra("catId", catItem.id)
startActivity(intent)
In Cards Activity onCreate method:
if they are
Required:
val arguments = requireNotNull(intent?.extras){"There should be parameters or your more meaningful message."}
with(arguments){
catId = getString("catId")
title = getString("title")
}
Not Required:
catId = intent?.extras?.getString("catId").orEmpty()
title= intent?.extras?.getString("title").orEmpty()
or
catId = intent?.extras?.getString("catId","default Cat").orEmpty()
title= intent?.extras?.getString("title","default Title").orEmpty()
Change getExtra to getStringExtra
bl.getStringExtra("catId")
I think in your code catItem.id is null, because of it catId is null, which causes of crash.
Please try below code to overcome the crash:
var bundle :Bundle ?=intent.extras
var title = bundle!!.getString("title")
var catId = bundle!!.getString("catId")
I hope its work for you.
Related
The value in string.xml file is.
<string name="hello_blank_fragment">Hello blank fragment</string>
From this Fragment i am trying to pass value to another activity ... by using putextra
i=new Intent(Basic.this.getContext(),DisplayBasicData.class);
i.putExtra("header", R.string.action_settings);
startActivity(i);
Then i try to get value using
String text_point = getIntent().getStringExtra("header");
But it not getting the value.
Try use Bundle :
Bundle extras = new Bundle();
extras.putString("header", getResources().getString(R.string.action_settings));
i=new Intent(Basic.this.getContext(),DisplayBasicData.class);
i.putExtras(extras);
startActivity(i);
and extract in DisplayBasicData activity:
String text_point = getIntent().getExtras().getString("header");
You are sending resource id ,not string
Here you can get string in two ways :
First way:
i.putExtra("header", R.string.action_settings);
int text_pointResourceId = getIntent().getIntExtra("header");
String text_point = getResources().getString(text_pointResourceId)
secondWay:
i.putExtra("header", getResources().getString(R.string.action_settings));
String text_point = getIntent().getStringExtra("header");
Hope it helps
Instead of sending data directly , it is better to use Bundle
you can pass that string simply in following way, no need of putting data in bundle.
Intent intent = new Intent(getActivity(), DisplayBasicData.class);
String myText = getActivity().getResources().getString(R.string.action_settings);
intent.putExtra("header", myText);
startActivity(intent);
then you can retrieve that data by using
String text_header = getIntent().getStringExtra("header");
Good evening guys,
I'm making an app and I want to know how to send a string to "List-View" in another activity ?
You can send data using the following code -
Intent intent = new Intent(this,newActivity);
intent.putExtra(name, value)
name = The name of the extra data
value = The String data value.
startActivity(intent);
In the new activity, you receive string via following (in onCreate)
Intent intent = getIntent();
String str = intent.getString(name)
name = The name of the extra data
Now search the web on how to add a string to list view. You will find it easily
I want to have more than one extra added to Intent. One to hold a double and one to hold long. Is this possible?
If so, how would I do it and how would I get the information from each extra?
You can add as many extras to an Intent as your heart desires, they are all just key value data:
Intent intent = new Intent();
intent.putExtra("name", "MyName");
intent.putExtra("age", 35);
intent.putExtra("weight", 155.6);
And they can be retrieved using the same key names:
String name = intent.getStringExtra("name");
int age = intent.getIntExtra("age", 0);
double weight = intent.getDoubleExtra("weight", 0.0);
intent.putExtra(#ExtraDoubleKey, #ExtraDoubleValue);
intent.putExtra(#ExtraLongKey, #ExtraLongValue);
Where #ExtraDoubleKey is a string that you will use to access the extra (i.e. "price" or something), and #ExtraDoubleValue is the value of the extra (the double variable you wish to pass). Similarly for #ExtraLongKey and #ExtraLongValue.
Then to access the extras in your next activity you can use:
double doubleValue = getIntent().getExtras().getDouble(#ExtraDoubleKey);
long longValue = getIntent().getExtras().getLong(#ExtraLongKey);
to get the value of the double extra with the key #ExtraDoubleKey.
https://stackoverflow.com/a/11461530/776075
Devunwired is correct.
But the way i see is
you can keep only one value per Type.
Like one string, one int, one double etc..
You are not capable of containing 2 string values. or two integers.
I have experienced this on a program and i have overcome it by using
one string and one Boolean.
You can use Bundle and pass it as a parameter to the Intent.
Intent nextActivity = new Intent(this, Activity2.class);
Bundle passData = new Bundle(); //to hold your data
passDataBndl.putString("fname", fname); //put in some String. the first parameter to it is the id, and the second parameter is the value
nextActivity.putExtras(passDataBndl); //Add bundle to the Intent
startActivityForResult(nextActivity, 0); //Start Intent
Thanks in advance.
When I print Log.d("me",getIntent().toString());
I am getting:
Intent { act=android.intent.action.CALL_PRIVILEGED dat=tel:888 flg=0x13800000 cmp=com.ninetology.freecall.two/.CallFinalActivity }
I am trying to fetch the value which is associated with "dat" but I am getting NullPointer exception.
//the code I am using is
getIntent().getStringExtra("dat"); // no use
//i tried
getIntent().getExtras("dat").toString(); // NullPointer exception
I tried with "tel" as key in above code still no use.
it seems you're doing this wrong.
The getExtras() function returns a bundle that you can extract data from and not a function that returns a specific String.
dat is NOT a String value as you can see from the data that was printed. it's a Uri,
try parsing it as you should and I'm sure you'll be able to get the data.
public void onCreate(Bundle b) { //mistyped
super.onCreate(b);
Uri data = getIntent().getData();
// OR USE THIS
String data = getIntent().getDataString();
// DO STUFF
}
try getIntent().getExtras().get("dat");
First of all, Its not necessary the string from Intent your are getting in log have a object with values..
So its better to just check its not a null, like,
Bundle bundle = getIntent().getExtras();
if(bundle ! = null)
{
// Now check you bundle object which has a values or not
}
else
{
// 1. get data in form of Uri
Uri data = getIntent().getData();
// 2. OR get string of Uri
String dataString = getIntent().getDataString();
// 3. Or split the data string
// The logic from this part may be different on your requirement.. I only suggests you to get data from string.. (Actual logic may different on your case)
String data = getIntent().toString();
data = data.subString(data.indexOf(":"), data.indexOf("flg")-1);
Log.e("tel:", data);
}
When you want to pass the data with the intent just add the below code before starting activity
intent.putExtra("dat", value); //value=the value you want to send
And when you want to fetch the same value in another activity just do:
Bundle bundle=getIntent().getExtras();
if(bundle!=null){
String string=bundle.getString("dat");
}
By doing this, you wont get the null pointer exception and will help you.
InboxDetailActivity.java:
Intent i = new Intent(InboxDetailActivity.this,Compose.class);
Bundle b = new Bundle();
b.putString("To", ConstantData.inbox_from);
Log.d("From Value", ConstantData.inbox_from);
b.putString("Subject", "RE:" + ConstantData.inbox_subject);
Log.d("Subject Value", ConstantData.inbox_subject);
b.putString("FromId", ConstantData.inbox_fromid);
Log.d("From Id Value",ConstantData.inbox_fromid);
i.putExtras(b);
startActivity(i);
Compose.java:
Intent i = getIntent();
Bundle b = i.getExtras();
to = b.getString("To");
subject = b.getString("Subject");
toId = b.getString("FromId");
I am getting NullPointerException at to = b.getString("To");
Bundle b = i.getExtras();
getExtras() returns null.
Agree with John's answer adding possible solution.
What you are doing is create a bundle , insert values in this and then pass this bundle.
And than you just fetch all the values one by one using its keys.
I am working with bundles but I simply add desired values directly using putExtra method. And I haven't got any problem till date. I recommend you to use put extra and check whether it works or not.
I would like to know what makes you to apply this way for bundles? Have you just read it somewhere and started applying this method ? or you have got some options and after some consideration you found it better to apply this method OR your requirement states that. Because normally me and my peers doesn't use bundles and directly pass the extras. And that works for me every time.
using this instead of bundle
i.putString("To", ConstantData.inbox_from);
Log.d("From Value", ConstantData.inbox_from);
i.putString("Subject", "RE:" + ConstantData.inbox_subject);
Log.d("Subject Value", ConstantData.inbox_subject);
i.putString("FromId", ConstantData.inbox_fromid);
Log.d("From Id Value",ConstantData.inbox_fromid);
and in another class..
to = getIntent().getString("To");