私は簡単なことをしようとしています: 変数をアクティビティ 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);
}
このコードのトーストは、「渡された 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();
このトーストには、「受信した ID: null」と表示されます。これは正しくありません。getInt("accountId") をテストすると、0 が返されます。コードの何が問題なのですか? それは非常に単純なようで、間違いの余地はありませんが、それでも...