Android(安卓)8.0 Activity启动崩溃异常:"Only fullscreen opaque activities can request orientation"
16lz
2022-05-14
今天测试突然提出 打开某某页面崩溃,怎么可能,我的代码怎么会有这么到的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 orientationat 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 orientationat 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 morejava.lang.IllegalStateException: Only fullscreen opaque activities can request orientationat 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(安卓)px和dip及sp的区别及转换代码
- android从相册中选择报无法加载照片
- Android(安卓)抢购类倒计时实现 (包括天数,小时,分钟,秒)
- Android+JNI调用–文件操作
- 【android】listview动态更新(与数据库之间的交道)
- 必须知道的Android屏幕自适应解决方案
- android音乐文件乱码显示问题解决
- 基于TCP/IP协议的Java服务端与Android客户端的Socket通信及数据
- android截图代码