WindowManager- InvalidDisplayException

本文解决了在Android中使用WindowManager添加View时遇到的InvalidDisplayException问题,详细介绍了错误原因及如何正确设置LayoutParams。
部署运行你感兴趣的模型镜像

最近在了解Window及WindowManger,

Window表示一个窗口,是一个抽象类,具体由PhoneWindow实现。

而创建一个Window需要用到WindowManager, WindowManagerImpl,WindowManagerGlobal, ViewRootImpl等等,

最终以View的形式展现给用户。具体概念不做介绍,分享一个简单的例子及遇到的问题以及解决的办法。

解决遇到问题的过程比较重要。

 

最初是想在一个Activity 里通过WindowManager添加一个View, 例子也是书上的

但是结果FC了, 原因如下

09-14 11:11:37.755  9367  9367 E AndroidRuntime: java.lang.RuntimeException: Unable to start activity ComponentInfo{com.XX.viewmotiontest02/com.XX.viewmotiontest02.ViewMotionTest02}: android.view.WindowManager$InvalidDisplayException: Unable to add window <a target=_blank href="mailto:android.view.ViewRootImpl$W@259f2ae">android.view.ViewRootImpl$W@259f2ae</a> -- the specified window type is not valid
......
09-14 11:11:37.755  9367  9367 E AndroidRuntime: Caused by:<span style="color:#ff0000;"> android.view.WindowManager$InvalidDisplayException: Unable to add window </span><a target=_blank href="mailto:android.view.ViewRootImpl$W@259f2ae"><span style="color:#ff0000;">android.view.ViewRootImpl$W@259f2ae</span></a><span style="color:#ff0000;"> -- the specified window type is not valid</span>
09-14 11:11:37.755  9367  9367 E AndroidRuntime:  at android.view.ViewRootImpl.setView(ViewRootImpl.java:817)

 

对这种错误毫无头绪,并且是书上的例子。。

百度及谷歌了都没有类似的问题, 尝试新启动一个Service, 然后在Service 里面再添加一个View, 依然保持同样的错误。

准备放弃的时候,去官方API 瞄了一眼 WindowManager$InvalidDisplayException ,介绍说这个Exception是因为无法作为第二个View显示之类的。。

又看看错误 是 window type not valid,  想到了每添加一个View , 都会为其设置WindowManager.LayoutParms, 其中WindowManager.LayoutParms 就有type属性。。

而看了创建LayoutParms的代码:

mLayoutParams = new WindowManager.LayoutParams(WindowManager.LayoutParams.WRAP_CONTENT, WindowManager.LayoutParams.WRAP_CONTENT,
                <span style="color:#ff0000;">0</span> , 0, PixelFormat.TRANSPARENT); // <span style="color:#ff0000;">w, h, type, flags, format;</span>


 从API上看,LayoutParms对应上面的构造函数中, type 并没有0这个值的!!!

type: 1~99  应用Window层

type: 1000 ~1999 子Window

type:  2000~2999 系统层 (最顶层)

因此type 为0 必须报错!!!!

改成 WindowManager.LayoutParams.TYPE_SYSTEM_OVERLAY, 并且添加 uses-permission android:name="android.permission.SYSTEM_ALERT_WINDOW"

所以,关键时候,官方API 还是很有帮助的!!!

 

部分代码:

package com.XX.viewmotiontest02;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.graphics.PixelFormat;
import android.os.Bundle;
import android.util.Log;
import android.view.Gravity;
import android.view.View;
import android.view.WindowManager;
import android.widget.Button;

public class ViewMotionTest02 extends Activity {
    private static final String LOG_TAG = "ViewMotionTest02";
    private boolean mIsUseService;
    Button mStartBtn;
    Button mFloatingButton;
    WindowManager.LayoutParams mLayoutParams;
    WindowManager mWindowManager;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        Log.d(LOG_TAG,"onCreate ");
        setContentView(R.layout.activity_view_motion_test02);

