美文网首页Android 进击
Android 8.0 Activity启动崩溃异常:"Only

Android 8.0 Activity启动崩溃异常:"Only

作者: 小程序猿一枚 | 来源:发表于2020-04-08 11:02 被阅读0次

    今天测试突然提出 打开某某页面崩溃,怎么可能,我的代码怎么会有这么到的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时,设置背景为不透明或者不固定。

    相关文章

      网友评论

        本文标题:Android 8.0 Activity启动崩溃异常:"Only

        本文链接:https://www.haomeiwen.com/subject/tobzphtx.html