WindowManager 崩溃 (Java/Android)

Posted

技术标签:

【中文标题】WindowManager 崩溃 (Java/Android)【英文标题】:WindowManager Crash (Java/Android) 【发布时间】:2022-01-24 05:42:37 【问题描述】:

做这个项目我太累了 我正在编写一个项目,该项目显示像 Facebook 这样的头部聊天 但是当我将视图添加到窗口管理器时,它不会显示头部聊天并崩溃 请帮帮我 MainActivity.java:

public class MainActivity extends AppCompatActivity 

    @Override
    protected void onCreate(Bundle savedInstanceState) 
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        findViewById(R.id.start_bubble).setOnClickListener(new View.OnClickListener() 
            @Override
            public void onClick(View view) 
                startService(new Intent(MainActivity.this, ChatHeadService.class));
                finish();
            
        );
    

这是我的 ChatHeadService.java 代码:

public class ChatHeadService extends Service 

    private WindowManager mWindowManager;
    private View mChatHeadView;

    public ChatHeadService() 
    

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

    @Override
    public void onCreate() 
        super.onCreate();
        //Inflate the chat head layout we created
        mWindowManager = (WindowManager) getSystemService(WINDOW_SERVICE);
        mChatHeadView = LayoutInflater.from(this).inflate(R.layout.layout_chat_head, null);

        //Add the view to the window.
        final WindowManager.LayoutParams params = new WindowManager.LayoutParams(
                WindowManager.LayoutParams.WRAP_CONTENT,
                WindowManager.LayoutParams.WRAP_CONTENT,
                WindowManager.LayoutParams.TYPE_PHONE,
                WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE,
                PixelFormat.TRANSLUCENT);

        //Specify the chat head position
        params.gravity = Gravity.TOP | Gravity.LEFT;        //Initially view will be added to top-left corner
        params.x = 0;
        params.y = 100;

        //Add the view to the window
        mWindowManager.addView(mChatHeadView, params);

        //Set the close button.
        ImageView closeButton = (ImageView) mChatHeadView.findViewById(R.id.close_btn);
        closeButton.setOnClickListener(new View.OnClickListener() 
            @Override
            public void onClick(View v) 
                //close the service and remove the chat head from the window
                stopSelf();
            
        );

        //Drag and move chat head using user's touch action.
        final ImageView chatHeadImage = (ImageView) mChatHeadView.findViewById(R.id.chat_head_profile_iv);
        chatHeadImage.setOnTouchListener(new View.OnTouchListener() 
            private int lastAction;
            private int initialX;
            private int initialY;
            private float initialTouchX;
            private float initialTouchY;

            @Override
            public boolean onTouch(View v, MotionEvent event) 
                switch (event.getAction()) 
                    case MotionEvent.ACTION_DOWN:

                        //remember the initial position.
                        initialX = params.x;
                        initialY = params.y;

                        //get the touch location
                        initialTouchX = event.getRawX();
                        initialTouchY = event.getRawY();

                        lastAction = event.getAction();
                        return true;
                    case MotionEvent.ACTION_UP:
                        //As we implemented on touch listener with ACTION_MOVE,
                        //we have to check if the previous action was ACTION_DOWN
                        //to identify if the user clicked the view or not.
                        if (lastAction == MotionEvent.ACTION_DOWN) 
                            //Open the chat conversation click.
                            Intent intent = new Intent(ChatHeadService.this, ChatActivity.class);
                            intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                            startActivity(intent);

                            //close the service and remove the chat heads
                            stopSelf();
                        
                        lastAction = event.getAction();
                        return true;
                    case MotionEvent.ACTION_MOVE:
                        //Calculate the X and Y coordinates of the view.
                        params.x = initialX + (int) (event.getRawX() - initialTouchX);
                        params.y = initialY + (int) (event.getRawY() - initialTouchY);

                        //Update the layout with new X & Y coordinate
                        mWindowManager.updateViewLayout(mChatHeadView, params);
                        lastAction = event.getAction();
                        return true;
                
                return false;
            
        );
    

    @Override
    public void onDestroy() 
        super.onDestroy();
        if (mChatHeadView != null) mWindowManager.removeView(mChatHeadView);
    

这是我的日志:

