我正在尝试做一件简单的事情:将变量从活动 A 传递到活动 B。我在活动 A 中有这个方法:
private void editAccount(CloudAccount account) {
Intent intent = new Intent(this, EditAccountActivity.class);
intent.putExtra("accountId", account.getId());
this.showToast("ID passed: " + account.getId());
startActivity(intent);
}
此代码中的 Toast 显示“ID 已通过:1”。这是对的。现在,我开始活动 B:
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Bundle extras = getIntent().getExtras();
Toast.makeText(this, "ID received: " + extras.getString("accountId"), Toast.LENGTH_LONG).show();
此 toast 显示“收到的 ID:null”。这是不正确的。如果我测试 getInt("accountId") - 它返回 0。我的代码有什么问题?看起来很简单,没有出错的地方,但还是……