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

代码中出现如下错误:

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();


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章