13

あるアクティビティ クラスの位置の値を別のアクティビティ クラスに渡したい...

私のコードは次のとおりです。

protected void onListItemClick(ListView listView, View v, int position,
            long id) {
        switch (position) {
            case 1:
                Intent newActivity1 = new Intent(this, BucketItemActivity.class);
                newActivity1.putExtra("bucketno", position);
                startActivity(newActivity1);
                break;
            case 2:
                Intent newActivity2 = new Intent(this, BucketItemActivity.class);
                newActivity2.putExtra("bucketno", position);
                startActivity(newActivity2);
                break;
    }
}

それを受け取る活動クラス..

protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_bucket_item);
        String value = getIntent().getExtras().getString("bucketno");
        TextView textView = new TextView(this);
        textView.setTextSize(40);
        textView.setText(value);
        setContentView(textView);
    }

しかし、私は常に文字列値にヌルを取得します...

助けてください。

4

7 に答える 7

22

これを交換して、

String value = getIntent().getExtras().getString("bucketno");

int value = getIntent().getExtras().getInt("bucketno");

int 値を渡そうとしていますが、文字列データを取得しています。それが nullpointerException を取得している理由です。

于 2012-12-24T06:14:11.190 に答える
14

使用できます:

最初のアクティビティ ( MainActivity ページ )

Intent i = new Intent(MainActivity.this,SecondActivity.class); 
i.putExtra("YourValueKey", yourData.getText().toString());

次に、次の方法で2番目のアクティビティから取得できます。2
番目のアクティビティ(SecondActivityページ)

Intent intent = getIntent();
String YourtransferredData = intent.getExtras().getString("YourValueKey");
于 2012-12-24T06:14:43.890 に答える
0

使用する:

String value = getIntent().getExtras().get("key").toString();

getIntent().getExtras()バウンドルをくれます。get()メソッドを使用して値を取得できます。

于 2013-11-09T16:22:33.903 に答える