passing values to another activity - android

I am trying to pass value "0000002" in string format to next activity like below:
Intent pass = new Intent(this, SecondActivity.class);
pass.putExtras("EmpID", "0000002");
In second activity
Bundle info = getIntent().getExtras();
System.out.println("Test " + info.getString("EmpID")); // this line printing "null" value instead of "0000002".
I am able to pass and fetch the other strings successfully. I am not able to fetch the EmpID.
Please help me.

Here is a Sample
From 1st Activity
Bundle localBundle = new Bundle();
localBundle.putString("Loan Amount", editText1.getText().toString());
localBundle.putString("Loan Tenture", editText2.getText().toString());
localBundle.putString("Interest Rate", editText3.getText().toString());
Intent localIntent = new Intent(this, Activity2.class);
localIntent.putExtras(localBundle);
startActivity(localIntent);
and in Activity2
String string1 = getIntent().getStringExtra("Loan Amount");
String string2 = getIntent().getStringExtra("Loan Tenture");
String string3 = getIntent().getStringExtra("Interest Rate");
For your case, you can use like
Bundle localBundle = new Bundle();
localBundle.putString("EmpID", "0000002");
Intent pass = new Intent(this, SecondActivity.class);
pass.putExtras(localBundle);
startActivity(pass);
and in SecondActivity you can get the EmpId like
String empId = getIntent().getStringExtra("EmpID");
----------------- Another Way -----------------
Intent pass = new Intent(this, SecondActivity.class);
pass.putExtra("EmpID", "0000002");
startActivity(pass);
In second activity you can get the EmpId like
Bundle bundle = getIntent().getExtras();
String empId = bundle.getString("EmpID");

Write pass.putExtra("EmpID", "0000002"); not putExtras

use this
Intent pass = new Intent(this, SecondActivity.class);
pass.putExtra("EmpID", "0000002");
startActivity(pass);
In second activity
Bundle info = getIntent().getExtras();
System.out.println("Test " + info.getString("EmpID"));

//Activity A
Intent pass = new Intent(this, SecondActivity.class);
pass.putExtra("EmpID", "0000002");
//Activity B
Intent intent = getIntent();
String EmpID = intent.getStringExtra("EmpID");
System.out.println("Test " + EmpID);

Try Using this:
When passing value:
Intent pass = new Intent(this, SecondActivity.class);
pass.putExtra("EmpID", "0000002");
To get the value:
System.out.println("Test " + getIntent().getStringExtra("EmpID"));

Related

intent getString error

I have created two classes inside other class . inside these two classes i have used the Intent class.
Intent intent = new Intent(getApplicationContext(), DurationsActivity.class );
intent.putExtra("to",mydate);
in parent class i used this code to retrieve the intent value .
String to = getIntent().getExtras().getString("to");
String from = getIntent().getExtras().getString("from");
my logcat retriev this
java.lang.NullPointerException
mydate is like a key to identify the value in intent.
Declare mydate :-
String mydate,mydate1;
Intent intent = new Intent(ActivityName.this, DurationsActivity.class ); //Be Specific
intent.putExtra("to",mydate);
intent.putExtra("from",mydate1);
Than in DurationActivity.class
You can use your usual code in onCreate to get the contents from previous activity:-
String to = getIntent().getExtras().getString("to");
String from = getIntent().getExtras().getString("from");
Try, hope it will work
Intent intent = new Intent(YourCurrentActivity.this, DurationsActivity.class );
intent.putExtra("to",""+mydate);
String to = getIntent().getExtras().getString("to");
String from = getIntent().getExtras().getString("from");

Send data from 1 activity to other activity via intent in android.. i want send SongName to next activity

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

Am unable to get the Data from bundle in my application

In my app, i need to send a Two dimensional Array and two more integer values form on Activity to Another with the help of Intent method.
This is done as ..
Intent i = new Intent(getApplicationContext(), ViewActivity.class);
Bundle postbundle = new Bundle();
String[][] X={{"abc"},{"def"}};
postbundle.putSerializable("data", X);
i.putExtra("A", postbundle);
i.putExtra("albumid", position);
i.putExtra("Bigcard",bigcard);
here am using .putSerializable method to place an array into bundle.
So to access these data in the receiver Activity am using
Bundle bundle = getIntent().getBundleExtra("A");
String[][] ABC=(String[][]) bundle.getSerializable("data");
Log.e("Array is",""+ABC);
but I got java.lang.NullPointerException error message..
Whith out use of " Static " declaration how can i get these values from bundle here (in the receiver Activity..)
Let me out pls from this ..
step-1:Write a seperate bean class and save into another file
public class MyBean implements Serializable{
String[][] data = null;
public void set2DArray(String[][] data){
this.data = data;
}
public String[][] get2DArray(){
return data;
}
}
step-2:In the calling activity
Intent intent = new Intent(this, Second.class);
String data[][] = new String[][] {{"1","kumar"},{"2","sona"},{"3","kora"},{"1","pavan"},{"2","kumar"},{"3","kora333"}};
MyBean bean = new MyBean();
bean.set2DArray(data);
Bundle b = new Bundle();
b.putSerializable("mybean", bean);
intent.putExtra("obj", b);
startActivity(intent);
step-3:In the caller activity
Bundle b = getIntent().getBundleExtra("obj");
MyBean dData = (MyBean) b.getSerializable("mybean");
String[][] str =dData.get2DArray();
Not a real answer, but a try:
what happens if You try:
Intent intent = getIntent();
int a = intent.getIntExtra("albumid"); //if Your value is an int, otherwise use String
//getStringExtra or whatever Your value is

passing intent between for two string arrays

I have two string Arrays, contactNames in the main Activity class, and address [] in the second activity class. when i click on the one of the names, it should show me the address details for that name. How should I create an intent for two string arrays in Android.
class ContactList
Intent i = new Intent (MainActivity.this, Details.class);
Bundle bundle = new Bundle ();
bundle.putStringArray("key", address);
i.putExtras(bundle);
startActivity(i);
}
class Details
Bundle b = this.getIntent().getExtras();
String array [] = b.getStringArray("key");
tvEmail.setText(array.toString());
}
thanks.
How should I create an intent for two string arrays in Android.
You can add another array (or any other data type) but you need to use a different key name. Example...
String[] array1;
String[] array2;
// Populate arrays here
Intent i = new Intent (MainActivity.this, Details.class);
Bundle bundle = new Bundle ();
bundle.putStringArray("key1", array1);
bundle.putStringArray("key2", array2);
i.putExtras(bundle);
startActivity(i);
Also note that you don't need to use a Bundle directly. You can use the Intent method putExtra(String name, String[] value). For example...
String[] array1;
String[] array2;
// Populate arrays here
Intent i = new Intent (MainActivity.this, Details.class);
i.putExtra("key1", array1);
i.putExtra("key2", array2);
startActivity(i);

passing 2d array to a new activity in android

How to pass a 2d String array to new activity???
and then in new activity, How can i retrieve the array??
You have to use:
String [][]str;
Intent summaryIntent = new Intent(this, Second.class);
Bundle b=new Bundle();
b.putSerializable("Array", str);
summaryIntent.putExtras(b);
startActivity(summaryIntent);
For Recieving the Array use:
Bundle b = getIntent().getExtras();
String[][] list_array = (String[][])b.getSerializable("Array");
Thanks

Categories

Resources