[英]passing values to another activity
I am trying to pass value "0000002" in string format to next activity like below: 我正在尝试将字符串格式的值“ 0000002”传递给下一个活动,如下所示:
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. 我无法获取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 在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 在SecondActivity中,您可以像
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");
写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"));
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.