2

I'm loading some fragments in a container, which holds a Navigation Drawer too. When rotating the screen, and then rotating it back, the app crashes:

java.lang.RuntimeException: Unable to start activity ComponentInfo{lu.gian.uniwhere.alfa/com.example.alfa.ActivityHome}: java.lang.IllegalArgumentException: No view found for id 0x7f0b004e (com.example.alfa:id/taxes_pager) for fragment FragmentTaxToBePaid{b5969898 #2 id=0x7f0b004e}

I got this error when rotating back to portrait mode, after going landscape. What's fun is that I'm not loading FragmentTaxes (that is, N.B.., a ViewPager), but totally another fragment (FragmentOverwiew - nothing special to note about it)!

It throws the exactly same error when rotating back from FragmentSession, which is a ViewPager holder too:

java.lang.RuntimeException: Unable to start activity ComponentInfo{lu.gian.uniwhere.alfa/com.example.alfa.ActivityHome}: java.lang.IllegalArgumentException: No view found for id 0x7f0b000f (com.example.alfa:id/session_pager) for fragment FragmentSessionApplied{b5afa300 #1 id=0x7f0b000f}

I've tried everything out there (not retaining instances of the fragments, putting them in the back stack, replacing Frame xml tag with FrameLayout, creating a specific layout for then landscape mode, etc, etc), but hardly anything sorted an effect. I think it's due to fragments lifecycle, because crashes are inconstant and difficult to debug, but it's quite hard to understand how to fix this sort of problem.

ActivityHome

public void selectItem(int position) {
        // ...
    FragmentManager fragmentManager = getSupportFragmentManager();
    fragmentManager.beginTransaction()
                    .addToBackStack(null)
                    .replace(R.id.content_frame, fragment)
                    .commit();
    mDrawerList.setItemChecked(position, true);
    setTitle(mDrawerTitles[position]);
    mDrawerLayout.closeDrawer(mDrawerList);
}

activity_home

<android.support.v4.widget.DrawerLayout
    ... >

    <FrameLayout
        android:id="@+id/content_frame"
        android:layout_width="match_parent"
        android:layout_height="match_parent" />


    <!-- The navigation drawer  .... -->

</android.support.v4.widget.DrawerLayout>

FragmentTaxes

    View rootView = inflater.inflate(R.layout.fragment_taxes, container, false);

fragment_taxes

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
    ... >

    <!-- Loader -->
    <include
        android:id="@+id/taxes_main_progress"
        layout="@layout/indeterminate_activity_bar" />

    <android.support.v4.view.ViewPager
        android:visibility="gone"
        android:id="@+id/taxes_pager"
        ... >

        <android.support.v4.view.PagerTabStrip
            android:id="@+id/taxes_pager_tab_strip"
            ... />
    </android.support.v4.view.ViewPager>

</RelativeLayout>

If needed I will post more snippets of code. Now I don't, I think it can result confusing for reading.

4

1 回答 1

8

都是因为setRetainInstance(true)。我认为在两个 ViewPager 中将其设置为 false 就足够了。实际上不是。我将它从单个(嵌套)片段中删除,一切顺利。

看起来嵌套片段无法处理setRetainInstance。从这里得到它:一页包含多个片段的 ViewPager “java.lang.IllegalArgumentException:找不到 id 的视图”

于 2013-08-14T18:50:53.440 回答