2021-12-23 13:34:05.327 12661-12661/com.example.projectkhkt2021 I/DecorView: updateCaptionType >> DecorView@788d084[], isFloating=false, isApplication=true, hasWindowDecorCaption=false, hasWindowControllerCallback=true
2021-12-23 13:34:05.327 12661-12661/com.example.projectkhkt2021 D/DecorView: setCaptionType = 0, this = DecorView@788d084[]
2021-12-23 13:34:05.387 12661-12661/com.example.projectkhkt2021 D/InputTransport: Input channel constructed: 'f8e5316', fd=78
2021-12-23 13:34:05.389 12661-12661/com.example.projectkhkt2021 I/ViewRootImpl@b9862c8[MainActivity]: setView = com.android.internal.policy.DecorView@788d084 TM=true
2021-12-23 13:34:05.418 12661-12661/com.example.projectkhkt2021 I/SurfaceControl: assignNativeObject: nativeObject = 0 Surface(name=null)/@0x3316999 / android.view.SurfaceControl.readFromParcel:1117 android.view.IWindowSession$Stub$Proxy.relayout:1820 android.view.ViewRootImpl.relayoutWindow:9005 android.view.ViewRootImpl.performTraversals:3360 android.view.ViewRootImpl.doTraversal:2618 android.view.ViewRootImpl$TraversalRunnable.run:9971 android.view.Choreographer$CallbackRecord.run:1010 android.view.Choreographer.doCallbacks:809 android.view.Choreographer.doFrame:744 android.view.Choreographer$FrameDisplayEventReceiver.run:995 
2021-12-23 13:34:05.420 12661-12661/com.example.projectkhkt2021 I/ViewRootImpl@b9862c8[MainActivity]: Relayout returned: old=(0,0,1080,2400) new=(0,0,1080,2400) req=(1080,2400)0 dur=11 res=0x7 s=true -5476376630825729904 ch=true fn=-1
2021-12-23 13:34:05.452 12661-12661/com.example.projectkhkt2021 I/ViewRootImpl@b9862c8[MainActivity]: MSG_WINDOW_FOCUS_CHANGED 1 1
2021-12-23 13:34:05.452 12661-12661/com.example.projectkhkt2021 D/InputMethodManager: prepareNavigationBarInfo() DecorView@788d084[MainActivity]
2021-12-23 13:34:05.452 12661-12661/com.example.projectkhkt2021 D/InputMethodManager: getNavigationBarColor() -855310
2021-12-23 13:34:05.454 12661-12661/com.example.projectkhkt2021 D/InputMethodManager: prepareNavigationBarInfo() DecorView@788d084[MainActivity]
2021-12-23 13:34:05.454 12661-12661/com.example.projectkhkt2021 D/InputMethodManager: getNavigationBarColor() -855310
2021-12-23 13:34:05.455 12661-12661/com.example.projectkhkt2021 V/InputMethodManager: Starting input: tba=com.example.projectkhkt2021 ic=null mNaviBarColor -855310 mIsGetNaviBarColorSuccess true , NavVisible : true , NavTrans : false
2021-12-23 13:34:05.455 12661-12661/com.example.projectkhkt2021 D/InputMethodManager: startInputInner - Id : 0
2021-12-23 13:34:05.455 12661-12661/com.example.projectkhkt2021 I/InputMethodManager: startInputInner - mService.startInputOrWindowGainedFocus
2021-12-23 13:34:05.458 12661-12661/com.example.projectkhkt2021 D/InputTransport: Input channel constructed: 'ClientS', fd=86
2021-12-23 13:34:05.474 12661-12661/com.example.projectkhkt2021 I/SurfaceControl: nativeRelease nativeObject s[-5476376635120561328]
2021-12-23 13:34:05.474 12661-12661/com.example.projectkhkt2021 I/SurfaceControl: nativeRelease nativeObject e[-5476376635120561328]
2021-12-23 13:34:05.474 12661-12661/com.example.projectkhkt2021 I/SurfaceControl: nativeRelease nativeObject s[-5476376635120557632]
2021-12-23 13:34:05.474 12661-12661/com.example.projectkhkt2021 I/SurfaceControl: nativeRelease nativeObject e[-5476376635120557632]
2021-12-23 13:34:10.047 12661-12661/com.example.projectkhkt2021 I/ViewRootImpl@b9862c8[MainActivity]: ViewPostIme pointer 0
2021-12-23 13:34:10.080 12661-12661/com.example.projectkhkt2021 I/ViewRootImpl@b9862c8[MainActivity]: ViewPostIme pointer 1
2021-12-23 13:34:10.133 12661-12661/com.example.projectkhkt2021 I/ViewRootImpl@b9862c8[MainActivity]: MSG_WINDOW_FOCUS_CHANGED 0 1
2021-12-23 13:34:10.199 12661-12661/com.example.projectkhkt2021 I/SurfaceControl: nativeRelease nativeObject s[-5476376635120539328]
2021-12-23 13:34:10.199 12661-12661/com.example.projectkhkt2021 I/SurfaceControl: nativeRelease nativeObject e[-5476376635120539328]
2021-12-23 13:34:10.199 12661-12661/com.example.projectkhkt2021 I/SurfaceControl: nativeRelease nativeObject s[-5476376635120557104]
2021-12-23 13:34:10.199 12661-12661/com.example.projectkhkt2021 I/SurfaceControl: nativeRelease nativeObject e[-5476376635120557104]
2021-12-23 13:34:10.199 12661-12661/com.example.projectkhkt2021 I/SurfaceControl: nativeRelease nativeObject s[-5476376635120560976]
2021-12-23 13:34:10.199 12661-12661/com.example.projectkhkt2021 I/SurfaceControl: nativeRelease nativeObject e[-5476376635120560976]
2021-12-23 13:34:10.222 12661-12661/com.example.projectkhkt2021 D/InputTransport: Input channel destroyed: 'ClientS', fd=86
2021-12-23 13:34:10.236 12661-12661/com.example.projectkhkt2021 I/SurfaceControl: nativeRelease nativeObject s[-5476376635120440240]
2021-12-23 13:34:10.236 12661-12661/com.example.projectkhkt2021 I/SurfaceControl: nativeRelease nativeObject e[-5476376635120440240]
2021-12-23 13:34:10.473 12661-12661/com.example.projectkhkt2021 I/SurfaceControl: nativeRelease nativeObject s[-5476376635120417360]
2021-12-23 13:34:10.473 12661-12661/com.example.projectkhkt2021 I/SurfaceControl: nativeRelease nativeObject e[-5476376635120417360]
2021-12-23 13:34:10.474 12661-12661/com.example.projectkhkt2021 I/SurfaceControl: assignNativeObject: nativeObject = 0 Surface(name=null)/@0x4770136 / android.view.SurfaceControl.readFromParcel:1117 android.view.IWindowSession$Stub$Proxy.relayout:1810 android.view.ViewRootImpl.relayoutWindow:9005 android.view.ViewRootImpl.performTraversals:3360 android.view.ViewRootImpl.doTraversal:2618 android.view.ViewRootImpl$TraversalRunnable.run:9971 android.view.Choreographer$CallbackRecord.run:1010 android.view.Choreographer.doCallbacks:809 android.view.Choreographer.doFrame:744 android.view.Choreographer$FrameDisplayEventReceiver.run:995 
2021-12-23 13:34:10.475 12661-12661/com.example.projectkhkt2021 I/SurfaceControl: assignNativeObject: nativeObject = 0 Surface(name=null)/@0x3316999 / android.view.SurfaceControl.readFromParcel:1117 android.view.IWindowSession$Stub$Proxy.relayout:1820 android.view.ViewRootImpl.relayoutWindow:9005 android.view.ViewRootImpl.performTraversals:3360 android.view.ViewRootImpl.doTraversal:2618 android.view.ViewRootImpl$TraversalRunnable.run:9971 android.view.Choreographer$CallbackRecord.run:1010 android.view.Choreographer.doCallbacks:809 android.view.Choreographer.doFrame:744 android.view.Choreographer$FrameDisplayEventReceiver.run:995 
2021-12-23 13:34:10.477 12661-12661/com.example.projectkhkt2021 I/ViewRootImpl@b9862c8[MainActivity]: Relayout returned: old=(0,0,1080,2400) new=(0,0,1080,2400) req=(1080,2400)8 dur=8 res=0x5 s=false 0 ch=true fn=23
2021-12-23 13:34:10.480 12661-12661/com.example.projectkhkt2021 I/ViewRootImpl@b9862c8[MainActivity]: stopped(true) old=false
2021-12-23 13:34:10.489 12661-12661/com.example.projectkhkt2021 I/ViewRootImpl@b9862c8[MainActivity]: dispatchDetachedFromWindow
2021-12-23 13:34:10.493 12661-12661/com.example.projectkhkt2021 D/InputTransport: Input channel destroyed: 'f8e5316', fd=78

