主な活動:
Intent intent = new Intent(Main.this, Secondary.class);
intent.putExtra("name",value);
startActivity(intent);
二次活動:
String value = getIntent().getStringExtra("name")
ここで何が問題なのですか?私は成功せずにたくさん検索しました...
ありがとう
これを試して:
MainActivity で:
//Make sure Secondary is an Activity name. Secondary.class.
Intent intent = new Intent(MainActivity.this, Secondary.class);
intent.putExtra("name",value);
startActivity(intent);
二次活動では:
String value = getIntent().getExtras().getString("name");
最初にバンドルを取得してから、そこから文字列を抽出する必要があります。
Bundle bundle = getIntent().getExtras();
if (bundle != null) {
bundle.getString("name");
}
どちらも機能するはずです。2 つ目は、バンドルが null かどうかを確認することです。
私はこの方法を何度も使用しました。値に値があるか初期化されていることを確認してください。Log または System.out.println(value); を使用できます 。.putExtraの後、値が null かどうかを (コンソール タブで) 確認します。そして2番目の活動でも。
この行を変更
String value = getIntent().getStringExtra("name");
この行へ
String value = getIntent().getString("name");
を呼び出すときはputExtra(...)
、value
オブジェクトがString
. value.toString()
他のオブジェクトを渡す場合、特に GUI コンポーネントを扱う場合は、必ず明示的に を呼び出してください。
詳細については、こちらを参照してください: Android Intent.getStringExtra() returns null
あなたはこれを試すことができます -->
intent.putExtra("name", textView.getText().toString());
それでもエラーが発生する場合は、正しい ID パスを使用している 2 番目のアクティビティをチェックインしてください...:-
value = findViewById(R.id.);