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

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


怎么了?

最佳答案

您正在传递context.getApplicationContext()


代替此通过活动上下文

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

08-04 06:46