报如下错

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

为开发者提供学习成长、分享交流、生态实践、资源工具等服务,帮助开发者快速成长。

更多推荐