android - Unable to add window -- token null is not for an application的解决方案

Error代码:

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

Error描述,使用AlertDialog.Builder报错:

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) 

解决方案:需要当前activity的context

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




作者:Susie 
出处:https://www.cnblogs.com/zwting/p/9952752.html   
欢迎转载,但还请尊重劳动果实,保留此段声明并注明原文链接。

 

转载于:https://www.cnblogs.com/zwting/p/9952752.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值