WindowManager $ InvalidDisplayException - 指定的窗口类型-1无效

时间:2018-04-12 06:08:40

标签: android intentservice

我正在尝试从服务中扩充UI元素。但是出现WindowManager$InvalidDisplayException - the specified window type -1 is not valid错误。 我的代码如下,

WindowManager windowManager = (WindowManager)getSystemService(WINDOW_SERVICE);
LayoutInflater inflater = (LayoutInflater)getSystemService(LAYOUT_INFLATER_SERVICE);
RelativeLayout layout = (RelativeLayout) inflater.inflate(R.layout.box, null);
WindowManager.LayoutParams params = new WindowManager.LayoutParams(
       WindowManager.LayoutParams.MATCH_PARENT,
       WindowManager.LayoutParams.MATCH_PARENT,
       PixelFormat.TRANSLUCENT));

windowManager.addView(layout, params);

我收到此错误,

android.view.WindowManager$InvalidDisplayException: Unable to add window android.view.ViewRootImpl$W@8b06417 -- the specified window type -1 is not valid
    at android.view.ViewRootImpl.setView(ViewRootImpl.java:725)
    at android.view.WindowManagerGlobal.addView(WindowManagerGlobal.java:342)
    at android.view.WindowManagerImpl.addView(WindowManagerImpl.java:94)
    at com.firebaseapp.privacy.MyIntentService.handleAction(MyIntentService.java:33)
    at com.firebaseapp.privacy.MyIntentService.onHandleIntent(MyIntentService.java:20)
    at android.app.IntentService$ServiceHandler.handleMessage(IntentService.java:68)
    at android.os.Handler.dispatchMessage(Handler.java:102)
    at android.os.Looper.loop(Looper.java:154)
    at android.os.HandlerThread.run(HandlerThread.java:61)

1 个答案:

答案 0 :(得分:1)

您需要在清单中添加权限

android.permission.SYSTEM_ALERT_WINDOW

还尝试添加params类型

params.type = WindowManager.LayoutParams.TYPE_SYSTEM_ERROR;

希望这能解决您的问题