异常处理 Only fullscreen opaque activities can request orientation

网上基本解决方案是

1、修改Activity的style中windowIsTranslucent为false

2、删除AndroidManifest中对应Activity的screenOrientation配置

 

异常源码:

Activity

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");
            }
        }

。。。
}

 ActivityInfo

public boolean isFixedOrientation() {
        return isFixedOrientationLandscape() || isFixedOrientationPortrait()
                || screenOrientation == SCREEN_ORIENTATION_LOCKED;
    }
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中调用setRequestedOrientation方法,同样出现异常

 

异常信息:

2018-11-28 15:23:09.431 9567-9567/com.feiteng.lieyou E/lieyouexception: java.lang.IllegalStateException: Only fullscreen activities can request orientation
    	android.os.Parcel.readException(Parcel.java:1951)
    	android.os.Parcel.readException(Parcel.java:1889)
    	android.app.IActivityManager$Stub$Proxy.setRequestedOrientation(IActivityManager.java:5775)
    	android.app.Activity.setRequestedOrientation(Activity.java:5803)

 

异常源码:

ActivityManagerService

@Override
    public int getRequestedOrientation(IBinder token) {
        synchronized (this) {
            ActivityRecord r = ActivityRecord.isInStackLocked(token);
            if (r == null) {
                return ActivityInfo.SCREEN_ORIENTATION_UNSPECIFIED;
            }
            return r.getRequestedOrientation();
        }
    }

 ActivityRecord

void setRequestedOrientation(int requestedOrientation) {
        if (ActivityInfo.isFixedOrientation(requestedOrientation) && !fullscreen
                && appInfo.targetSdkVersion > O) {
            throw new IllegalStateException("Only fullscreen activities can request orientation");
        }

        final int displayId = getDisplayId();
        final Configuration displayConfig =
                mStackSupervisor.getDisplayOverrideConfiguration(displayId);

        final Configuration config = mWindowContainerController.setOrientation(requestedOrientation,
                displayId, displayConfig, mayFreezeScreenLocked(app));
        if (config != null) {
            frozenBeforeDestroy = true;
            if (!service.updateDisplayOverrideConfigurationLocked(config, this,
                    false /* deferResume */, displayId)) {
                mStackSupervisor.resumeFocusedStackTopActivityLocked();
            }
        }
        service.mTaskChangeNotificationController.notifyActivityRequestedOrientationChanged(
                task.taskId, requestedOrientation);
    }

这样只能用最后一个方案,就是修改targetSdkVersion为26,然后compileSdkVersion 和 buildToolsVersion为27或者更高版本

你可能感兴趣的:(Android)