دنبال کننده ها

۱۳۹۷ فروردین ۲۱, سه‌شنبه

android - LayoutInflater in RecyclerView adapter

[ad_1]



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.




[ad_2]

لینک منبع