簡體   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