【发布时间】:2021-10-01 06:16:03
【问题描述】:
我不知道为什么下面的代码不适用于 Activity RecycleView Adapter 类,但我使用 Fragment RecycleView Adapter 类和绝对的工作做到了。有人可以帮忙吗?
viewHolder.rlCV.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
LayoutInflater inflater = LayoutInflater.from(mContext);
final View vv = inflater.inflate(R.layout.popup_pdf, null);
final AlertDialog.Builder alert = new AlertDialog.Builder(
mContext);
alert.setView(vv);
dialog = alert.create();
dialog.show();
ImageButton btnCancel=vv.findViewById(R.id.btnCancel12);
pdfView=vv.findViewById(R.id.pdfView);
//new RetrivePdfStremQbank().execute(cvUri);
btnCancel.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
dialog.dismiss();
}
});
}
});
运行时显示错误消息:
android.view.WindowManager$BadTokenException: 无法添加窗口 -- 令牌 null 无效;您的活动正在运行吗? 在 android.view.ViewRootImpl.setView(ViewRootImpl.java:1003) 在 android.view.WindowManagerGlobal.addView(WindowManagerGlobal.java:384) 在 android.view.WindowManagerImpl.addView(WindowManagerImpl.java:101) 在 android.app.Dialog.show(Dialog.java:430) 在 com.rpaskewdevelopment.suap.Adapter.Comment_info_Adapter$2.onClick(Comment_info_Adapter.java:127)
【问题讨论】:
-
mContext不是Activity,这可能意味着Adapter在其构造函数调用中被赋予了错误的Context。 -
私有上下文 mContext;是的。我定义的上下文是适配器上的 mContext
-
在
Activity中,当你实例化Adapter时,你传递了错误的Context。例如,您可能有类似MyAdapter adapter = new MyAdapter(getApplicationContext());的内容。那是错误的Context。您需要传递当前的Activity;例如,new MyAdapter(this)或new MyAdapter(MyActivity.this),视需要而定。 -
向适配器传递一个arrayList 和一个字符串。请检查代码:comment_info_adapter =new Comment_info_Adapter(getApplicationContext(), commentModels,file); recyclerView.setAdapter(comment_info_adapter);
-
哦。人。我搞砸了。我明白了,你想修正什么。现在没事了。非常感谢和赞赏:)。
标签: java android android-alertdialog