关于android:Android兼容性优化Android-80设置Activity透明主题崩溃

80次阅读

共计 3241 个字符,预计需要花费 9 分钟才能阅读完成。

原文连贯:https://mp.weixin.qq.com/s/g6…

解体日志:

1 java.lang.RuntimeException:Unable to start activity ComponentInfo{com.pxwx.assistant/com.pxwx.main.ui.MainActivity}: java.lang.IllegalStateException: Only fullscreen opaque activities can request orientation
2 android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2957)
3 ......
4 Caused by:
5 java.lang.IllegalStateException:Only fullscreen opaque activities can request orientation
6 android.app.Activity.onCreate(Activity.java:1038)
7 androidx.core.app.ComponentActivity.onCreate(Unknown Source:0)
8 androidx.activity.ComponentActivity.onCreate(Unknown Source:0)
9 androidx.fragment.app.FragmentActivity.onCreate(Unknown Source:99)
10 androidx.appcompat.app.AppCompatActivity.onCreate(Unknown Source:10)
11 com.pxwx.base.activity.BaseActivity.onCreate(Unknown Source:0)
12 com.pxwx.main.ui.MainActivity.onCreate(Native Method)
13 android.app.Activity.performCreate(Activity.java:7183)
14 android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1221)
15 android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2910)
16 android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3032)
17 android.app.ActivityThread.-wrap11(Unknown Source:0)
18 android.app.ActivityThread$H.handleMessage(ActivityThread.java:1696)
19 android.os.Handler.dispatchMessage(Handler.java:105)
20 android.os.Looper.loop(Looper.java:164)
21 android.app.ActivityThread.main(ActivityThread.java:6942)
22 java.lang.reflect.Method.invoke(Native Method)
23 com.android.internal.os.Zygote$MethodAndArgsCaller.run(Zygote.java:327)
24 com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1374)

抛出异样信息:

Only fullscreen opaque activities can request orientation

Android8.0 源码 Activity.java:

https://www.androidos.net.cn/…


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();
            // 如果 Activity 方向固定并且是通明 (或者浮动),则会抛异样。if (isTranslucentOrFloating) {
                throw new IllegalStateException("Only fullscreen opaque activities can request orientation");
            }
        }
        ...
    }

查看 ActivityInfo.java 的 isTranslucentOrFloating() 办法:
https://www.androidos.net.cn/…

// 查看 Activity 形式是否是通明或者是浮动的
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;
    }

解决办法:

1、降级 targetSDKVersion 到 26 以下 (废话!!)

2、移除 mainfest 文件里的 screenOrientation 属性

3、勾销 Activity 主题里的 windowIsTranslucent 属性或者 windowSwipeToDismiss 属性或者 windowIsFloating 属性(依据你设置了什么属性来具体分析)

==4、移除 manifest 文件里的 screenOrientation 属性,并在 Activity 的 onCreate 办法里设置屏幕方向 ==

if (Build.VERSION.SDK_INT != Build.VERSION_CODES.O) {setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
}

关注我的技术公众号

正文完
 0