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

时间:2015-06-26 20:52:45

标签: android android-activity android-windowmanager

我已经尝试了建议here,建议here,建议here,我在我的基本活动中注释了onAttachedToWindow()。我有两个活动继承自这个类BaseActivity。一个运行,一个不运行。有什么区别?我的目标SDK是19;将它改为12没有区别。这是我的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);

    ....
    }

导航到第二个活动时,单步执行代码,它会通过onCreate(),onResume(),然后崩溃。

可能是什么问题?

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)

编辑:如果我更改为目标API 12并将所有更改从onCreate添加到onAttachedToWindow,我可以让此异常消失,但我和#39; d将目标SDK设为19。

我的 onCreate():

@Override
protected void onCreate(Bundle savedInstanceState) {

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

我的 onAttachedToWindow():

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

这仅适用于目标API 12.

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

5 个答案:

答案 0 :(得分:6)

尝试将此用于窗口:

<video id="my-video-id" autoplay></video>

答案 1 :(得分:2)

我遇到了同样的问题。但是在停止添加

之后
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);
getWindow().setType(WindowManager.LayoutParams.TYPE_KEYGUARD);
requestWindowFeature(Window.FEATURE_NO_TITLE);
super.onCreate(savedInstanceState); BaseActivity之前的

,效果很好。我在super.onCreate(savedInstanceState);下降的BaseActivity之前剪切并粘贴了上面的代码。

顺便说一句,我不认为你必须在super.onAttachedToWindow();的生活圈子中调用Activity。因为当您覆盖onAttachedToWindow();时视图附加到窗口时会调用View

答案 2 :(得分:1)

我也有这个问题,但是我通过删除Window,更改params然后再添加窗口来解决它。这对我来说已经足够了。

POST your_index/_close

答案 3 :(得分:0)

很抱歉迟到的回复。你应该添加:

    location /app-php7 {
            fastcgi_split_path_info ^(.+\.php)(/.+)$;
            # NOTE: You should have "cgi.fix_pathinfo = 0;" in php.ini
            fastcgi_pass unix:/var/run/php/php7.0-fpm.sock;
            fastcgi_index index.php;
            include fastcgi_params;
            fastcgi_param   APPLICATION_ENV local;

            fastcgi_read_timeout 600;
            fastcgi_connect_timeout 600;
    }

    location ~ \.php {
            fastcgi_split_path_info ^(.+\.php)(/.+)$;
            # NOTE: You should have "cgi.fix_pathinfo = 0;" in php.ini
            fastcgi_pass unix:/var/run/php5-fpm.sock;
            fastcgi_index index.php;
            include fastcgi_params;
            fastcgi_param   APPLICATION_ENV local;
            fastcgi_read_timeout 600;
            fastcgi_connect_timeout 600;
    }
如果您使用的是api等级15或以上,请在 onCreate 方法中

。它对我有用。

答案 4 :(得分:0)

删除

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

来自您的onAttachedToWindow(),就像这样:

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

          super.onAttachedToWindow();
     }