Tag Archives: Unable to add window error

Android – Unable to add window — token null is not for an application [How to Solve]

Error code:

new AlertDialog.Builder(getActivity().getApplicationContext())
        .setTitle("No phone access permission")
        ******
        .show();

Error description, use AlertDialog.Builder to report an error:

2018-11-13 11:48:32.264 1062-1062/air.app E/AndroidRuntime: FATAL EXCEPTION: main
Process: air.app, PID: 1062
java.lang.RuntimeException: Failure delivering result ResultInfo{who=@android:requestPermissions:, request=65636, result=-1, data=Intent { act=android.content.pm.action.REQUEST_PERMISSIONS (has extras) }} to activity {*.activity.BaseActivity}: android.view.WindowManager$BadTokenException: Unable to add window — token null is not for an application
at android.app.ActivityThread.deliverResults(ActivityThread.java:4089)
at android.app.ActivityThread.handleSendResult(ActivityThread.java:4132)
at android.app.ActivityThread.-wrap20(ActivityThread.java)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1533)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:154)
at android.app.ActivityThread.main(ActivityThread.java:6119)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:886)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:776)
Caused by: android.view.WindowManager$BadTokenException: Unable to add window — token null is not for an application
at android.view.ViewRootImpl.setView(ViewRootImpl.java:683)
at android.view.WindowManagerGlobal.addView(WindowManagerGlobal.java:342)
at android.view.WindowManagerImpl.addView(WindowManagerImpl.java:93)
at android.app.Dialog.show(Dialog.java:322)
at android.app.AlertDialog$Builder.show(AlertDialog.java:1112)
at android.support.v4.app.FragmentActivity.onRequestPermissionsResult(FragmentActivity.java:911)
at android.app.Activity.dispatchRequestPermissionsResult(Activity.java:7084)
at android.app.Activity.dispatchActivityResult(Activity.java:6936)
at android.app.ActivityThread.deliverResults(ActivityThread.java:4085)
at android.app.ActivityThread.handleSendResult(ActivityThread.java:4132)
at android.app.ActivityThread.-wrap20(ActivityThread.java)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1533)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:154)
at android.app.ActivityThread.main(ActivityThread.java:6119)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:886)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:776)

Solution: need the context of the current activity

new AlertDialog.Builder(ConfirmFragment.this.getContext())
        .setTitle("No phone access permission")
        ******
        .show();