java - Android 删除 View 崩溃

标签 java android

我正在创建一个屏幕锁定应用程序,并面临能够在锁定屏幕中向下拖动的通知。

我进行了研究,发现此解决方案可以使用下面的 addView 代码来阻止通知栏,但在用户解锁我的应用程序后我无法删除该 View 。请告知如何删除 View 。

我尝试在 public void onDestroy() { 中使用 manager.removeView(view);,但运行此代码后崩溃了。

这是错误消息

FATAL EXCEPTION: main
Process: com.mehuljoisar.lockscreen, PID: 10256
java.lang.IllegalArgumentException: View=android.view.View{42348cc0 V.ED.... ........ 0,0-0,0} not attached to window manager
at android.view.WindowManagerGlobal.findViewLocked(WindowManagerGlobal.java:373)
at android.view.WindowManagerGlobal.removeView(WindowManagerGlobal.java:302)
at android.view.WindowManagerImpl.removeView(WindowManagerImpl.java:79)
at com.mehuljoisar.lockscreen.LockScreenActivity.disablePullNotificationTouch(LockScreenActivity.java:261)
at com.mehuljoisar.lockscreen.LockScreenActivity.access$100(LockScreenActivity.java:24)
at com.mehuljoisar.lockscreen.LockScreenActivity$2.onClick(LockScreenActivity.java:113)
at android.view.View.performClick(View.java:4469)
at android.view.View$PerformClick.run(View.java:18788)
at android.os.Handler.handleCallback(Handler.java:808)
at android.os.Handler.dispatchMessage(Handler.java:103)
at android.os.Looper.loop(Looper.java:193)
at android.app.ActivityThread.main(ActivityThread.java:5347)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:515)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:835)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:651)
at dalvik.system.NativeStart.main(Native Method)

我应该把removeView代码放在哪里?

private void disablePullNotificationTouch(String gg) {
  WindowManager manager = ((WindowManager)   getApplicationContext().getSystemService(Context.WINDOW_SERVICE));
  WindowManager.LayoutParams localLayoutParams = new WindowManager.LayoutParams();
  localLayoutParams.type = WindowManager.LayoutParams.TYPE_SYSTEM_ERROR;
  localLayoutParams.gravity = Gravity.TOP; //make the line top
  localLayoutParams.flags = WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE |

  // this is to enable the notification to recieve touch events
  WindowManager.LayoutParams.FLAG_NOT_TOUCH_MODAL |
  // Draws over status bar
  WindowManager.LayoutParams.FLAG_LAYOUT_IN_SCREEN;

  localLayoutParams.width = WindowManager.LayoutParams.MATCH_PARENT;
  localLayoutParams.height = (int) (25 *   getResources().getDisplayMetrics().scaledDensity);
  localLayoutParams.format = PixelFormat.RGBX_8888;
  View view = new View(this);
  if(gg == "on") {
    manager.addView(view, localLayoutParams);
  }else {
    manager.removeView(view);
  }
}

@Override
  public void onCreate(Bundle savedInstanceState) {
    disablePullNotificationTouch("on");
  }
@Override
  public void onDestroy() {
    super.onDestroy();
    disablePullNotificationTouch("off");
  }  

最佳答案

你使用

WindowManager manager = ((WindowManager)   this.getSystemService(Context.WINDOW_SERVICE));

不使用

WindowManager manager = ((WindowManager)   getApplicationContext().getSystemService(Context.WINDOW_SERVICE));

试试吧!

关于java - Android 删除 View 崩溃,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36538798/

相关文章:

java - Java中如何检查JsonNode是单个元素还是数组?

java - Android 中 OffsetDateTime.now() 的奇怪行为

android:icon 在某些设备上没有显示

android - TextView 动态宽度与末尾的 View

android - 在帧缓冲区上显示数据

java - BigInteger 内存泄漏导致 Java 中的堆栈溢出

java - 是否有 Android XML 引用?

java - 查找异常类型

java - 通过单击 imageView 打开 Activity

java - 如何用Gson解析响应