活动未附加到windows管理器



我有一个浮动小部件(覆盖所有应用程序(,长时间单击(按住鼠标(会从底部显示相对布局(remove_relativelayout(,这样用户就可以关闭小部件,服务就会被破坏。

在大多数三星手机上,我都会遇到这种崩溃:

Fatal Exception: java.lang.IllegalArgumentException: View=android.widget.RelativeLayout{762db5 G.E...... ......ID 0,0-160,160 #7f09022d app:id/remove_relativelayout} not attached to window manager
at android.view.WindowManagerGlobal.findViewLocked(WindowManagerGlobal.java:569)
at android.view.WindowManagerGlobal.updateViewLayout(WindowManagerGlobal.java:463)
at android.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 android.animation.ValueAnimator.animateValue(ValueAnimator.java:1558)
at android.animation.ValueAnimator.animateBasedOnTime(ValueAnimator.java:1349)
at android.animation.ValueAnimator.doAnimationFrame(ValueAnimator.java:1481)
at android.animation.AnimationHandler.doAnimationFrame(AnimationHandler.java:146)
at android.animation.AnimationHandler.access$100(AnimationHandler.java:37)
at android.animation.AnimationHandler$1.doFrame(AnimationHandler.java:54)
at android.view.Choreographer$CallbackRecord.run(Choreographer.java:994)
at android.view.Choreographer.doCallbacks(Choreographer.java:794)
at android.view.Choreographer.doFrame(Choreographer.java:725)
at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:981)
at android.os.Handler.handleCallback(Handler.java:883)
at android.os.Handler.dispatchMessage(Handler.java:100)
at android.os.Looper.loop(Looper.java:237)
at android.app.ActivityThread.main(ActivityThread.java:7857)
at java.lang.reflect.Method.invoke(Method.java)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:493)
at com.android.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();
}
}
}
}

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

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

我找到了问题的解决方案,问题是我在加载视图之前关闭了活动,所以我所做的是在关闭活动之前等待1秒,简单使用:

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

最新更新