如何在Android中的Activity之间使用Intent传递值? [英] How to pass value using Intent between Activity in Android?

查看:372
本文介绍了如何在Android中的Activity之间使用Intent传递值?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I want to pass the value of the position in one activity class to another...

我的代码如下:

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;
    }
}

将接收该活动的活动类.

The activity class that will receive it..

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);
    }

但是我总是在字符串值中得到一个空值...

But i always get a null in the String value...

请帮助.

推荐答案

替换此内容

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

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

您正在尝试传递int值,但正在检索字符串数据.这就是为什么您会得到nullpointerException的原因.

You are trying to pass int value but retrieving String Data. That's why you are getting the nullpointerException.

这篇关于如何在Android中的Activity之间使用Intent传递值?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