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

前端 未结 30 1473
难免孤独
难免孤独 2020-12-02 04:43

I am facing the problem while retrieving the contacts from the contact book in Android 8.0 Oreo java.lang.IllegalStateException: Only fullscreen opaque activities can reques

相关标签:
30条回答
  • 2020-12-02 05:36

    I've just removed the tags "portrait" from the non full-screen elements and everything went fine.

    android:screenOrientation="portrait"
    
    0 讨论(0)
  • 2020-12-02 05:37

    Many people have given a fix, so I'll talk about the source of the problem.

    According to the exception log:

    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(SupportActivity.java:66)
        at android.support.v4.app.FragmentActivity.onCreate(FragmentActivity.java:297)
        at android.support.v7.app.AppCompatActivity.onCreate(AppCompatActivity.java:84)
        at com.nut.blehunter.ui.DialogContainerActivity.onCreate(DialogContainerActivity.java:43)
        at android.app.Activity.performCreate(Activity.java:7372)
        at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1218)
        at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:3147)
        at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3302) 
        at android.app.ActivityThread.-wrap12(Unknown Source:0) 
        at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1891) 
        at android.os.Handler.dispatchMessage(Handler.java:108) 
        at android.os.Looper.loop(Looper.java:166)
    

    The code that triggered the exception in Activity.java

        //Need to pay attention mActivityInfo.isFixedOrientation() and ActivityInfo.isTranslucentOrFloating(ta)
        if (getApplicationInfo().targetSdkVersion >= O_MR1 && mActivityInfo.isFixedOrientation()) {
            final TypedArray ta = obtainStyledAttributes(com.android.internal.R.styleable.Window);
            final boolean isTranslucentOrFloating = ActivityInfo.isTranslucentOrFloating(ta);
            ta.recycle();
    
            //Exception occurred
            if (isTranslucentOrFloating) {
                throw new IllegalStateException(
                        "Only fullscreen opaque activities can request orientation");
            }
        }
    

    mActivityInfo.isFixedOrientation():

            /**
            * 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 portrait.
            * @hide
            */
            boolean isFixedOrientationPortrait() {
                return isFixedOrientationPortrait(screenOrientation);
            }
    
            /**
            * 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;
            }
    
            /**
            * 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;
            }
    

    According to the above code analysis, when TargetSdkVersion>=27, when using SCREEN_ORIENTATION_LANDSCAPE, SCREEN_ORIENTATION_PORTRAIT, and other related attributes, the use of windowIsTranslucent, windowIsFloating, and windowSwipeToDismiss topic attributes will trigger an exception.

    After the problem is found, you can change the TargetSdkVersion or remove the related attributes of the theme according to your needs.

    0 讨论(0)
  • 2020-12-02 05:37

    I resolved this issue by removing android:screenOrientation="portrait" and added below code into my onCreate

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

    while my theme properties are

    <item name="android:windowIsTranslucent">true</item>
    <item name="android:windowDisablePreview">true</item>
    
    0 讨论(0)
  • 2020-12-02 05:38

    After doing some research, it seems that this problem may be due to a google bug. For me, I was able to leave this line in my Activities onCreate method:

    setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
    

    AND I changed my targetSdkVersion to 26. Having that line in my onCreate still resulted in a crash while my targetSdkVersion was still set at 27. Since no one else's solution has worked for me thus far, I found that this works as a temporary fix for now.

    0 讨论(0)
  • 2020-12-02 05:38

    Use

    android:screenOrientation="behind"
    

    And Theme

    <style name="translucent" parent="Theme.AppCompat.Light.NoActionBar">
        <item name="android:windowBackground">#beaaaaaa</item>
        <item name="android:windowIsTranslucent">true</item>
        <item name="android:windowNoTitle">true</item>
        <item name="android:windowAnimationStyle">@android:style/Animation</item>
        <item name="android:typeface">normal</item>
        <item name="android:windowSoftInputMode">adjustPan</item>
        <item name="windowActionBar">false</item>
    </style>
    
    0 讨论(0)
  • 2020-12-02 05:38

    Probably you showing Activity looking like Dialog(non-fullscreen), so remove screenOrientation from Manifest or from code. This will fix the issue.

    0 讨论(0)
提交回复
热议问题