1

我正在尝试将字符串格式的值“0000002”传递给下一个活动,如下所示:

Intent pass = new Intent(this, SecondActivity.class);
pass.putExtras("EmpID", "0000002");

在第二个活动

Bundle info = getIntent().getExtras();
System.out.println("Test " + info.getString("EmpID")); // this line printing "null" value instead of "0000002". 

我能够成功传递和获取其他字符串。我无法获取 EmpID。

请帮我。

4

5 回答 5

7

这是一个示例

从第一个活动

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

并在 Activity2

String string1 = getIntent().getStringExtra("Loan Amount");
String string2 = getIntent().getStringExtra("Loan Tenture");
String string3 = getIntent().getStringExtra("Interest Rate");

对于您的情况,您可以使用 like

Bundle localBundle = new Bundle();
localBundle.putString("EmpID", "0000002");
Intent pass = new Intent(this, SecondActivity.class);
pass.putExtras(localBundle);
startActivity(pass);

在 SecondActivity 你可以得到 EmpId

String empId = getIntent().getStringExtra("EmpID");


-----------------另一种方式-----------------

Intent pass = new Intent(this, SecondActivity.class);
pass.putExtra("EmpID", "0000002");
startActivity(pass);

在第二个活动中,您可以获得 EmpId 之类的

Bundle bundle = getIntent().getExtras();
String empId = bundle.getString("EmpID"); 
于 2013-09-27T12:12:32.643 回答
1

pass.putExtra("EmpID", "0000002");不放Extras

于 2013-09-27T12:14:13.740 回答
1

用这个

Intent pass = new Intent(this, SecondActivity.class);
pass.putExtra("EmpID", "0000002");
startActivity(pass);

在第二个活动

Bundle info = getIntent().getExtras();
System.out.println("Test " + info.getString("EmpID")); 
于 2013-09-27T12:16:39.987 回答
0
//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);
于 2013-09-27T12:08:07.240 回答
0

尝试使用这个:传递值时:

Intent pass = new Intent(this, SecondActivity.class);
pass.putExtra("EmpID", "0000002");

要获取值:

System.out.println("Test " + getIntent().getStringExtra("EmpID"));
于 2013-09-28T08:51:06.713 回答