WindowManager- InvalidDisplayException

最近在了解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>


---

  • 2
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值