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

5

23

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

Misstate answered 26/6, 2015 at 20:52 Comment(5)
try this for API 19 : developer.android.com/training/system-ui/immersive.htmlNeurogram
Do not call super.onAttachedToWindow() anywhere other than from inside your override of onAttachedToWindow(), if you have one. While I cannot speak to TYPE_KEYGUARD, the rest of that code should be after super.onCreate(), but before any setContentView() call. Beyond that, see if you have anything interesting in your manifest that might explain the difference between your two activities.Joerg
The only thing that's different between the two activities is: android:clearTaskOnLaunch="true"Misstate
I did also move everything back to onAttachedToWindow.Misstate
someone mentioned allready taht the super.onCretae() should be called at the beginning of onCreate(), so try this. And also you dont need to call the onAttachedWindow() method!Isometrics
G
7

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);
Girlhood answered 7/7, 2015 at 5:1 Comment(1)
This will probably be the answer that gets awarded the bounty, but this answer DID NOT WORK.Misstate
D
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.

Difficile answered 7/7, 2015 at 8:39 Comment(1)
getWindow().setType(WindowManager.LayoutParams.TYPE_KEYGUARD); TYPE_KEYGUARD is deprecated.Misstate
S
2

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

Stereophotography answered 20/1, 2016 at 22:31 Comment(1)
This was the only thing that worked for me in an AccessibilityService overlay. But the updateViewLayout isn't necessary since the view has already been removed from the window and will be re-added in the next line.Maggy
A
1

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();
     }
Adorn answered 14/2, 2018 at 19:14 Comment(0)
H
0

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.

Holdback answered 15/6, 2016 at 8:35 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.