android.view.WindowManager$BadTokenException: Unable to add window -- token null is not for an appli
来源:互联网 发布:insert into多条数据 编辑:程序博客网 时间:2024/05/17 07:20
昨天纠结了我一天的问题,今天终于有点眉目了,看来做这一行休息是必要的啊。。。今天记录一下似乎这类型的错误已经犯了不少,真是不应该啊。。
这几天在做一个客户端,公司产品的管理从服务器上移置到Android上面来。在开发的时候有这个一个错误一直找不出来:
android.view.WindowManager$BadTokenException: Unable to add window -- token null is not for an appli
这是我在做用TabHost做应用的时候,在子标签里面进行Activty之间的跳转的时候犯的一个错误。大家都知道,在TabHost的子标签里面要很好的实现Activity跳转,而不覆盖TabHost我们需要用到ActivityGroup。即我们的主Activity需要用到ActivityGroup,在跳转的时候,我们就可以用下面这段代码。
// 传递当前点击的轮询信息到下一个ActivityPollingRequestvo pollingRequestvo = listAllvo.get(adapter.position);Intent intent = new Intent(PollingManagementActivity.this,PollingInforActivity.class);// intent.setFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP);Bundle bundle = new Bundle();bundle.putSerializable("pollingRequestvo", pollingRequestvo);intent.putExtras(bundle);intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);Holder.viewSwitcher.removeAllViews();View view = getLocalActivityManager().startActivity("polling",intent).getDecorView();Holder.viewSwitcher.addView(view);
当然本类PollingManagementActivity是继承了MyTabActivity类,MyTabActivity类也继承了ActivityGroup类并且覆盖了ActivityGroup类的onBackPress方法,如下:
@Overridepublic void onBackPressed() {new AlertDialog.Builder(this).setTitle("注意").setMessage("确定要退出手机网管吗?").setIcon(android.R.drawable.ic_menu_help).setPositiveButton("确定", new DialogInterface.OnClickListener() {public void onClick(DialogInterface dialog, int which) {dialog.dismiss();finish();}}).setNegativeButton("取消", new DialogInterface.OnClickListener() {public void onClick(DialogInterface dialog, int which) {dialog.cancel();}}).show();}
这样覆盖的注意是为了更好的管理Activity ,只要是继承了MyTabActivity的类,这个方法都有用,问题出现了,我们看上面的给出的Activity跳转代码,当我们跳转到PollingInforActivyt的时候,直接点击“返回”键 是可以下面运行父类的onBackPress方法的。但是如果点击其它的页签,再点击这个页签问题就出来了,提示:
android.view.WindowManager$BadTokenException: Unable to add window -- token null is not for an appli错误。
在网上找了许多,问题终于找到了。这是因为我们在继承ActivityGroup,覆盖onBackPress方法的时候,用到new AlertDialog.Builder(this)导致出的错。
这是为什么呢。原因是我们跳转到目标Activity的之后,如果这个Activyt还是继承MyTabActivyt的话,new AlertDialog.Builder(this)中这个this也就代表了当前的Activyt,即PollingInforActivyt,但是我们看到,在跳转Activyt的时候,我们的Activity是以View形式贴到Holder.viewSwitcher的,不能自己再添加view(对话框也是由Activyt添加的一个View),必须用PollingManagementActivity,代替this,我们只需要在PollingInforActivity里,用getParent代替this就可以了,这样我们实际这个对话框是由PollingManagementActivity添加的。。。。。
- android.view.WindowManager$BadTokenException: Unable to add window -- token null is not for an appli
- android.view.WindowManager$BadTokenException: Unable to add window -- token null is not for an appli
- android.view.WindowManager$BadTokenException: Unable to add window -- token null is not for an appli
- android.view.WindowManager$BadTokenException: Unable to add window -- token null is not for an appli
- android.view.WindowManager$BadTokenException: Unable to add window -- token null is not for an appli
- android.view.WindowManager$BadTokenException: Unable to add window -- token null is not for an appli
- android.view.WindowManager$BadTokenException: Unable to add window -- token null is not for an appli
- android.view.WindowManager$BadTokenException: Unable to add window -- token null is not for an appli
- android.view.WindowManager$BadTokenException: Unable to add window -- token null is not for an appli
- android.view.WindowManager$BadTokenException: Unable to add window — token null is not for an appli
- android.view.WindowManager$BadTokenException: Unable to add window -- token null is not for an appli
- android.view.WindowManager$BadTokenException: Unable to add window -- token null is not for an appli
- android.view.WindowManager$BadTokenException: Unable to add window -- token null is not for an appli
- android.view.WindowManager$BadTokenException: Unable to add window -- token null is not for an appli
- android.view.WindowManager$BadTokenException: Unable to add window -- token null is not for an appli
- android.view.WindowManager$BadTokenException: Unable to add window -- token null is not for an appli
- android.view.WindowManager$BadTokenException: Unable to add window -- token null is not for an appli
- android.view.WindowManager$BadTokenException: Unable to add window -- token null is not for an appli
- string::find 与 string::npos
- 换分币:用一元人民币兑换成1 分、2 分和5 分硬币,共有多少种不同的兑换方法
- PHP框架开发一(首言)
- android去掉系统状态栏(StatusBar),让所有app程序均是全屏显示
- 有等式[※×(※3+※)]2=8※※9,其中※处为1 个数字,滴上了墨水无法辨认。请编程找出※表示的数字
- android.view.WindowManager$BadTokenException: Unable to add window -- token null is not for an appli
- 来表达我说过如果有那么一天
- 【Android】获取手机中已安装apk文件信息(PackageInfo、ResolveInfo)(应用图片、应用名、包名等)
- 生成 excel知识点
- TextBox弹出的输入键盘的设置及限制输入功能
- California Mathmetics-G6-Ch3
- ASP.NET将Session保存到数据库中
- maven 配置文件说明
- 实用SQL语句大全(上)