java.lang.IllegalStateException: Only fullscreen opaque activities can request orientation

java.lang.IllegalStateException: Only fullscreen opaque activities can request orientation

字面意思就是说:只有不透明的全屏activity可以自主设置界面方向。

这个问题出现在android8.0以上。原因是我们给Activity同时设置了
android:screenOrientation="" 和 true。
解决方法1:在BaseActivit中
@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {

    if (Build.VERSION.SDK_INT == Build.VERSION_CODES.O && isTranslucentOrFloating()) {
        boolean result = fixOrientation();
        Log.e(TAG, "onCreate: "+"onCreate fixOrientation when Oreo, result = " + result );
    }
    super.onCreate(savedInstanceState);

}

private boolean fixOrientation(){
try {
Field field = Activity.class.getDeclaredField(“mActivityInfo”);
field.setAccessible(true);
ActivityInfo o = (ActivityInfo)field.get(this);
o.screenOrientation = -1;
field.setAccessible(false);
return true;
} catch (Exception e) {
e.printStackTrace();
}
return false;
}

@Override
public void setRequestedOrientation(int requestedOrientation) {
    if (Build.VERSION.SDK_INT == Build.VERSION_CODES.O && isTranslucentOrFloating()) {
        Log.e(TAG, "setRequestedOrientation: "+"avoid calling setRequestedOrientation when Oreo." );
        return;
    }
    super.setRequestedOrientation(requestedOrientation);
}

private boolean isTranslucentOrFloating(){
    boolean isTranslucentOrFloating = false;
    try {
        int [] styleableRes = (int[]) Class.forName("com.android.internal.R$styleable").getField("Window").get(null);
        final TypedArray ta = obtainStyledAttributes(styleableRes);
        Method m = ActivityInfo.class.getMethod("isTranslucentOrFloating", TypedArray.class);
        m.setAccessible(true);
        isTranslucentOrFloating = (boolean)m.invoke(null, ta);
        m.setAccessible(false);
    } catch (Exception e) {
        e.printStackTrace();
    }
    return isTranslucentOrFloating;
}

就可以完美解决问题.

解决办法2:删除AndroidManifest中相应Activity的 android:screenOrientation="“属性;
或者删除相应Activity的theme中true属性。
<activity
android:name=“com.zkrj.patient.xindian.ConnectActivity”
android:configChanges="keyboardHidden|orientation"
android:screenOrientation=“portrait”
android:theme=”@style/selectorDialog"/>

< Activity 设置 Dialog >

 <style name="selectorDialog" parent="@android:style/Theme.Dialog">
    <!-- 边框 -->
    <item name="android:windowFrame">@null</item>
    <!-- 是否浮现在activity之上 -->
    <item name="android:windowIsFloating">true</item>
    <!-- 半透明 -->
    <!--<item name="android:windowIsTranslucent">false</item>-->
    <!-- 无标题 -->
    <item name="android:windowNoTitle">true</item>
    <!-- 背景 -->
    <!--    <item name="android:windowBackground">@drawable/selector_dialog_bg</item> -->
    <!--是否模糊-->
    <item name="android:backgroundDimEnabled">true</item>
    <!-- 模糊 -->
    <item name="android:backgroundDimAmount">0.5</item>
</style>
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值