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


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