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 android.view.ViewRootImpl$W@259f2ae -- the specified window type is not valid
......
09-14 11:11:37.755  9367  9367 E AndroidRuntime: Caused by: android.view.WindowManager$InvalidDisplayException: Unable to add window android.view.ViewRootImpl$W@259f2ae -- the specified window type is not valid
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,
                0 , 0, PixelFormat.TRANSPARENT); // w, h, type, flags, format;


 从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




    
        
            
                

                
            
        

        
        

    

    
    


---

你可能感兴趣的:(Android,Basic)