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

标签 android

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

我的代码如下:

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

将接收它的 Activity 类..

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

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

请帮忙。

最佳答案

替换这个,

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

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

您正在尝试传递 int 值但检索字符串数据。这就是您收到 nullpointerException 的原因。

关于android - 如何在 Android 中使用 Intent 在 Activity 之间传递值?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14017720/

相关文章:

android - 如何知道 Retrofit 调用何时完成

java - 如何将 .dex 文件转换为 android studio 项目

android - Proguard 混淆导致 dex 编译抛出异常

java - 如何在 Android 中的联系人数据库上设置触发器

java - Android:如何设置 MenuButton 的监听器?

java - 将堆栈大小设置为线程似乎在android中没有什么区别

java - Android 应用程序说该文件不存在,但确实存在。我需要安装它

java - 如何删除使用 addContentView 添加的 View ?

android - cardBackgroundColor 和 cardCornerRadius 在 AndroidX 中不起作用

android - kivy:水平ListView