【安卓】Android兼容性优化-Android 8.0设置Activity透明主题崩溃

原文连接: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)

抛出异常信息:

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;

}

解决方法:

if (Build.VERSION.SDK_INT != Build.VERSION_CODES.O) {

setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);

}

关注我的技术公众号

【安卓】Android兼容性优化-Android 8.0设置Activity透明主题崩溃

以上是 【安卓】Android兼容性优化-Android 8.0设置Activity透明主题崩溃 的全部内容, 来源链接: utcz.com/a/103056.html

回到顶部