Android removeView崩溃了

时间:2016-04-11 02:16:49

标签: java android

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

我正在研究它并发现此解决方案通过使用下面的addView代码来阻止通知栏,但我无法在用户解锁我的应用后删除该视图。请建议如何删除视图。

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

这是错误消息

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");
  }  

1 个答案:

答案 0 :(得分:1)

您使用

import UIKit

class CustomCell: UITableViewCell {

    @IBOutlet var photo: UIImageView!
    @IBOutlet var name: UILabel!
    @IBOutlet var detail: UILabel!

    override func awakeFromNib() {
        super.awakeFromNib()
        // Initialization code
    }

    override func setSelected(selected: Bool, animated: Bool) {
        super.setSelected(selected, animated: animated)

        // Configure the view for the selected state
    }

}

不使用

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

试试吧!