在后台打开app一段时间后崩溃

时间:2016-12-26 20:19:51

标签: android android-fragments

这是我一直试图解决的问题。在后台打开我的应用程序时(我假设被杀死以使用其资源),我得到了这个崩溃:

java.lang.RuntimeException: Unable to start activity ComponentInfo{package_path/package_path.activities.MainActivity}: java.lang.IllegalArgumentException: Wrong state class, expecting View State but received class android.os.Bundle instead. This usually happens when two views of different type have the same id in the same hierarchy. This view's id is id/nav_watching_queue. Make sure other views do not use the same id.
    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2665)
    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2726)
    at android.app.ActivityThread.-wrap12(ActivityThread.java)
    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1477)
    at android.os.Handler.dispatchMessage(Handler.java:102)
    at android.os.Looper.loop(Looper.java:154)
    at android.app.ActivityThread.main(ActivityThread.java:6119)
    at java.lang.reflect.Method.invoke(Native Method)
    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:886)
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:776)
Caused by: java.lang.IllegalArgumentException: Wrong state class, expecting View State but received class android.os.Bundle instead. This usually happens when two views of different type have the same id in the same hierarchy. This view's id is id/nav_watching_queue. Make sure other views do not use the same id.
    at android.view.View.onRestoreInstanceState(View.java:15776)
    at com.roughike.bottombar.BottomBarTab.onRestoreInstanceState(BottomBarTab.java:555)
    at android.view.View.dispatchRestoreInstanceState(View.java:15751)
    at android.view.ViewGroup.dispatchRestoreInstanceState(ViewGroup.java:3231)
    at android.view.ViewGroup.dispatchRestoreInstanceState(ViewGroup.java:3237)
    at android.view.ViewGroup.dispatchRestoreInstanceState(ViewGroup.java:3237)
    at android.view.ViewGroup.dispatchRestoreInstanceState(ViewGroup.java:3237)
    at android.view.ViewGroup.dispatchRestoreInstanceState(ViewGroup.java:3237)
    at android.view.ViewGroup.dispatchRestoreInstanceState(ViewGroup.java:3237)
    at android.view.ViewGroup.dispatchRestoreInstanceState(ViewGroup.java:3237)
    at android.view.ViewGroup.dispatchRestoreInstanceState(ViewGroup.java:3237)
    at android.view.View.restoreHierarchyState(View.java:15729)
    at com.android.internal.policy.PhoneWindow.restoreHierarchyState(PhoneWindow.java:2106)
    at android.app.Activity.onRestoreInstanceState(Activity.java:1047)
    at android.app.Activity.performRestoreInstanceState(Activity.java:1002)
    at android.app.Instrumentation.callActivityOnRestoreInstanceState(Instrumentation.java:1174)
    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2638)
    ... 9 more

我已检查过,只有一个ID为nav_watching_queue的视图。知道在这种情况下我的活动正在被重新创建,我的onCreate()逻辑会检查片段管理器中是否存在预先存在的片段,然后再创建一个新片段:

if (App.getInstance().isInitializing()) {
        SeriesFragment seriesFragment;

        if (SharedPrefsHelper.getInstance().isLoggedIn()) {
            seriesFragment = UserListFragment.newInstance();
        } else {
            seriesFragment = SeasonsFragment.newInstance();
            defaultTabId = R.id.nav_seasons;
        }

        SenpaiExportHelper senpaiExportHelper = new SenpaiExportHelper(seriesFragment);
        senpaiExportHelper.getSeasonList();

        startFragment(seriesFragment);
    } else {
        if (getCurrentFragment() == null) {
            if (getIntent() != null && getIntent().hasExtra("notificationClicked")) {
                defaultTabId = R.id.nav_watching_queue;
                startFragment(BacklogFragment.newInstance());
            } else {
                startFragment(UserListFragment.newInstance());
            }
        }
    }
private Fragment getCurrentFragment() {
    if (getSupportFragmentManager().getFragments() != null && !getSupportFragmentManager().getFragments().isEmpty()) {
        Iterator iterator = getSupportFragmentManager().getFragments().iterator();
        Fragment fragment = (Fragment) iterator.next();
        Fragment previousFragment = fragment;

        while (iterator.hasNext()) {
            fragment = (Fragment) iterator.next();

            if (fragment == null) {
                fragment = previousFragment;
            } else {
                previousFragment = fragment;
            }
        }

        return fragment;
    } else {
        return null;
    }
}

我也无法可靠地重新创建这种情况。我在模拟器的设置中启用了“不要保持活动”,但重新打开应用程序时我没有遇到此异常。

0 个答案:

没有答案