null value in getIntent.getExtras.getString() - android

This is my code in my first activity:
Intent i = new Intent(this, OtherScreen.class);
i.putExtra("id1", "first");
i.putExtra("id2", "second");
startActivity(i);
where first,second are the values I want to be passed.
and on my other class i have this:
Intent i = getIntent();
Bundle extras = i.getExtras();
String result = extras.getString("id1");
System.out.println("yeah"+result);
but after i run it, my result is null.Can you help me?
If I write my getString in that ways, I am getting syntax errors.
String result = extras.getString(id1); //id1 cannot be resolved to a variable
String result = extras.getString("id1","default value"); // remove argument

Intent i = new Intent(yourcurrentactivity.this, OtherScreen.class);
i.putExtra("id1", "first");
i.putExtra("id2", "second");
startActivity(i);
Bundle extras = getIntent().getExtras();
if (extras != null) {
String result = extras.getString("id1");
System.out.println("yeah"+result);
}

Alternatively for more accuracy, you can use
`if(getIntent.hasExtras("id11")'
before actually getting extras from the bundle and do some action if its null

try:
getIntent().getStringExtra("id11");
Of course its best to FIRST check if it even has the extra before you even go for it.
Ref:
http://developer.android.com/reference/android/content/Intent.html#getStringExtra(java.lang.String)

I had the same problem in my project.
intent.putExtra("key",editText.getText())
I have changed editText.getText() to String text.
String text = editText.getText();
intent.putExtra("key",text);
And that has solved my problem. Seems like it is an API problem.

You may try :String result = extras.get("id1");

I was also having trouble with null values. #YuDroid's suggestion to use if (getIntent.hasExtras("id11") is a good idea. Another safety check that I found when reading the Bundle documentation, though, is
getString(String key, String defaultValue)
That way if the string does not exist you can provide it with a default value rather than it being returned null.
So here is #AprilSmith's answer modified with the default String value.
Intent i = new Intent(yourcurrentactivity.this, OtherScreen.class);
i.putExtra("id1", "first");
i.putExtra("id2", "second");
startActivity(i);
Bundle extras = getIntent().getExtras();
if (extras != null) {
String result = extras.getString("id1", "myDefaultString"); // This line modified
System.out.println("yeah"+result);
}

If anybody still having this issue..here is the solution
the datatype you are passing in first activity should be same in receiving end.
'Intent myIntent = new Intent(D1Activity.this, D2Activity.class);'
'myIntent.putExtra("dval", dval);'
' D1Activity.this.startActivity(myIntent);'
in D2Activity
Bundle extras = getIntent().getExtras();
if (extras != null) {;
edval = extras.getInt ( "dval" );
}
here extras.getString() will not work as we passed an Int not a string

Related

send value from one activity to another [duplicate]

This question already has answers here:
How do I pass data between Activities in Android application?
(53 answers)
Closed 2 years ago.
I have made the code and there's no error, but logcat shows a NullPointerException. This is my code to pass and to catch the value from each activity. Please help me solve this.
First activity:
try {
Intent in = new Intent();
in.setClass(p_daftar_pelanggan.this, ubah_data_pelanggan.class);
in.putExtra("id", eid.getText().toString());
in.putExtra("nama", tnama.getText().toString());
in.putExtra("alamat", ealamat.getText().toString());
in.putExtra("no_hp", ehp.getText().toString());
startActivity(in);
}
catch(NullPointerException ex)
{
}
Second activity:
Bundle b = getIntent().getExtras();
String idpgu = b.getString("id");
String namapgu = b.getString("nama");
String alamatpgu = b.getString("alamat");
String hppgu = b.getString("no_hp");
idp.setText(idpgu);
idp.setKeyListener(null);
nama.setText(namapgu);
ealamat.setText(alamatpgu);
hp.setText(hppgu);
Try the following in the second activity:
Intent intent = getIntent();
String idpgu = intent.getStringExtra("id");
You can use Bundle for this purpose:
Activity destActivity = new Activty();
Bundle b = new Bundle();
destActivity.putString("TAG_ForValue",YourValue.getText().toString());
destActivity.setArguments(b);
Intent intent = new Intent(startActivity.this, destActivity.class);
startActivity(intent);
And in your destActivity :
Bundle bundle = getArguments();
if(bundle !=null)
String value = bundle.getString("TAG_ForValue");
You are sending and receiving the values correctly, but probably some values are null, you can validate setting an empty string instead of a null value:
Bundle b = getIntent().getExtras();
String idpgu = (b.getString("id") != null)? b.getString("id") : "";
String namapgu = (b.getString("nama") != null)? b.getString("nama") : "";
String alamatpgu = (b.getString("alamat") != null)? b.getString("alamat") : "";
String hppgu = (b.getString("no_hp") != null)? b.getString("no_hp") : "";
You put extras using Intent in FirstActivity But Use Bundle to receive it, that is why you have NullPointerException because SecondActivity is looking for values in different place.
Do the following in your secondActvity
Intent intent = getIntent();
String idpgu = intent.getStringExtra("id");
String namapgu = intent.getStringExtra("nama");
String alamatpgu = intent.getStringExtra("alamat");
String hppgu = intent.getStringExtra("no_hp");
// Check Null for each Values e.g.
if (idpgu.trim().equals(" "))
{ idpgu = "null";
}

Cannot assign variable value to a string from extra.getString();

New to android, I'm currently trying to send a String value from one Activity to another. I have looked through several threads like How to use putExtra() and getExtra() for string data for an answer, but I cannot get it to work.
The string I want to send is:
public void golf(View view) {
Intent intent = new Intent(SearchSport.this, EventList.class);
intent.putExtra("type", "golf");
startActivity(intent);
and my receiver looks like
String type;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_event_list);
Intent intent = getIntent();
Bundle extras = intent.getExtras();
if ( extras != null)
type = extras.getString("type");
if (type == "golf"){
TextView eventName = (TextView) findViewById(R.id.EOName);
TextView eventTime = (TextView) findViewById(R.id.EOTime);
TextView eventLocation = (TextView) findViewById(R.id.EOLocation);
DatabaseOperations dop = new DatabaseOperations(ctx);
Cursor CR = dop.getInformation(1);
CR.moveToFirst();
eventName.setText(CR.getString(1));
eventTime.setText(CR.getString(7) + " " + CR.getString(8) + ". " + CR.getString(9) + " kl. " + CR.getString(10) + ":" + CR.getString(11));
eventLocation.setText(CR.getString(4));}
else {Toast toast = Toast.makeText(this, "Error in Type.", Toast.LENGTH_LONG);
toast.show();}
I have no Idea what's wrong here. The app displays the toast everytime I test it, instead of filling out the TextViews with the data from my database entry.
EDIT: Code has been changed based on answers, but the problem has not yet been solved. Writing if (type.equals("golf")){} instead of if (type == "golf"){} crashed the app.
EDIT 2: Problem solved! Fixed case sensitivity, used .equals instead of ==, and wrote the receiver as
if(getIntent().hasExtra("Type"))
type = getIntent().getStringExtra("Type");
In the end, it turns out is was the virtual device I used which crashed the app, for as of yet unknown reasons. When tested on an actual android phone, the app works as intented.
Thanks to everyone for their help!
try this
if(getIntent().hasExtra("Type"))
type = getIntent().getStringExtra("Type");
Change the key it is case sensitive :
type = extras.getString("Type");
Key is wrong when try to get String from Intent Extra :
type = extras.getString("type");
Replace with :
type = extras.getString("Type");
Note : Also use equals() instead == for comparing String
if (type.equals("golf")){
1) First keys are case sensitive
2) pass as
Bundle bundle = new Bundle()
bundle.putString("type","golf");
intent.putExtras(bundle);
after that get it using
Bundle received = i.getExtras();
received.getString("type");
As you are passing the value using Extra. You must have to catch that using getStringExtra(); and the Key must be case sensitive.
Intent i = getIntent();
type = i.getStringExtra("Type");

Sending ApplicationInfo from one activity to another

This is how I send it
Intent i = new Intent(v.getContext(), Permissions.class);
i.putExtra("AppSelected",installedApps.get((int) id));
startActivity(i);
THis is how I retrieve it:
Bundle extras = getIntent().getExtras();
ApplicationInfo a = extras.get[What do I put here?]("AppSelected");
I cannot seem to figure out get this to work. Any thoughts?
Call getParcelable() on the Bundle to retrieve your ApplicationInfo (which implements the Parcelable interface). You will need to cast the result to ApplicationInfo.
Assuming installedApps.get... returns an integer, you should change your code
Bundle extras = getIntent().getExtras();
for
int appId = getIntent().getIntExtra("AppSelected", 0);
to retrieve any extra just use the key that you use to save value plus a default in some case
example
Bundle extras = getIntent().getExtras();
int example = extras.getIntExtra("key", int defaultValue) ;//to retrieve an Integer
String example = extras.getStringExtra("key"); //to retrieve a String
.....son so forth
in your case
Bundle extras = getIntent().getExtras();
ApplicationInfo a = extras.getIntExtra("AppSelected", 0);

Problems with putExtra and putStringArrayList

I'm trying to send some data from one activity to another and it's sorta working but not like I want to work.
Problem 1-Things are getting mixed up. On the Next Activity part of the listitem is going to an incorrect textView and part to the correct textview.
Problem 2- I am only able to list 1 item on the new activity but I want to be able to send multiple listitems. I think the problem lies in combining different types of putExtra request to the same place like I do here.
.putExtra("inputPrice",(CharSequence)pick)
.putStringArrayListExtra("list", listItems)
Ant help would be appreciated.
Sending Data to next Activity
final TextView username =(TextView)findViewById(R.id.resultTextView);
String uname = username.getText().toString();
final TextView uplane =(TextView)findViewById(R.id.inputPrice);
String pick = uplane.getText().toString();
final TextView daplane =(TextView)findViewById(R.id.date);
String watch = daplane.getText().toString();
startActivity(new Intent(MenuView1Activity.this,RecordCheckActivity.class)
.putExtra("date",(CharSequence)watch)
.putExtra("Card Number",(CharSequence)uname)
.putExtra("inputPrice",(CharSequence)pick)
.putStringArrayListExtra("list", listItems)
);
finish();
This is the Next Activity
Intent is = getIntent();
if (is.getCharSequenceExtra("Card Number") != null) {
final TextView setmsg = (TextView)findViewById(R.id.saleRccn);
setmsg.setText(is.getCharSequenceExtra("Card Number"));
}
Intent it = getIntent();
if (it.getCharSequenceExtra("date") != null) {
final TextView setmsg = (TextView)findViewById(R.id.saleTime);
setmsg.setText(it.getCharSequenceExtra("date"));
}
Intent id1 = getIntent();
if (id1.getCharSequenceExtra("inputPrice") != null) {
final TextView setmsg = (TextView)findViewById(R.id.saleName);
setmsg.setText(id1.getCharSequenceExtra("inputPrice"));
}
ArrayList<String> al= new ArrayList<String>();
al = getIntent().getExtras().getStringArrayList("list");
saleNotes= (TextView) findViewById(R.id.saleNotes);
saleNotes.setText(al.get(0));
Alright, a few things:
First of all you do not need to cast your strings as CharSequence.
Second thing,
Define intent, add your extras and only then call startActivity as below:
Intent intent = new Intent(MenuView1Activity.this,RecordCheckActivity.class);
intent.putExtra("date", watch);
startActivity(intent);
Third, when retrieving the intent create a bundle first as below:
Bundle extras = getIntent().getExtras();
String date = extras.getString("date");
EDIT:
Here is how you convert your entire array list to one single string and add it to your textview.
String listString = "";
for (String s : al)
{
listString += s + "\t"; // use " " for space, "\n" for new line instead of "\t"
}
System.out.println(listString);
saleNotes.setText(listString);
Hope this helps!
Try this, Don't use CharSequence just put string value
startActivity(new Intent(MenuView1Activity.this,RecordCheckActivity.class)
.putExtra("date",watch)
.putExtra("Card Number",uname)
.putExtra("inputPrice",pick)
.putStringArrayListExtra("list", listItems)
);
And get like this
Intent is = getIntent();
if (is.getCharSequenceExtra("Card Number") != null) {
final TextView setmsg = (TextView)findViewById(R.id.saleRccn);
setmsg.setText(is.getStringExtra("Card Number"));
}

Passing Integer Between Activities and Intents in Android Is Always Resulting in Zero / Null

I'm attempting to pass two integers from my Main Page activity (a latitude and longitude) to a second activity that contains an instance of Google Maps that will place a marker at the lat and long provided. My conundrum is that when I retrieve the bundle in the Map_Page activity the integers I passed are always 0, which is the default when they are Null. Does anyone see anything glaringly wrong?
I have the following stored in a button click OnClick method.
Bundle dataBundle = new Bundle();
dataBundle.putInt("LatValue", 39485000);
dataBundle.putInt("LongValue", -80142777);
Intent myIntent = new Intent();
myIntent.setClassName("com.name.tlc", "com.name.tlc.map_page");
myIntent.putExtras(dataBundle);
startActivity(myIntent);
Then in my map_page activity I have the following in onCreate to pick up the data.
Bundle extras = getIntent().getExtras();
System.out.println("Get Intent done");
if(extras !=null)
{
System.out.println("Let's get the values");
int latValue = extras.getInt("latValue");
int longValue = extras.getInt("longValue");
System.out.println("latValue = " + latValue + " longValue = " + longValue);
}
Geeklat,
You don't need to use Bundle in this case.
Do your puts like this...
Intent myIntent = new Intent();
myIntent.setClassName("com.name.tlc", "com.name.tlc.map_page");
myIntent.putExtra("LatValue", (int)39485000);
myIntent.putExtra("LongValue", (int)-80142777);
startActivity(myIntent);
Then you can retrieve them with...
Bundle extras = getIntent().getExtras();
int latValue = extras.getInt("LatValue");
int longValue = extras.getInt("LongValue");
System.out.println("Let's get the values");
int latValue = extras.getInt("latValue");
int longValue = extras.getInt("longValue");
Not the same as
myIntent.putExtra("LatValue", (int)39485000);
myIntent.putExtra("LongValue", (int)-80142777);
Also it might be because you do not keep the name of the Int exactly the same throughout your code. Java and the Android SDK are Case-sensitive

Categories

Resources