1

主な活動:

Intent intent = new Intent(Main.this, Secondary.class);

intent.putExtra("name",value);

startActivity(intent);

二次活動:

String value = getIntent().getStringExtra("name")

ここで何が問題なのですか?私は成功せずにたくさん検索しました...

ありがとう

4

6 に答える 6

3

これを試して:

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 かどうかを確認することです。

于 2013-07-28T20:08:08.557 に答える
0

私はこの方法を何度も使用しました。値に値があるか初期化されていることを確認してください。Log または System.out.println(value); を使用できます .putExtraの後、値が null かどうかを (コンソール タブで) 確認します。そして2番目の活動でも。

于 2013-07-28T20:08:51.510 に答える
0

この行を変更

String value = getIntent().getStringExtra("name");

この行へ

String value = getIntent().getString("name");
于 2013-07-28T20:08:56.563 に答える
0

を呼び出すときはputExtra(...)valueオブジェクトがString. value.toString()他のオブジェクトを渡す場合、特に GUI コンポーネントを扱う場合は、必ず明示的に を呼び出してください。

詳細については、こちらを参照してください: Android Intent.getStringExtra() returns null

于 2013-07-28T19:50:00.493 に答える
0

あなたはこれを試すことができます -->

intent.putExtra("name", textView.getText().toString());

それでもエラーが発生する場合は、正しい ID パスを使用している 2 番目のアクティビティをチェックインしてください...:-

value = findViewById(R.id.);
于 2021-10-16T20:39:52.960 に答える