Gian Segato
Gian Segato

Reputation: 2489

No view found for id 0x for fragment after rotation?

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.

Upvotes: 2

Views: 2734

Answers (1)

Gian Segato
Gian Segato

Reputation: 2489

It was all due to setRetainInstance(true). I thought it was sufficient to set it to false in the two ViewPagers. It wasn't, actually. I removed it from the single (nested) fragments, and all went well.

It looks like nested fragments can't handle setRetainInstance. Got it from here: ViewPager with one page containing multiple Fragments "java.lang.IllegalArgumentException: No view found for id"

Upvotes: 8

Related Questions