java - 活动的简单对话框错误 - android.view.WindowManager$BadTokenException
问题描述
我的主要活动调用了第二个活动。这第二个活动调用了一个 viewholder java 类,从中调用了一个警报对话框:
Log.w(LOG_TAG, "call the alertdialog");
showAlertDialog();
public void showAlertDialog() {
/** define onClickListener for dialog */
DialogInterface.OnClickListener listener
= new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
// do some stuff eg: context.onCreate(super)
}
};
/** create builder for dialog */
AlertDialog.Builder builder = new AlertDialog.Builder(NFLActivity.context)
.setCancelable(false)
.setMessage("You have made all bets. Select OK to submit...")
.setTitle("All Bets Are In")
.setNegativeButton("Return", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialogInterface, int i) {
应用程序因此崩溃:06-29 10:03:31.340 3740-3740/com.bab_c.picksandbets E/AndroidRuntime: 致命异常:主进程:com.bab_c.picksandbets,PID:3740 android.view.WindowManager$BadTokenException:无法添加窗口——token null 无效;您的活动正在运行吗?
我研究发现对话框需要一个活动,但是当使用第二个活动showAlertDialog(SecondActivity.context)时它仍然失败。
解决方案
首先,使用 viewHolder 类来保存视图并使用 viewHolder 的视图从第二个 Activity 调用警报。
第二件事,不要将上下文设为静态
推荐阅读
- google-chrome-extension - chrome 扩展程序可以截取它自己的弹出窗口吗?
- makefile - 在linux内核源码中,为什么arch/arm64只有一个defconfig文件?(对于 arch/arm 有很多 xxx_defconfig 文件)
- python - 高效处理 pandas 组
- php - PHP会话读取错误值
- python - 我的文本游戏 python 脚本无法正确执行 if/else 函数
- python - 传递 df 时带有 OneHotEncoder 问题的列转换器
- reactjs - 如何使 Material UI AutoComplete 组件看起来像 Material UI 网站上的组件
- node.js - Axios 发布请求提供参考错误
- javascript - 我应该如何删除 d3-force 中的节点?
- php - 如何使用 PHP 脚本模拟确切的浏览器请求?