安卓系统中Toast报错无法弹出

在Android应用开发中,遇到一个错误,尝试从非视觉Context(com.android.bluetooth.btservice.AdapterApp)访问WindowManager服务导致IllegalAccessException。解决方案是使用Context.createWindowContext()创建一个新的上下文来显示Toast。

报如下错

03-31 15:27:06.221 18797 18797 E ContextImpl: Tried to access visual service WindowManager from a non-visual Context:com.android.bluetooth.btservice.AdapterApp@69e9f3d Visual services, such as WindowManager, WallpaperService or LayoutInflater should be accessed from Activity or other visual Context. Use an Activity or a Context created with Context#createWindowContext(int, Bundle), which are adjusted to the configuration and visual bounds of an area on screen.
03-31 15:27:06.221 18797 18797 E ContextImpl: java.lang.IllegalAccessException: Tried to access visual service WindowManager from a non-visual Context:com.android.bluetooth.btservice.AdapterApp@69e9f3d
03-31 15:27:06.221 18797 18797 E ContextImpl:   at android.app.ContextImpl.getSystemService(ContextImpl.java:1914)
03-31 15:27:06.221 18797 18797 E ContextImpl:   at android.content.ContextWrapper.getSystemService(ContextWrapper.java:803)
03-31 15:27:06.221 18797 18797 E ContextImpl:   at android.content.ContextWrapper.getSystemService(ContextWrapper.java:803)
03-31 15:27:06.221 18797 18797 E ContextImpl:   at android.content.Context.getSystemService(Context.java:3705)
03-31 15:27:06.221 18797 18797 E ContextImpl:   at android.widget.ToastPresenter.<init>(ToastPresenter.java:81)
03-31 15:27:06.221 18797 18797 E ContextImpl:   at android.widget.Toast$TN.<init>(Toast.java:615)
03-31 15:27:06.221 18797 18797 E ContextImpl:   at android.widget.Toast.<init>(Toast.java:167)
03-31 15:27:06.221 18797 18797 E ContextImpl:   at android.widget.Toast.makeText(Toast.java:492)
03-31 15:27:06.221 18797 18797 E ContextImpl:   at android.widget.Toast.makeText(Toast.java:480)
03-31 15:27:06.221 18797 18797 E ContextImpl:   at com.android.bluetooth.opp.BluetoothOppReceiver.onReceive(BluetoothOppReceiver.java:275)
03-31 15:27:06.221 18797 18797 E ContextImpl:   at android.app.ActivityThread.handleReceiver(ActivityThread.java:4026)
03-31 15:27:06.221 18797 18797 E ContextImpl:   at android.app.ActivityThread.access$1400(ActivityThread.java:237)
03-31 15:27:06.221 18797 18797 E ContextImpl:   at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1924)
03-31 15:27:06.221 18797 18797 E ContextImpl:   at android.os.Handler.dispatchMessage(Handler.java:106)
03-31 15:27:06.221 18797 18797 E ContextImpl:   at android.os.Looper.loop(Looper.java:223)
03-31 15:27:06.221 18797 18797 E ContextImpl:   at android.app.ActivityThread.main(ActivityThread.java:7664)
03-31 15:27:06.221 18797 18797 E ContextImpl:   at java.lang.reflect.Method.invoke(Native Method)
03-31 15:27:06.221 18797 18797 E ContextImpl:   at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:592)
03-31 15:27:06.221 18797 18797 E ContextImpl:   at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:952)

修改方法:

Toast.makeText(context, toastMsg, Toast.LENGTH_SHORT).show();

修改为

DisplayManager dm = context.getSystemService(DisplayManager.class);
Display primaryDisplay = dm.getDisplay(DEFAULT_DISPLAY);
Context windowContext = context.createDisplayContext(primaryDisplay)
        .createWindowContext(TYPE_APPLICATION_OVERLAY, null);

Toast.makeText(windowContext, toastMsg, Toast.LENGTH_SHORT).show();
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值