Python中文网

一个关于 编程问题的解答网站.

有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

java活动未附加到windows管理器

我有一个浮动小部件(可以覆盖所有的应用程序),长时间点击(鼠标按住)将从底部显示一个相对布局(remove_relativelayout),这样用户可以关闭小部件,服务将被销毁

在大多数三星手机上,我都会遇到这样的故障:

Fatal Exception: java.lang.IllegalArgumentException: View=安卓.widget.RelativeLayout{762db5 G.E...... ......ID 0,0-160,160 #7f09022d app:id/remove_relativelayout} not attached to window manager
       at 安卓.view.WindowManagerGlobal.findViewLocked(WindowManagerGlobal.java:569)
       at 安卓.view.WindowManagerGlobal.updateViewLayout(WindowManagerGlobal.java:463)
       at 安卓.view.WindowManagerImpl.updateViewLayout(WindowManagerImpl.java:101)
       at com.xxxx.dev.floatingwidget.services.FloatingWidgetServices.lambda$widgetLongClick$0(FloatingWidgetServices.java:20)
       at com.xxxxx.dev.floatingwidget.services.FloatingWidgetServices.lambda$widgetLongClick$0$FloatingWidgetServices(FloatingWidgetServices.java)
       at com.xxxxxx.dev.floatingwidget.services.-$$Lambda$FloatingWidgetServices$DhV3rKQxnQ9oEEnb70PNAJKR8NQ.onAnimationUpdate(-.java:4)
       at 安卓.animation.ValueAnimator.animateValue(ValueAnimator.java:1558)
       at 安卓.animation.ValueAnimator.animateBasedOnTime(ValueAnimator.java:1349)
       at 安卓.animation.ValueAnimator.doAnimationFrame(ValueAnimator.java:1481)
       at 安卓.animation.AnimationHandler.doAnimationFrame(AnimationHandler.java:146)
       at 安卓.animation.AnimationHandler.access$100(AnimationHandler.java:37)
       at 安卓.animation.AnimationHandler$1.doFrame(AnimationHandler.java:54)
       at 安卓.view.Choreographer$CallbackRecord.run(Choreographer.java:994)
       at 安卓.view.Choreographer.doCallbacks(Choreographer.java:794)
       at 安卓.view.Choreographer.doFrame(Choreographer.java:725)
       at 安卓.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:981)
       at 安卓.os.Handler.handleCallback(Handler.java:883)
       at 安卓.os.Handler.dispatchMessage(Handler.java:100)
       at 安卓.os.Looper.loop(Looper.java:237)
       at 安卓.app.ActivityThread.main(ActivityThread.java:7857)
       at java.lang.reflect.Method.invoke(Method.java)
       at com.安卓.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:493)
       at com.安卓.internal.os.ZygoteInit.main(ZygoteInit.java:1068)

以下是开始:

WindowManager.LayoutParams paramRemove = new WindowManager.LayoutParams(
                WindowManager.LayoutParams.WRAP_CONTENT,
                WindowManager.LayoutParams.WRAP_CONTENT,
                windowType,
                WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE | WindowManager.LayoutParams.FLAG_WATCH_OUTSIDE_TOUCH |
                        WindowManager.LayoutParams.FLAG_LAYOUT_NO_LIMITS,
                PixelFormat.TRANSLUCENT);
        paramRemove.gravity = Gravity.TOP | Gravity.LEFT;

        removeView.setVisibility(View.GONE);
        removeImg = removeView.findViewById(R.id.remove_img);

        windowManager.addView(removeView, paramRemove);

我认为它来自这个函数,但我不确定:

private void widgetLongClick() {
        GlobalFunctions.printLn(">>>>>>>>>>>>Into FloatingWidgetService.chathead_longclick() ");

        WindowManager.LayoutParams param_remove = (WindowManager.LayoutParams) removeView.getLayoutParams();
        int x_cord_remove = (szWindow.x - removeView.getWidth()) / 2;
        int y_cord_remove = szWindow.y - (removeView.getHeight() + getStatusBarHeight());

        param_remove.x = x_cord_remove;
        param_remove.y = y_cord_remove;

        ValueAnimator va = ValueAnimator.ofFloat(szWindow.y, y_cord_remove);
        int mDuration = 100;
        if(removeView.isAttachedToWindow()){
            if(param_remove != null){
                if(windowManager !=null){
                    va.setDuration(mDuration);
                    va.addUpdateListener(animation -> {
                        param_remove.y = Math.round((Float) animation.getAnimatedValue());
                        windowManager.updateViewLayout(removeView, param_remove);
                    });
                    va.start();
                }
            }
        }
    }

共 (2) 个答案

  1. # 1 楼答案

    我从我的问题中找到解决办法, 问题是我正在加载视图之前关闭活动, 所以我所做的是在活动结束前等待1秒钟, 简单地使用:

    new Handler().postDelayed(new Runnable() {
        @Override
        public void run() {
            //close the activity after 1 sec 
        }
    },1000);    
    
  2. # 2 楼答案

    我建议您将这个可移动布局添加到XML文件中,并在需要之前将其设置为View.GONE,这样您就不必担心以编程方式添加它并设置正确的布局参数(这可能是您当前代码中的失败之处)

    一旦你在另一个视图上收到长点击,你可以将可移动布局切换为View.VISIBLE,甚至可以添加一个快速动画,让它看起来对用户更“友好”