【问题标题】:IllegalStateException in Android Studio: Only Fullscreen activities can request orientation [duplicate]Android Studio中的IllegalStateException:只有全屏活动才能请求方向[重复]
【发布时间】:2018-12-18 06:18:24
【问题描述】:

我的应用程序中有一个教程活动,其中包含一堆幻灯片,其中包含有关如何使用该应用程序的一些详细信息。我希望我的整个应用程序仅处于 PORTRAIT 模式,因此我使用以下代码创建了一个自定义 Application 类:

public class DokkanCardsApplication extends Application {
@Override
public void onCreate() {
    super.onCreate();
    registerActivityLifecycleCallbacks(new ActivityLifecycleCallbacks() {
        @Override
        public void onActivityCreated(Activity activity, Bundle bundle) {
            activity.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
        }

        @Override
        public void onActivityStarted(Activity activity) {

        }

        @Override
        public void onActivityResumed(Activity activity) {

        }

        @Override
        public void onActivityPaused(Activity activity) {

        }

        @Override
        public void onActivityStopped(Activity activity) {

        }

        @Override
        public void onActivitySaveInstanceState(Activity activity, Bundle bundle) {

        }

        @Override
        public void onActivityDestroyed(Activity activity) {

        }
    });
}

}

现在,问题是本教程在我第一次安装它时运行良好,但现在它只是崩溃并抛出 IllegalArgumentException 异常说 只有全屏活动可以请求方向强>。经过一番研究,我意识到这是谷歌的错误(如果我错了,请纠正我)但我还没有找到解决方法。异常指向我的自定义 Application 类和我的教程类您可以在下面看到。但是,我还没有发现问题。有什么想法吗?

教程.java:

public class Tutorial extends MaterialIntroActivity {

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    addSlide(new SlideFragmentBuilder()
            .title("Main Screen")
            .description("Search through the entire Dokkan Cards database and find any card you like.")
            .image(R.drawable.image_main)
            .backgroundColor(R.color.mainScreen)
            .buttonsColor(R.color.colorPrimaryGLB)
            .build());


    addSlide(new SlideFragmentBuilder()
            .title("Tap & Long-Press Actions")
            .description("Tap on any card icon to view it's details or Long-press on it to add it to either one of your boxes")
            .image(R.drawable.image_long_tap)
            .backgroundColor(R.color.long_tap)
            .buttonsColor(R.color.colorPrimaryGLB)
            .build());

    addSlide(new SlideFragmentBuilder()
            .title("Side Menu")
            .description("Swipe from left to right to access a handful of menu options")
            .image(R.drawable.image_menu_slide)
            .backgroundColor(R.color.side_menu)
            .buttonsColor(R.color.colorPrimaryGLB)
            .build());

    addSlide(new SlideFragmentBuilder()
            .title("Auto-Save")
            .description("This app saves your box data automatically when you close the app so that you don't have to worry about saving manually")
            .image(R.drawable.image_auto_save)
            .backgroundColor(R.color.auto_save)
            .buttonsColor(R.color.colorPrimaryGLB)
            .build());

    addSlide(new SlideFragmentBuilder()
            .title("Data Security")
            .description("Your data is 100% safe since it cannot be accessed by anyone else.All data are stored locally on your device and not on a server or cloud")
            .image(R.drawable.image_data_security)
            .backgroundColor(R.color.data_security)
            .buttonsColor(R.color.colorPrimaryGLB)
            .build());

    addSlide(new SlideFragmentBuilder()
            .title("Donations")
            .description("Although donations are not mandatory, they help a lot in the app's development process. You can submit your donation via the website tab in the app's side menu.")
            .image(R.drawable.image_donation)
            .backgroundColor(R.color.donations)
            .buttonsColor(R.color.colorPrimaryGLB)
            .build());

   addSlide(new TutorialDisclaimerSlide());

    addSlide(new SlideFragmentBuilder()
            .title("That's it")
            .description("That's the end of this tutorial.You can access it at any time from the top right corner of the app")
            .image(R.drawable.image_tutorial)
            .backgroundColor(R.color.tutorial_end)
            .buttonsColor(R.color.colorPrimaryGLB)
            .build());
}

}

这是完整的例外:

java.lang.RuntimeException: Unable to start activity ComponentInfo{com.dcv.spdesigns.dokkancards/com.dcv.spdesigns.dokkancards.ui.Tutorial}: java.lang.IllegalStateException: Only fullscreen activities can request orientation
    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2955)
    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3030)
    at android.app.ActivityThread.-wrap11(Unknown Source:0)
    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1696)
    at android.os.Handler.dispatchMessage(Handler.java:105)
    at android.os.Looper.loop(Looper.java:164)
    at android.app.ActivityThread.main(ActivityThread.java:6938)
    at java.lang.reflect.Method.invoke(Native Method)
    at com.android.internal.os.Zygote$MethodAndArgsCaller.run(Zygote.java:327)
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1374)
 Caused by: java.lang.IllegalStateException: Only fullscreen activities can request orientation
    at android.os.Parcel.readException(Parcel.java:1966)
    at android.os.Parcel.readException(Parcel.java:1904)
    at android.app.IActivityManager$Stub$Proxy.setRequestedOrientation(IActivityManager.java:6186)
    at android.app.Activity.setRequestedOrientation(Activity.java:5831)
    at com.dcv.spdesigns.dokkancards.presenter.DokkanCardsApplication$1.onActivityCreated(DokkanCardsApplication.java:20)
    at android.app.Application.dispatchActivityCreated(Application.java:221)
    at android.app.Activity.onCreate(Activity.java:1067)
    at android.support.v4.app.SupportActivity.onCreate(ComponentActivity.java:71)
    at android.support.v4.app.FragmentActivity.onCreate(FragmentActivity.java:325)
    at android.support.v7.app.AppCompatActivity.onCreate(AppCompatActivity.java:85)
    at agency.tango.materialintroscreen.MaterialIntroActivity.onCreate(MaterialIntroActivity.java:73)
    at com.dcv.spdesigns.dokkancards.ui.Tutorial.onCreate(Tutorial.java:17)
    at android.app.Activity.performCreate(Activity.java:7174)
    at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1220)
    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2908)

*如果有人感兴趣,这里是我在 Manifest.xml 文件中使用自定义应用程序类的部分:

<application
    android:name=".presenter.DokkanCardsApplication"
    android:allowBackup="true"
    android:fullBackupContent="@xml/backup_descriptor"
    android:icon="@drawable/ic_launcher"
    android:label="@string/app_name"
    android:supportsRtl="true"
    android:theme="@style/AppTheme">

【问题讨论】:

  • 你检查了这个。 stackoverflow.com/q/48787075/6891563
  • @Khemraj ,您的链接进行了一些更改,节省了我的时间。谢谢!
  • @stelios-papamichael 如果该链接对您有帮助,请投票支持该答案!我需要很长时间才能找到正确的解决方案

标签: android exception screen-orientation illegalstateexception


【解决方案1】:

感谢@Khemraj 的link,我设法通过使用以下 if 语句包围 custom Application 类的 onActivityCreated 方法的内容来解决问题:

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

【讨论】:

  • 我认为这不是一个好主意。您刚刚禁用了奥利奥版本设备上所有活动的方向设置。不仅仅是有问题的。
猜你喜欢
  • 2018-07-07
  • 1970-01-01
  • 1970-01-01
  • 2018-04-10
  • 1970-01-01
  • 1970-01-01
  • 2023-03-16
  • 1970-01-01
相关资源
最近更新 更多