passing values to another activity

2019-06-05 21:02发布

问题:

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.

回答1:

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


回答2:

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



回答3:

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


回答4:

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


回答5:

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