Android:BadTokenException: Unable to add window

Android:创建alertDialog报错(错误信息):

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

            at android.view.ViewRootImpl.setView(ViewRootImpl.java:525)

            at android.view.WindowManagerImpl.addView(WindowManagerImpl.java:407)

            at android.view.WindowManagerImpl.addView(WindowManagerImpl.java:321)

            at android.view.WindowManagerImpl$CompatModeWrapper.addView(WindowManagerImpl.java:152)

            at android.app.Dialog.show(Dialog.java:301)

报错原因:创建dialog时传的参数有误,下面的写法会报错

AlertDialog.Builder builder = new AlertDialog.Builder(getActivity().getApplicationContext());

参数传Context是有误的,应该传Activity  在context中不能增添窗体

正确的写法应该是:

AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());

你可能感兴趣的:(TO,android,window,add,Unable)