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";
}
Related
Intent in = new Intent(MainActivity.this, Player.class);
String SongName = SpinSongSelector.getSelectedItem().toString ();
startActivity (in);
i wan to send SongName to next Activity
Try this in your first activity,
Intent intent = new Intent(MainActivity.this, Player.class);
String SongName = SpinSongSelector.getSelectedItem().toString ();
intent.putExtra("message", SongName);
startActivity(intent);
and then in second activity get your string like below
Bundle bundle = getIntent().getExtras();
String message = bundle.getString("message");
then set this string to text-view (This is not necessary but just for your reference, if you want to check whether you got correct string or not)
TextView txtView = (TextView) findViewById(R.id.your_resource_textview);
txtView.setText(message);
use this line before startActivty :
intent.putExtra("SongName",SongName);
then retrieve it from the target activty.
just use
first activity to send :
Intent in = new Intent(MainActivity.this, Player.class);
String SongName = SpinSongSelector.getSelectedItem().toString ();
in.putExtra("data",SongName);
startActivity (in);
to receive in another activity:
String Sname = getIntent().getExtras().getString("data");
to see the details of intents refer here : http://developer.android.com/guide/components/intents-filters.html
protected void onMessage(Context context, Intent intent) {
String msg = intent.getStringExtra("payload");
String newspushid1= intent.getStringExtra("payload1");
gc.setnews_pushid(newspushid1);
mNotificationManager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
BackgroundAlert bg = new BackgroundAlert(mNotificationManager, msg);
bg.onReceive(getApplicationContext(), intent);
}
In this i am Getting value of msg and Push id (helloworld and 101 respectively) i want get this value in another activity .
if (gdata.getPush_message() != null) {
String push_id_value = gdata.getnews_pushid();
Log.e("CATID", "ID-->" + push_id_value);
}
I have apply this code for getting value .from GCMIntentService class but i am getting same value push_id_value ,getPush_message in another activity which is hello world i am unable to get value 101 in another activity please check my code tell me solution .
Create a Constant Class like :
public class Constant {
public static String PAYLOAD = "";
public static String PAYLOAD_1 = "";
}
Now, you can set this value in Activity1 like
Constant.PAYLOAD = intent.getStringExtra("payload");
Constant.PAYLOAD_1= intent.getStringExtra("payload1");
Now, get this value in Activity2 like:
String push_id_value = Constant.PAYLOAD;
String push_id_value1 = Constant.PAYLOAD_1;
And/or for Preference go to my this answer:Android. How to save user name and password after the app is closed?
In your current Activity,
Intent i = new Intent(getApplicationContext(), NewActivity.class);
i.putExtra("new_variable_name","value");
Then in the new Activity, retrieve those values:
Bundle extras = getIntent().getExtras();
if (extras != null) {
String value = extras.getString("new_variable_name");
}
Use this technique to pass values from one Activity to another.
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"));
}
This question already has answers here:
How to pass a value from one Activity to another in Android? [duplicate]
(7 answers)
Closed 9 years ago.
i would like to send the values from one activity to another but i got null pointer
exception please solve my problem.the first activity contains sms the details of that
sms is send to second activity based on these values th esecond activity search contact
no and send reply sms.
public void onReceive( Context context, Intent intent )
{
// Get SMS map from Intent
Bundle bundle = null;
Bundle extras = intent.getExtras();
String messages = "";
String address = null,body=null;
if ( extras != null )
{
// Get received SMS array
Object[] smsExtra = (Object[]) extras.get( "pdus" );
// Get ContentResolver object for pushing encrypted SMS to incoming folder
//ContentResolver contentResolver = context.getContentResolver();
for ( int i = 0; i < smsExtra.length; ++i )
{
SmsMessage sms = SmsMessage.createFromPdu((byte[])smsExtra[i]);
body = sms.getMessageBody().toString();
address = sms.getOriginatingAddress();
messages += "SMS from " + address + " :\n";
messages += body + "\n";
// Here you can add any your code to work with incoming SMS
// I added encrypting of all received SMS
}
// Display SMS message
Toast.makeText( context, messages, Toast.LENGTH_SHORT ).show();
Intent i=new Intent(context,AlertActivity.class);
bundle.putString("from",address);
bundle.putString("msg",body);
i.putExtras(bundle);
i.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
context.startActivity(i);
}
}
Activity2:
Intent i=getIntent();
Bundle bundle=i.getExtras();
String fromAdd=bundle.getString("from");
String msgBody=bundle.getString("body");
Change this
String msgBody=bundle.getString("body");
to
String msgBody=bundle.getString("msg");
in Android Bundle we put key value pair, and it is mandatory that you pass same key, while getting data from bundle, which you are putting data. check your code you are putting two string on intent, which are:
"from", and "msg"
and you are getting values from intent by key:
"from" and "body"
so change it either in starting activity or Activity 2. so that key values would match.
try this:
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
Bundle bundle = this.getIntent().getExtras();
if (bundle != null)
{
String fromAdd = bundle.getString("from");
String msgBody = bundle.getString("body");
}
}
Try this.....
Activity1.java
Intent intent = new Intent(getApplication(),Activity2.class);
intent.putExtra("from",from);
intent.putExtra("Body",Body);
StartActivity(intent);
Activity2.java
Intent intent = getintent();
Bundle bundle=intent.getExtras();
String Body=bundle.getString("Body");
String From=bundle.getString("From");
setResult("RESULT_OK",intent);
try with this way..
Bundle bu=getIntent().getExtras();
String title=bu.get("from").toString();
String msg=bu.get("body").toString();
Try this..... to pass value from Activity1 to Activity2.
Intent myIntent = new Intent(Activity1.this, Activity2.class);
myIntent.putExtra("UserId",UserId);
myIntent.putExtra("UserName",UserName);
myIntent.putExtra("CompanyID",CompanyID);
myIntent.putExtra("CompanyName",CompanyName);
myIntent.putExtra("ProjectId",ProjectId);
startActivity(myIntent);
Also can extract the value you can use
Intent intent = getIntent();
UserId=intent.getStringExtra("UserId");
UserName=intent.getStringExtra("UserName");
CompanyID=intent.getStringExtra("CompanyID");
CompanyName=intent.getStringExtra("CompanyName");
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