在RecyclerView OnItemClickListener上显示对话框

问题描述 投票:0回答:2

我想显示一个对话框,当选择每个带有不同数据的recyclerView项时。我不确定如何执行此操作,我尝试了以下代码,但应用程序崩溃并提示此错误

NullPointerException:尝试在空对象引用上调用虚拟方法'void android.widget.TextView.setText(java.lang.CharSequence)'

这是代码,因为它很长,所以我清空了实际信息。我知道这种方式并不理想,但我知道RecyclerView项的大小不会改变,这只是一个演示,并不实际使用。

public void showDialog(Activity activity, int position) {
        final Dialog dl = new Dialog(activity);
        dl.requestWindowFeature(Window.FEATURE_NO_TITLE);
        dl.setCancelable(true);
        dl.setContentView(R.layout.dialog);
        dl.getWindow().setBackgroundDrawable(new ColorDrawable(android.graphics.Color.TRANSPARENT));

        TextView title = findViewById(R.id.title);
        ImageView imageView = findViewById(R.id.imageView);
        TextView short_desc = findViewById(R.id.short_desc);
        TextView long_desc = findViewById(R.id.long_desc);

        switch (position) {
            case 0:
                title.setText("");
                Picasso.get().load("").into(imageView);
                short_desc.setText("");
                long_desc.setText("");
                break;
            case 1:
                title.setText("");
                Picasso.get().load("").into(imageView);
                short_desc.setText("");
                long_desc.setText("");
                break;
            case 2:
                title.setText("");
                Picasso.get().load(").into(imageView);
                short_desc.setText("");
                long_desc.setText("");
                break;
            case 3:
                title.setText("");
                Picasso.get().load("").into(imageView);
                short_desc.setText("");
                long_desc.setText("");
                break;
            case 4:
                title.setText("");
                Picasso.get().load("").into(imageView);
                short_desc.setText("");
                long_desc.setText("");
                break;
            case 5:
                title.setText("");
                Picasso.get().load("").into(imageView);
                short_desc.setText("");
                long_desc.setText("");
                break;
            case 6:
                title.setText("");
                Picasso.get().load("").into(imageView);
                short_desc.setText("");
                long_desc.setText("");
                break;
            default:
                title.setText("title");
                Picasso.get().load("").into(imageView);
                short_desc.setText("short desc.");
                long_desc.setText("long desc.");
        }
        dl.show();
    }

如何使这项工作有效,或使代码更好?

android
2个回答
1
投票

更改您的:

TextView title = findViewById(R.id.title);
ImageView imageView = findViewById(R.id.imageView);
TextView short_desc = findViewById(R.id.short_desc);
TextView long_desc = findViewById(R.id.long_desc);

收件人:

TextView title = dl.findViewById(R.id.title);
ImageView imageView = dl.findViewById(R.id.imageView);
TextView short_desc = dl.findViewById(R.id.short_desc);
TextView long_desc = dl.findViewById(R.id.long_desc);

在代码中保存在findViewById变量中的对话框中执行final Dialog dl。否则,它将找不到元素,并且在尝试访问它们时会得到此空指针。


0
投票

如果使用Dialog,则必须不在Activity中找到视图,而要在Dialog中找到视图。

所以您必须在对话框上调用findViewById(),而不是在Activity上。

所以全部更改:

findViewById(...);

进入:

dl.findViewById(...);
© www.soinside.com 2019 - 2024. All rights reserved.