繁体   English   中英

将价值传递给另一个活动

[英]passing values to another 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。

请帮我。

这是一个样本

从第一个活动

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中,您可以像

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


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

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

在第二个活动中,您可以像

Bundle bundle = getIntent().getExtras();
String empId = bundle.getString("EmpID"); 

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

用这个

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

在第二活动中

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

尝试使用此方法:传递值时:

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

获取值:

System.out.println("Test " + getIntent().getStringExtra("EmpID"));

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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