Android错误IllegalArgumentException:添加窗口后无法更改窗口类型

时间:2015-04-12 20:40:55

标签: java android

我正在尝试制作自定义锁定屏幕。所以,我不需要允许用户按主页按钮。在开始我写

public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.lock_screen);

    getWindow().addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON
                        |WindowManager.LayoutParams.FLAG_SHOW_WHEN_LOCKED);

然后我重写 OnKeyDown

 @Override
public boolean onKeyDown(int keyCode, android.view.KeyEvent event) {

    if ((keyCode == KeyEvent.KEYCODE_VOLUME_DOWN)||(keyCode == KeyEvent.KEYCODE_POWER)||(keyCode == KeyEvent.KEYCODE_VOLUME_UP)||(keyCode == KeyEvent.KEYCODE_CAMERA)) {
        //this is where I can do my stuff
        return true; //because I handled the event
    }
    if((keyCode == KeyEvent.KEYCODE_HOME)){

        return true;
    }

    return false;

}

我在这里覆盖 onAttacheToWindow

 @Override
public void onAttachedToWindow() {
    this.getWindow().setType(WindowManager.LayoutParams.TYPE_KEYGUARD_DIALOG);
    super.onAttachedToWindow();
}

但是它给了我错误 IllegalArgumentException:添加窗口后无法更改窗口类型。我的错误在哪里?

如何处理主页按钮?

0 个答案:

没有答案