如何在Android中使用Intent在Activity之间传递值?

15

我想将一个活动类中的位置值传递到另一个活动类中...

我的代码如下:

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);
    }
但我总是得到一个空值字符串...
请帮忙。

protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_bucket_item); Bundle bundle = getIntent().getExtras(); String value = bundle.getString("bucketno"); TextView textView = (TextView) findViewById(R.layout.textView1); textView.setTextSize(40); textView.setText(value); } - User
7个回答

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

您正在尝试传递int值,但检索的是String数据。这就是为什么您会收到nullpointerException的原因。


1
应该是 int value 或者 String value = String.valueOf(...);。你正在尝试使用这段代码将 int 存储在 String 中。 ;) - Cat
@Andro Selva,你说得对,但他应该会得到ParseException或ClassCastException异常,但他却得到了NullPointerException异常? - Android Killer
还有一件事我之前没有考虑到——在这种情况下,setText(value) 会失败于 int。使用 String.valueOf() 是更好的选择。 - Cat
你可以使用Phil的解决方案,如果需要它本身是字符串。两种方法都可以很好地工作。 - Andro Selva

15
你可以使用:

在第一个活动(MainActivity页面)中:

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

然后你可以通过以下方式从第二个活动中获取它:


在第二个活动中(SecondActivity页面)
Intent intent = getIntent();
String YourtransferredData = intent.getExtras().getString("YourValueKey");

4
String value = Integer.toString(getIntent().getExtras().getInt("bucketno"));

2
Bundle extras = getIntent().getExtras();

if (extras != null) { 
    String data = intent.getExtras().getString("key").toString();
}

1
从第一个活动开始。
Intent i=new Intent(getApplicationContext(),BookPractionerAppoinment.class);
i.putExtra("prac","test");
startActivity(i);

获取第二个 ACT 活动中的值。
String prac=getIntent().getStringExtra("prac);

对于序列化对象:

传递

Intent i=new Intent(getApplicationContext(),BookPractionerAppoinment.class);
 i.putExtra("prac",pract);
 startActivity(i);

获取(Getting)
pract= (Payload) getIntent().getSerializableExtra("prac");

0

此外,有不同情况的人

  double userDMH = Util.getInstance(TakeInfoOne.this).calculateBMH(userGender, kg, talll, currentAge, activityy);
                        Intent intentTogeneral = new Intent(TakeInfoOne.this, TakeInfoTwo.class);
                        intentTogeneral.putExtra("user_current_age",userDMH );
                        startActivity(intentTogeneral);

如果您在其他基本类型中使用了double、boolean或int,请在获取第二个Activity中的值时不要忘记输入正确的格式。

Bundle extras = getIntent().getExtras();
    if (extras != null) {
        double value =  extras.getDouble("user_current_age");
        txt_metabolism.setText(""+value);
    }

在此处使用extras.getDouble(),将您关心的值类型填入。
extras.getInt("user_current_age");
extras.getBoolean("user_current_age");
 extras.getString("user_current_age");

0

使用:

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

getIntent().getExtras()会给你一个Bundle。可以使用get()方法来获取值。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接