Ayhem
Ayhem

Reputation: 247

LayoutInflater in RecyclerView adapter

Im trying to create a popup in recycler view adapter to show a small menu when the user click on an item of the recycler view. This is the popup function:

public void Popup(final int id) {
        AlertDialog.Builder rBuilder = new AlertDialog.Builder(context);
        LayoutInflater i = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        final View rView = i.inflate(R.layout.comment_setting_popup, null);
    rBuilder.setView(rView);
        dialog = rBuilder.create();
        dialog.show();
    }

I call this function in the onBindViewHolder of the adapter. This code work fine in one adapter but in another one it always show me the same error:

E/AndroidRuntime: FATAL EXCEPTION: main
                  Process: com.ayhemprod.jokesbox, PID: 4939
                  android.view.WindowManager$BadTokenException: Unable to add window -- token null is not valid; is your activity running?
                      at android.view.ViewRootImpl.setView(ViewRootImpl.java:765)
                      at android.view.WindowManagerGlobal.addView(WindowManagerGlobal.java:356)
                      at android.view.WindowManagerImpl.addView(WindowManagerImpl.java:93)
                      at android.app.Dialog.show(Dialog.java:330)
                      at com.ayhemprod.jokesbox.Adapters.AddShowCommentsAdapter.signalePopup(AddShowCommentsAdapter.java:295)
                      at com.ayhemprod.jokesbox.Adapters.AddShowCommentsAdapter$1.onClick(AddShowCommentsAdapter.java:71)
                      at android.view.View.performClick(View.java:6294)
                      at android.view.View$PerformClick.run(View.java:24770)
                      at android.os.Handler.handleCallback(Handler.java:790)
                      at android.os.Handler.dispatchMessage(Handler.java:99)
                      at android.os.Looper.loop(Looper.java:164)
                      at android.app.ActivityThread.main(ActivityThread.java:6494)
                      at java.lang.reflect.Method.invoke(Native Method)
                      at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:438)
                      at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:807)

I tried some solution i found here in stackoverflow but nothing helped and i couldnt find the source of the problem especially that the same code work in another adapter in the same project.

UPDATE: this is the code for the onBindViewHolder:

public void onBindViewHolder(AddShowCommentsAdapter.MyViewHolder holder, final int position) {
        Comment c = commentsList.get(position);
holder.comment_menu.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                signalePopup(commentsList.get(position).getId());
            }
        });
}

Im getting the context threw the adapter constructor, when i call the adapter from activity i add the getApplicationContext as a parameter.

public AddShowCommentsAdapter(List<Comment> commentsList, Context context) {
        this.context = context;
        this.commentsList = commentsList;
    }

Upvotes: 2

Views: 2633

Answers (2)

Pawel Laskowski
Pawel Laskowski

Reputation: 6336

In case of AlertDialog you should be using Activity as your Context instead of getApplicationContext().

You can find more detailed information on that here: Why does AlertDialog.Builder(Context context) only accepts Activity as a parameter?

and here: Dialog throwing "Unable to add window — token null is not for an application” with getApplication() as context

Upvotes: 2

Ganesh Tikone
Ganesh Tikone

Reputation: 1057

Instead of writing popup function in Adapter class, Write in Fragment/Activity class where adapter object is attached to recyclerview.

Delegate user click event from Adapter class to respective Activity/Fragment by writing Interface. Call Popup function from callback function of interface

Upvotes: 0

Related Questions