gpt4 book ai didi

java - 警告对话框无法添加窗口错误

转载 作者:行者123 更新时间:2023-11-29 19:53:22 24 4
gpt4 key购买 nike

我正在尝试使用此代码从 RecyclerView.Adapter 创建 AlertDialog

AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(new ContextThemeWrapper(context, R.style.AppTheme));
alertDialogBuilder.setView(R.layout.reserve_dialog);
alertDialogBuilder.create();
alertDialogBuilder.show();

但我在 logcat 中收到此错误:

Theme: themes:{}
android.view.WindowManager$BadTokenException: Unable to add window -- token null is not for an application

怎么了?

最佳答案

you are passing context.getApplicationContext()

代替这个传递 Activity 上下文

    AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(new ContextThemeWrapper(MainActivity.this, R.style.AppTheme));
alertDialogBuilder.setView(R.layout.reserve_dialog);
alertDialogBuilder.create();
alertDialogBuilder.show();

关于java - 警告对话框无法添加窗口错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36914993/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com