【问题标题】:Android removeView crashedAndroid removeView 崩溃
【发布时间】:2016-04-11 02:16:49
【问题描述】:

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

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

我尝试在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");
  }  

【问题讨论】:

标签: java android


【解决方案1】:

你使用

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

不使用

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

试试吧!

【讨论】:

  • 感谢您的回答,但删除视图后它仍然崩溃。我再次编辑问题,这就是我删除视图的方式。
  • 是的。希望对你有帮助?
  • 啊,刚刚意识到,如果我使用 WindowManager manager = ((WindowManager) this.getSystemService(Context.WINDOW_SERVICE));通知栏将能够拖动..
猜你喜欢
  • 1970-01-01
  • 2018-02-07
  • 2012-07-07
  • 2012-12-27
  • 2012-06-05
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多