IllegalArgumentException: Window type can not be changed after the window is added

空扰寡人 提交于 2019-11-30 16:27:38

问题


I've tried the advice here, the advice here, the advice here, I've commented out the onAttachedToWindow() in my Base Activity. I have two Activities inheriting from this class, BaseActivity. One runs, and one does not. What could be the difference? My target SDK is 19; changing it to 12 makes no difference. Here is my onCreate for BaseActivity:

    @Override
    protected void onCreate(Bundle savedInstanceState) {
    getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);
    this.getWindow().setType(WindowManager.LayoutParams.TYPE_KEYGUARD);
    requestWindowFeature(Window.FEATURE_NO_TITLE);
    super.onAttachedToWindow();
    super.onCreate(savedInstanceState);

    ....
    }

When navigating to the second activity, stepping through the code, it makes it through onCreate(), onResume(), then crashes.

What could be the problem?

Stacktrace:

06-26 13:41:57.963  28667-28667/com.assistek.ediary E/AndroidRuntime﹕ FATAL EXCEPTION: main
Process: com.assistek.ediary, PID: 28667
java.lang.IllegalArgumentException: Window type can not be changed after the window is added.
        at android.os.Parcel.readException(Parcel.java:1550)
        at android.os.Parcel.readException(Parcel.java:1499)
        at android.view.IWindowSession$Stub$Proxy.relayout(IWindowSession.java:903)
        at android.view.ViewRootImpl.relayoutWindow(ViewRootImpl.java:5301)
        at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:1507)
        at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:1061)
        at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:5885)
        at android.view.Choreographer$CallbackRecord.run(Choreographer.java:767)
        at android.view.Choreographer.doCallbacks(Choreographer.java:580)
        at android.view.Choreographer.doFrame(Choreographer.java:550)
        at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:753)
        at android.os.Handler.handleCallback(Handler.java:739)
        at android.os.Handler.dispatchMessage(Handler.java:95)
        at android.os.Looper.loop(Looper.java:135)
        at android.app.ActivityThread.main(ActivityThread.java:5254)
        at java.lang.reflect.Method.invoke(Native Method)
        at java.lang.reflect.Method.invoke(Method.java:372)
        at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:903)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:698)

EDIT: if I change to target API 12 and put all of the changes in from onCreate into onAttachedToWindow, I can get this exception to go away, but I'd like the target SDK to be 19.

My new onCreate():

@Override
protected void onCreate(Bundle savedInstanceState) {

    requestWindowFeature(Window.FEATURE_NO_TITLE);
    super.onCreate(savedInstanceState);
...
}

My new onAttachedToWindow():

@Override
public void onAttachedToWindow() {
    getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);
    this.getWindow().setType(WindowManager.LayoutParams.TYPE_KEYGUARD);
    super.onAttachedToWindow();
}

This only works with target API 12.

Answer Min target must be less than 14 when WindowManager.LayoutParams.TYPE_KEYGUARD used


回答1:


Try to use this for window :

requestWindowFeature(Window.FEATURE_NO_TITLE);
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
                     WindowManager.LayoutParams.FLAG_FULLSCREEN);
setContentView(R.layout.activity_adjectives);



回答2:


I run into the same question. But after stopping adding

getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);
getWindow().setType(WindowManager.LayoutParams.TYPE_KEYGUARD);
requestWindowFeature(Window.FEATURE_NO_TITLE);

before super.onCreate(savedInstanceState); of BaseActivity, it works well. I cut and paste above codes before super.onCreate(savedInstanceState); of descends of BaseActivity.

BTW, I don't think you have to call super.onAttachedToWindow(); inside methods of life circle of Activity. Because onAttachedToWindow(); is called when the view is attached to a window when you overriding a View.




回答3:


I also had this problem, but I solved it by removing the Window, changing the params and then adding the window again. This was good enough for me.

WindowManager.removeView(View); params = params2; //changed the params to something else WindowManager.updateViewLayout(View, params); WindowManager.addView(View, params);




回答4:


Remove

this.getWindow().setType(WindowManager.LayoutParams.TYPE_KEYGUARD_DIALOG|WindowManager.LayoutParams.FLAG_FULLSCREEN);

From your onAttachedToWindow(), like this:

 @Override
     public void onAttachedToWindow() {
        //this.getWindow().setType(WindowManager.LayoutParams.TYPE_KEYGUARD_DIALOG|WindowManager.LayoutParams.FLAG_FULLSCREEN);

          super.onAttachedToWindow();
     }



回答5:


Sorry for late reply. You should add:

requestWindowFeature(Window.FEATURE_NO_TITLE);
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
WindowManager.LayoutParams.FLAG_FULLSCREEN);

in onCreate method if you are using api level 15 or above. It works for me.



来源:https://stackoverflow.com/questions/31081968/illegalargumentexception-window-type-can-not-be-changed-after-the-window-is-add

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!