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

标签 java android

我正在尝试使用此代码从 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/

相关文章:

android - onPause 之后 AsyncTasks 会发生什么?

android - Xamarin.Android 在没有互联网的情况下崩溃

android - 如何使用java代码在sp值中分配文本大小

Android OpenGL ES 通过 NDK : placement of jpg decompression and binding to texture

java - 用泛型实现父子关系

java - 正则表达式未提取确切的模式

java - Spring Cloud 中的水平扩展

Android:特殊的短信发送

java - 如何在警报对话框中强制输入单行?

java - 在运行时评估一系列 boolean 条件是否有效