Nancy
Nancy

Reputation: 1051

Show dialog on RecyclerView OnItemClickListener

I want to show a dialog when an item of the recyclerView is selected each one with different data. I'm not sure how to do this, I've tried the code below but the app crashes and says this error

NullPointerException: Attempt to invoke virtual method 'void android.widget.TextView.setText(java.lang.CharSequence)' on a null object reference

This is the code, I emptied out the actual info because it's pretty long. I know this way isn't ideal but I know the size of the RecyclerView items aren't going to change this is just a demonstration not for actual use.

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

How can I make this work, or make the code better?

Upvotes: 0

Views: 67

Answers (2)

Boken
Boken

Reputation: 5362

If you are using Dialog, you have to find your view not in your Activity, but in your Dialog.

You have to call findViewById() on Dialog, not on Activity.

So change all:

findViewById(...);

in to:

dl.findViewById(...);

Project modification

Change from:

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

to:

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

Full working example:

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

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

Upvotes: 1

jeprubio
jeprubio

Reputation: 18012

Change your:

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

To:

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

To perform the findViewById in the dialog saved in the final Dialog dl var in your code. Otherwise it won't find the elements and you get this null pointers when trying to access them.

Upvotes: 3

Related Questions