将值传递给另一个活动

时间:2013-09-27 12:05:09

标签: android android-intent android-activity

我正在尝试将字符串格式的值“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。

请帮帮我。

5 个答案:

答案 0 :(得分:7)

这是一个示例

从第1次活动开始

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

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

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

答案 1 :(得分:1)

pass.putExtra("EmpID", "0000002");而不是putExtras

答案 2 :(得分: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")); 

答案 3 :(得分: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);

答案 4 :(得分:0)

尝试使用此: 传递价值时:

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

获取值:

System.out.println("Test " + getIntent().getStringExtra("EmpID"));
相关问题