简体   繁体   中英

passing values to another activity

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"));

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM