Android 8.0 Activity启动崩溃异常:"Only fullscreen opaque activities can request orientation"

今天测试突然提出 打开某某页面崩溃,怎么可能,我的代码怎么会有这么到的bug呢,抓紧找出信息如下

崩溃机型:华为荣耀手机
系统版本:Android8.0

日志如下:

java.lang.RuntimeException:  Unable  to  start  activity  ComponentInfo{com.cloudy.linglingbang.test/com.cloudy.linglingbang.activity.fm.FmCommentListActivity}:  java.lang.IllegalStateException:  Only  fullscreen  opaque  activities  can  request  orientation
at  android.app.ActivityThread.performLaunchActivity(ActivityThread.java:3303)
at  android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3411)
at  android.app.ActivityThread.-wrap12(Unknown  Source:0)
at  android.app.ActivityThread$H.handleMessage(ActivityThread.java:1994)
at  android.os.Handler.dispatchMessage(Handler.java:108)
at  android.os.Looper.loop(Looper.java:166)
at  android.app.ActivityThread.main(ActivityThread.java:7529)
at  java.lang.reflect.Method.invoke(Native  Method)
at  com.android.internal.os.Zygote$MethodAndArgsCaller.run(Zygote.java:245)
at  com.android.internal.os.ZygoteInit.main(ZygoteInit.java:921)
Caused  by:  java.lang.IllegalStateException:  Only  fullscreen  opaque  activities  can  request  orientation
at  android.app.Activity.onCreate(Activity.java:1081)
at  android.support.v4.app.SupportActivity.onCreate(ComponentActivity.java:75)
at  android.support.v4.app.FragmentActivity.onCreate(FragmentActivity.java:335)
at  android.support.v7.app.AppCompatActivity.onCreate(AppCompatActivity.java:85)
at  com.cloudy.linglingbang.activity.basic.BaseActivity.onCreate(BaseActivity.java:112)
at  com.cloudy.linglingbang.activity.fm.FmCommentListActivity.onCreate(FmCommentListActivity.java:32)
at  android.app.Activity.performCreate(Activity.java:7383)
at  android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1218)
at  android.app.ActivityThread.performLaunchActivity(ActivityThread.java:3256)
...  9  more
java.lang.IllegalStateException:  Only  fullscreen  opaque  activities  can  request  orientation
at  android.app.Activity.onCreate(Activity.java:1081)
at  android.support.v4.app.SupportActivity.onCreate(ComponentActivity.java:75)
at  android.support.v4.app.FragmentActivity.onCreate(FragmentActivity.java:335)
at  android.support.v7.app.AppCompatActivity.onCreate(AppCompatActivity.java:85)
at  com.cl

通过排查,代码没问题。
于是查找源码
Activity.java(API:26)

 @MainThread
    @CallSuper
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        if (DEBUG_LIFECYCLE) Slog.v(TAG, "onCreate " + this + ": " + savedInstanceState);

        if (getApplicationInfo().targetSdkVersion > O && mActivityInfo.isFixedOrientation()) {
            final TypedArray ta = obtainStyledAttributes(com.android.internal.R.styleable.Window);
            final boolean isTranslucentOrFloating = ActivityInfo.isTranslucentOrFloating(ta);
            ta.recycle();

            if (isTranslucentOrFloating) {
                throw new IllegalStateException(
                        "Only fullscreen opaque activities can request orientation");
            }
        }
        ....省略
    }

通过代码对比,只有在Android8.0机型上会抛该异常;

继续排查 ActivityInfo源码

     /**
     * Returns true if the activity's orientation is fixed.
     * @hide
     */
     public boolean isFixedOrientation() {
        return isFixedOrientationLandscape() || isFixedOrientationPortrait()
                || screenOrientation == SCREEN_ORIENTATION_LOCKED;
    }

 /**
     * Returns true if the activity's orientation is fixed to landscape.
     * @hide
     */
    boolean isFixedOrientationLandscape() {
        return isFixedOrientationLandscape(screenOrientation);
    }

    /**
     * Returns true if the activity's orientation is fixed to landscape.
     * @hide
     */
    public static boolean isFixedOrientationLandscape(@ScreenOrientation int orientation) {
        return orientation == SCREEN_ORIENTATION_LANDSCAPE
                || orientation == SCREEN_ORIENTATION_SENSOR_LANDSCAPE
                || orientation == SCREEN_ORIENTATION_REVERSE_LANDSCAPE
                || orientation == SCREEN_ORIENTATION_USER_LANDSCAPE;
    }

    /**
     * Returns true if the activity's orientation is fixed to portrait.
     * @hide
     */
    public static boolean isFixedOrientationPortrait(@ScreenOrientation int orientation) {
        return orientation == SCREEN_ORIENTATION_PORTRAIT
                || orientation == SCREEN_ORIENTATION_SENSOR_PORTRAIT
                || orientation == SCREEN_ORIENTATION_REVERSE_PORTRAIT
                || orientation == SCREEN_ORIENTATION_USER_PORTRAIT;
    }

发现是检查Activity方式是否是固定的

继续分析

  /**
     * Determines whether the {@link Activity} is considered translucent or floating.
     * @hide
     */
    public static boolean isTranslucentOrFloating(TypedArray attributes) {
        final boolean isTranslucent =
                attributes.getBoolean(com.android.internal.R.styleable.Window_windowIsTranslucent,
                        false);
        final boolean isSwipeToDismiss = !attributes.hasValue(
                com.android.internal.R.styleable.Window_windowIsTranslucent)
                && attributes.getBoolean(
                        com.android.internal.R.styleable.Window_windowSwipeToDismiss, false);
        final boolean isFloating =
                attributes.getBoolean(com.android.internal.R.styleable.Window_windowIsFloating,
                        false);

        return isFloating || isTranslucent || isSwipeToDismiss;
    }

发现是检查Activity方式是否是透明或者是浮动的

如果是Activity方向固定并且是透明(或者浮动),则会抛异常。

修复方法:
1、Activity 注册声明中去除android:screenOrientation属性或者修改为 android:screenOrientation=“behind”。
2、针对API为26时,设置背景为不透明或者不固定。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值