美文网首页
Only fullscreen activities can r

Only fullscreen activities can r

作者: 远在远方的风yzyfdf | 来源:发表于2020-03-30 18:08 被阅读0次

    这是个只有8.0才会出现的错误(╯‵□′)╯︵┻━┻

    Entry ent = AttributeCache.instance().get(packageName,realTheme, com.android.internal.R.styleable.Window, userId);
    final boolean translucent = ent != null && (ent.array.getBoolean(com.android.internal.R.styleable.Window_windowIsTranslucent, false)|| (!ent.array.hasValue(
                    com.android.internal.R.styleable.Window_windowIsTranslucent) &&  ent.array.getBoolean(com.android.internal.R.styleable.Window_windowSwipeToDismiss,false)));
    fullscreen = ent != null && !ent.array.getBoolean(com.android.internal.R.styleable.Window_windowIsFloating, false) && !translucent;
    fullscreen = ent != null && !ActivityInfo.isTranslucentOrFloating(ent.array);
    noDisplay = ent != null && ent.array.getBoolean(com.android.internal.R.styleable.Window_windowNoDisplay, false);
    
    if (ActivityInfo.isFixedOrientation(requestedOrientation) && !fullscreen && appInfo.targetSdkVersion > O) {
        throw new IllegalStateException("Only fullscreen activities can request orientation");
    }
    

    简单说就是背景透明的Activity设置固定方向就崩了。。。
    分两种情况
    1.Activity的风格为透明,在manifest文件中指定了一个方向,则在onCreate中crash
    2.Activity的风格为透明,如果调用setRequestedOrientation方法固定方向,则crash

    解决

    AndroidManifest.xml的activity去掉:android:screenOrientation=”portrait”
    在onCreate中用代码设置

           if (Build.VERSION.SDK_INT == Build.VERSION_CODES.O && ScreenOrientationUtil.getInstance().isTranslucentOrFloating(this)) {
              //安卓8.0,透明,不能设置固定方向
           } else {
               setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
           }
    
        //判断是透明的方法
        public boolean isTranslucentOrFloating(Activity activity){
            boolean isTranslucentOrFloating = false;
            try {
                int [] styleableRes = (int[]) Class.forName("com.android.internal.R$styleable").getField("Window").get(null);
                final TypedArray ta = activity.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;
        }
    

    当然,也可以简单粗暴一点,不用判断版本以及是否透明了

            try {
                setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
            } catch (Exception ignored) {
            }
    

    相关文章

      网友评论

          本文标题:Only fullscreen activities can r

          本文链接:https://www.haomeiwen.com/subject/vyavuhtx.html