Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

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

like image 691
Kristy Welsh Avatar asked Jun 26 '15 20:06

Kristy Welsh


2 Answers

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);
like image 197
parik dhakan Avatar answered Sep 20 '22 15:09

parik dhakan


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.

like image 42
SilentKnight Avatar answered Sep 16 '22 15:09

SilentKnight