你对我有什么建议吗?

[不知道为什么说我的帖子有太多代码,我来客它是因为日志所以我需要在这里写点东西]

【问题讨论】:

您发布的日志没有任何崩溃堆栈跟踪。将日志设置为错误模式,然后发布崩溃日志。 好的,我会编辑帖子 似乎没有任何错误。不知道为什么,但它关闭了应用程序并且没有出现任何头部聊天。顺便说一句,我认为这些信息会有所帮助 [另一个日志] \ I/ViewRootImpl@ffa9f53[MainActivity]:stopped(true) old=false I/ViewRootImpl@ffa9f53[MainActivity]: dispatchDetachedFromWindow \ 视频显示它是如何崩溃的:drive.google.com/file/d/1A5WI9vN6Mw2GzkQmuTK5QCRTPUy19yXa/… 【参考方案1】:

首先您需要验证目标设备,然后它才能工作。这是根据您的服务提供的示例代码,它可以正常工作。

public class ChatHeadService extends Service 

private WindowManager mWindowManager;
private View mChatHeadView;
int LAYOUT_FLAG;

public ChatHeadService() 


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


@Override
public void onCreate() 
    super.onCreate();
    
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) 
        LAYOUT_FLAG = WindowManager.LayoutParams.TYPE_APPLICATION_OVERLAY;
     else 
        LAYOUT_FLAG = WindowManager.LayoutParams.TYPE_PHONE;
    
    //Inflate the chat head layout we created
    mWindowManager = (WindowManager) getSystemService(WINDOW_SERVICE);
    mChatHeadView = LayoutInflater.from(this).inflate(R.layout.layout_chat_head, null);

    //Add the view to the window.
    final WindowManager.LayoutParams params = new WindowManager.LayoutParams(
            WindowManager.LayoutParams.WRAP_CONTENT,
            WindowManager.LayoutParams.WRAP_CONTENT,
            LAYOUT_FLAG,
            WindowManager.LayoutParams.FLAG_NOT_TOUCHABLE | WindowManager.LayoutParams.FLAG_LAYOUT_IN_SCREEN | WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE,
            PixelFormat.TRANSLUCENT);

    //Specify the chat head position
    params.gravity = Gravity.TOP | Gravity.LEFT;        //Initially view will be added to top-left corner
    params.x = 0;
    params.y = 100;

    //Add the view to the window
    mWindowManager.addView(mChatHeadView, params);

    //Set the close button.
    ImageView closeButton = (ImageView) mChatHeadView.findViewById(R.id.close_btn);
    closeButton.setOnClickListener(new View.OnClickListener() 
        @Override
        public void onClick(View v) 
            //close the service and remove the chat head from the window
            stopSelf();
        
    );

    //Drag and move chat head using user's touch action.
    final ImageView chatHeadImage = (ImageView) mChatHeadView.findViewById(R.id.chat_head_profile_iv);
    chatHeadImage.setOnTouchListener(new View.OnTouchListener() 
        private int lastAction;
        private int initialX;
        private int initialY;
        private float initialTouchX;
        private float initialTouchY;

        @Override
        public boolean onTouch(View v, MotionEvent event) 
            switch (event.getAction()) 
                case MotionEvent.ACTION_DOWN:

                    //remember the initial position.
                    initialX = params.x;
                    initialY = params.y;

                    //get the touch location
                    initialTouchX = event.getRawX();
                    initialTouchY = event.getRawY();

                    lastAction = event.getAction();
                    return true;
                case MotionEvent.ACTION_UP:
                    //As we implemented on touch listener with ACTION_MOVE,
                    //we have to check if the previous action was ACTION_DOWN
                    //to identify if the user clicked the view or not.
                    if (lastAction == MotionEvent.ACTION_DOWN) 
                        //Open the chat conversation click.
                        Intent intent = new Intent(ChatHeadService.this, ChatActivity.class);
                        intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                        startActivity(intent);

                        //close the service and remove the chat heads
                        stopSelf();
                    
                    lastAction = event.getAction();
                    return true;
                case MotionEvent.ACTION_MOVE:
                    //Calculate the X and Y coordinates of the view.
                    params.x = initialX + (int) (event.getRawX() - initialTouchX);
                    params.y = initialY + (int) (event.getRawY() - initialTouchY);

                    //Update the layout with new X & Y coordinate
                    mWindowManager.updateViewLayout(mChatHeadView, params);
                    lastAction = event.getAction();
                    return true;
            
            return false;
        
    );


@Override
public void onDestroy() 
    super.onDestroy();
    if (mChatHeadView != null) mWindowManager.removeView(mChatHeadView);


【讨论】:

还是不行:( 我已经测试过了,它的工作。您是否在清单 和服务示例中添加了这些行 哦,好的,谢谢。我忘了添加服务

以上是关于WindowManager 崩溃 (Java/Android)的主要内容,如果未能解决你的问题,请参考以下文章

Android7.1.1系统,Toast的Exception: android.view.WindowManager$BadTokenException解决

Android7.1.1系统,Toast的Exception: android.view.WindowManager$BadTokenException解决

Android7.1.1系统,Toast的Exception: android.view.WindowManager$BadTokenException解决

Android7.1.1系统,Toast的Exception: android.view.WindowManager$BadTokenException解决

[Android]异常8-android.view.WindowManager$BadTokenException

Android报错:WindowManager$BadTokenException: Unable to add window -- window has already been added(示例代