        mIsUseService = false; // true: Service,  false: Activity
        Log.d(LOG_TAG,"mIsUseService: " + mIsUseService);
        if(mIsUseService) {
            mStartBtn = (Button) findViewById(R.id.startServiceBtn);
            mStartBtn.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    Log.d(LOG_TAG, "onClick");
                    Intent intent = new Intent(getApplicationContext(), ViewMotionService.class);
                    startService(intent);
                }
            });
        } else {
            //mStartBtn.setVisibility(View.INVISIBLE);
            mFloatingButton = new Button(this);
            mFloatingButton.setText("Button In Activity");
            mLayoutParams = new WindowManager.LayoutParams(WindowManager.LayoutParams.WRAP_CONTENT, WindowManager.LayoutParams.WRAP_CONTENT,
                    WindowManager.LayoutParams.TYPE_SYSTEM_OVERLAY , 0, PixelFormat.TRANSPARENT); // w, h, type, flags, format;
            mLayoutParams.flags = WindowManager.LayoutParams.FLAG_NOT_TOUCH_MODAL
                    | WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE
                    | WindowManager.LayoutParams.FLAG_SHOW_WHEN_LOCKED;
            mLayoutParams.gravity = Gravity.LEFT | Gravity.TOP;
            mLayoutParams.x = 100;
            mLayoutParams.y = 300;
            mWindowManager = (WindowManager) getSystemService(Context.WINDOW_SERVICE);
            mWindowManager.addView(mFloatingButton,mLayoutParams);
        }

    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        Log.d(LOG_TAG,"onDestroy ");
        if(mIsUseService) {
            Intent intent = new Intent(getApplicationContext(), ViewMotionService.class);
            stopService(intent);
        } else{
            mWindowManager.removeView(mFloatingButton);
        }
    }
}


 

 

package com.XX.viewmotiontest02;

import android.app.Service;
import android.content.Context;
import android.content.Intent;
import android.graphics.PixelFormat;
import android.os.IBinder;
import android.support.annotation.Nullable;
import android.util.Log;
import android.view.Gravity;
import android.view.WindowManager;
import android.widget.Button;

public class ViewMotionService extends Service {
    private static final String LOG_TAG = "ViewMotionService";
    Button mFloatingButton;
    WindowManager.LayoutParams mLayoutParams;
    WindowManager mWindowManager;

    @Override
    public void onCreate() {
        super.onCreate();

        Log.d(LOG_TAG, "onCreate" );
        mFloatingButton = new Button(this);
        mFloatingButton.setText("Button");
        mLayoutParams = new WindowManager.LayoutParams(WindowManager.LayoutParams.WRAP_CONTENT, WindowManager.LayoutParams.WRAP_CONTENT,
                WindowManager.LayoutParams.TYPE_SYSTEM_OVERLAY , 0, PixelFormat.TRANSPARENT); // w, h, type, flags, format;
        //mLayoutParams = new WindowManager.LayoutParams();
        //mLayoutParams.height = WindowManager.LayoutParams.WRAP_CONTENT;
        //mLayoutParams.width = WindowManager.LayoutParams.WRAP_CONTENT;
        //mLayoutParams.type = WindowManager.LayoutParams.TYPE_SYSTEM_OVERLAY;
        mLayoutParams.flags = WindowManager.LayoutParams.FLAG_NOT_TOUCH_MODAL
                | WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE
                | WindowManager.LayoutParams.FLAG_SHOW_WHEN_LOCKED;
        //mLayoutParams.format = PixelFormat.TRANSPARENT;
        mLayoutParams.gravity = Gravity.LEFT | Gravity.TOP;
        mLayoutParams.x = 100;
        mLayoutParams.y = 300;
        mWindowManager = (WindowManager) getSystemService(Context.WINDOW_SERVICE);
        //Log.d(LOG_TAG,"onCreate  - 2 change -3  ");
        mWindowManager.addView(mFloatingButton,mLayoutParams);
        //Log.d(LOG_TAG,"onCreate  - 3  ");
    }

    @Nullable
    @Override
    public IBinder onBind(Intent intent) {
        return null;
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        Log.d(LOG_TAG, "onDestroy" );
        mWindowManager.removeView(mFloatingButton);
    }
}

 

AndroidManifest.xml

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.xx.viewmotiontest02">

    <application
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:supportsRtl="true"
        android:theme="@style/AppTheme">
        <activity android:name=".ViewMotionTest02">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>

        <service android:name=".ViewMotionService" >
        </service>

    </application>

    <uses-permission android:name="android.permission.SYSTEM_ALERT_WINDOW" >
    </uses-permission>

