android Unable to add window -- token null is not for an application

来源:互联网 发布:计算机二级java考试 编辑:程序博客网 时间:2024/06/06 02:02

代码中出现如下错误:

android.view.WindowManager$BadTokenException:Unable to add window -- token null is not for anapplication

分析:

问题在于newAlertDialog.Builder(Context),虽然这里的参数是AlertDialog.Builder(Contextcontext)

但我们不能使用getApplicationContext()获得的Context,而必须使用Activity的Context对象,因为只有一个Activity才能添加一个窗体。  

解决方法:

将new AlertDialog.Builder(Contextcontext)中的参数用Activity的Context对象即可,但我写Dialog的地方是fragement,起初我用的是getActivity();但是还是有这个问题,不是毕现的,最后改为getContext();


0 0
原创粉丝点击