</manifest>


---

您可能感兴趣的与本文相关的镜像

Dify

Dify

AI应用
Agent编排

Dify 是一款开源的大语言模型(LLM)应用开发平台,它结合了 后端即服务(Backend as a Service) 和LLMOps 的理念,让开发者能快速、高效地构建和部署生产级的生成式AI应用。 它提供了包含模型兼容支持、Prompt 编排界面、RAG 引擎、Agent 框架、工作流编排等核心技术栈,并且提供了易用的界面和API,让技术和非技术人员都能参与到AI应用的开发过程中

07-20 19:57:40.672 5131 5131 D AndroidRuntime: Shutting down VM --------- beginning of crash 07-20 19:57:40.673 5131 5131 E AndroidRuntime: FATAL EXCEPTION: main 07-20 19:57:40.673 5131 5131 E AndroidRuntime: Process: com.adayo.aaop_deviceservice, PID: 5131 07-20 19:57:40.673 5131 5131 E AndroidRuntime: java.lang.RuntimeException: Unable to create service com.adayo.aaop_deviceservice.service.BurialPointService: android.view.WindowManager$InvalidDisplayException: Unable to add window android.view.ViewRootImpl$W@e6477e5 -- the specified display can not be found 07-20 19:57:40.673 5131 5131 E AndroidRuntime: at android.app.ActivityThread.handleCreateService(ActivityThread.java:4221) 07-20 19:57:40.673 5131 5131 E AndroidRuntime: at android.app.ActivityThread.access$1600(ActivityThread.java:238) 07-20 19:57:40.673 5131 5131 E AndroidRuntime: at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1955) 07-20 19:57:40.673 5131 5131 E AndroidRuntime: at android.os.Handler.dispatchMessage(Handler.java:106) 07-20 19:57:40.673 5131 5131 E AndroidRuntime: at android.os.Looper.loop(Looper.java:223) 07-20 19:57:40.673 5131 5131 E AndroidRuntime: at android.app.ActivityThread.main(ActivityThread.java:7731) 07-20 19:57:40.673 5131 5131 E AndroidRuntime: at java.lang.reflect.Method.invoke(Native Method) 07-20 19:57:40.673 5131 5131 E AndroidRuntime: at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:592) 07-20 19:57:40.673 5131 5131 E AndroidRuntime: at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:947) 07-20 19:57:40.673 5131 5131 E AndroidRuntime: Caused by: android.view.WindowManager$InvalidDisplayException: Unable to add window android.view.ViewRootImpl$W@e6477e5 -- the specified display can not be found 07-20 19:57:40.673 5131 5131 E AndroidRuntime: at android.view.ViewRootImpl.setView(ViewRootImpl.java:1099) 07-20 19:57:40.673 5131 5131 E AndroidRuntime: at android.view.WindowManagerGlobal.addView(WindowManagerGlobal.java:409) 07-20 19:57:40.673 5131 5131 E AndroidRuntime: at android.view.WindowManagerImpl.addView(WindowManagerImpl.java:109) 07-20 19:57:40.673 5131 5131 E AndroidRuntime: at android.app.Dialog.show(Dialog.java:342) 07-20 19:57:40.673 5131 5131 E AndroidRuntime: at android.app.Presentation.show(Presentation.java:257) 07-20 19:57:40.673 5131 5131 E AndroidRuntime: at com.adayo.aaop_deviceservice.service.BurialPointService.x(Unknown Source:77) 07-20 19:57:40.673 5131 5131 E AndroidRuntime: at com.adayo.aaop_deviceservice.service.BurialPointService.A(Unknown Source:50) 07-20 19:57:40.673 5131 5131 E AndroidRuntime: at com.adayo.aaop_deviceservice.service.BurialPointService.onCreate(Unknown Source:10) 07-20 19:57:40.673 5131 5131 E AndroidRuntime: at android.app.ActivityThread.handleCreateService(ActivityThread.java:4209) 07-20 19:57:40.673 5131 5131 E AndroidRuntime: ... 8 more 07-20 19:57:40.676 5131 5131 I Process : Sending signal. PID: 5131 SIG: 9 private void createBackgroundToQnx() { DisplayManager displayManager = (DisplayManager)this.getSystemService(Context.DISPLAY_SERVICE); Display[] displays = displayManager.getDisplays(); Log.i(TAG, "createBackgroundToQnx displays = " + Arrays.toString(displays)); for (Display display : displayManager.getDisplays()) { if (display.getDisplayId() == 1) { Presentation presentation = new Presentation(this, displayManager.getDisplay(1)); View view = new View(this); view.setBackgroundColor(Color.BLACK); presentation.setContentView(view); presentation.show(); } } }
08-07
我的是系统级服务,然后接了主屏和仪表屏也报这个错误,延迟初始化也没用android.view.WindowManager$InvalidDisplayException: Unable to add window android.view.ViewRootImpl$W@81df4e9 -- the specified display can not be found 07-20 19:57:33.600 2235 2235 I BurialPointService1: at android.view.ViewRootImpl.setView(ViewRootImpl.java:1099) 07-20 19:57:33.600 2235 2235 I BurialPointService1: at android.view.WindowManagerGlobal.addView(WindowManagerGlobal.java:409) 07-20 19:57:33.600 2235 2235 I BurialPointService1: at android.view.WindowManagerImpl.addView(WindowManagerImpl.java:109) 07-20 19:57:33.600 2235 2235 I BurialPointService1: at android.app.Dialog.show(Dialog.java:342) 07-20 19:57:33.600 2235 2235 I BurialPointService1: at android.app.Presentation.show(Presentation.java:257) 07-20 19:57:33.600 2235 2235 I BurialPointService1: at com.adayo.aaop_deviceservice.service.BurialPointService.x(Unknown Source:109) 07-20 19:57:33.600 2235 2235 I BurialPointService1: at com.adayo.aaop_deviceservice.service.BurialPointService.A(Unknown Source:50) 07-20 19:57:33.600 2235 2235 I BurialPointService1: at com.adayo.aaop_deviceservice.service.BurialPointService.onCreate(Unknown Source:10) 07-20 19:57:33.600 2235 2235 I BurialPointService1: at android.app.ActivityThread.handleCreateService(ActivityThread.java:4209) 07-20 19:57:33.600 2235 2235 I BurialPointService1: at android.app.ActivityThread.access$1600(ActivityThread.java:238) 07-20 19:57:33.600 2235 2235 I BurialPointService1: at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1955) 07-20 19:57:33.600 2235 2235 I BurialPointService1: at android.os.Handler.dispatchMessage(Handler.java:106) 07-20 19:57:33.600 2235 2235 I BurialPointService1: at android.os.Looper.loop(Looper.java:223) 07-20 19:57:33.600 2235 2235 I BurialPointService1: at android.app.ActivityThread.main(ActivityThread.java:7731) 07-20 19:57:33.600 2235 2235 I BurialPointService1: at java.lang.reflect.Method.invoke(Native Method) 07-20 19:57:33.600 2235 2235 I BurialPointService1: at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:592) 07-20 19:57:33.600 2235 2235 I BurialPointService1: at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:947) private void createBackgroundToQnx() { DisplayManager displayManager = (DisplayManager)this.getSystemService(Context.DISPLAY_SERVICE); Display[] displays = displayManager.getDisplays(); Log.i(TAG, "createBackgroundToQnx displays = " + Arrays.toString(displays)); for (Display display : displayManager.getDisplays()) { if (display.getDisplayId() != Display.DEFAULT_DISPLAY) { if (!display.isValid()) { Log.i(TAG, "createBackgroundToQnx display is invalid ! displayID = " + display.getDisplayId()); continue; } try { Presentation presentation = new Presentation(getApplicationContext(), display); View view = new View(this); view.setBackgroundColor(Color.BLACK); presentation.setContentView(view); presentation.show(); } catch (WindowManager.InvalidDisplayException e) { Log.i(TAG, "Failed to show presentation on display: " + display, e); } } } }
08-07
07-20 19:57:33.402 2248 2248 I BurialPointService1: Failed to show presentation on display: Display id 1: DisplayInfo{"内置屏幕", displayId 1, FLAG_SECURE, FLAG_SUPPORTS_PROTECTED_BUFFERS, FLAG_TRUSTED, real 1920 x 720, largest app 1920 x 1920, smallest app 720 x 720, appVsyncOff 1000000, presDeadline 16666666, mode 2, defaultMode 2, modes [{id=2, width=1920, height=720, fps=60.000004}], hdrCapabilities HdrCapabilities{mSupportedHdrTypes=[], mMaxLuminance=500.0, mMaxAverageLuminance=500.0, mMinLuminance=0.0}, minimalPostProcessingSupported false, rotation 0, state ON, type INTERNAL, uniqueId "local:130", app 1920 x 720, density 160 (167.013 x 167.779) dpi, layerStack 1, colorMode 0, supportedColorModes [0], address {port=130}, deviceProductInfo null, removeMode 0}, DisplayMetrics{density=1.0, width=1920, height=720, scaledDensity=1.0, xdpi=167.013, ydpi=167.779}, isValid=true 07-20 19:57:33.402 2248 2248 I BurialPointService1: android.view.WindowManager$InvalidDisplayException: Unable to add window android.view.ViewRootImpl$W@a379ee3 -- the specified display can not be found 07-20 19:57:33.402 2248 2248 I BurialPointService1: at android.view.ViewRootImpl.setView(ViewRootImpl.java:1099) 07-20 19:57:33.402 2248 2248 I BurialPointService1: at android.view.WindowManagerGlobal.addView(WindowManagerGlobal.java:409) 07-20 19:57:33.402 2248 2248 I BurialPointService1: at android.view.WindowManagerImpl.addView(WindowManagerImpl.java:109) 07-20 19:57:33.402 2248 2248 I BurialPointService1: at android.app.Dialog.show(Dialog.java:342) 07-20 19:57:33.402 2248 2248 I BurialPointService1: at android.app.Presentation.show(Presentation.java:257) 07-20 19:57:33.402 2248 2248 I BurialPointService1: at com.adayo.aaop_deviceservice.service.BurialPointService.y(Unknown Source:48) 07-20 19:57:33.402 2248 2248 I BurialPointService1: at com.adayo.aaop_deviceservice.service.BurialPointService.B(Unknown Source:53) 07-20 19:57:33.402 2248 2248 I BurialPointService1: at com.adayo.aaop_deviceservice.service.BurialPointService.onCreate(Unknown Source:10) 07-20 19:57:33.402 2248 2248 I BurialPointService1: at android.app.ActivityThread.handleCreateService(ActivityThread.java:4209) 07-20 19:57:33.402 2248 2248 I BurialPointService1: at android.app.ActivityThread.access$1600(ActivityThread.java:238) 07-20 19:57:33.402 2248 2248 I BurialPointService1: at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1955) 07-20 19:57:33.402 2248 2248 I BurialPointService1: at android.os.Handler.dispatchMessage(Handler.java:106) 07-20 19:57:33.402 2248 2248 I BurialPointService1: at android.os.Looper.loop(Looper.java:223) 07-20 19:57:33.402 2248 2248 I BurialPointService1: at android.app.ActivityThread.main(ActivityThread.java:7731) 07-20 19:57:33.402 2248 2248 I BurialPointService1: at java.lang.reflect.Method.invoke(Native Method) 07-20 19:57:33.402 2248 2248 I BurialPointService1: at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:592) 07-20 19:57:33.402 2248 2248 I BurialPointService1: at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:947) private void createBackgroundToQnx() { int displayId = 1; DisplayManager dm = (DisplayManager) getSystemService(Context.DISPLAY_SERVICE); Display display = dm.getDisplay(displayId); if (display == null || !display.isValid()) { Log.e(TAG, "Invalid display: " + displayId); return; } try { Context displayContext = getApplicationContext().createDisplayContext(display); Presentation presentation = new Presentation(displayContext, display); View view = new View(this); view.setBackgroundColor(Color.BLACK); presentation.setContentView(view); presentation.show(); } catch (WindowManager.InvalidDisplayException e) { Log.i(TAG, "Failed to show presentation on display: " + display, e); } }
最新发布
08-08
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值