Search code examples
androidandroid-fragmentsmaster-detail

Android: can't load in a detail fragment in master detail


I have an app that currently works on a handheld device and I now want it to work on a tablet using a master/detail flow. I have an InformationFragment that is shown when you click on an item in a ListView. The app works on a handheld device, but on a tablet I get an exception that there is no view found for two_pane_information_container. Here is my code:

public class MainActivity extends FragmentActivity {
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        twoPane = getResources().getBoolean(R.bool.isTablet);
        setContentView(R.layout.main_layout);
        // setting up the navigation view and listfragment etc.
    }
    public static class InformationListFragment extends ListFragment {
    @Override
    public void onListItemClick(ListView l, View v, int position, long id) {
        Info o = (Info) getListAdapter().getItem(position);
        Bundle b = new Bundle();
        b.putParcelable("info", o);
        if (twoPane) {
            // load the fragment to the right of the list
            // this is the part causing problems
            android.support.v4.app.FragmentTransaction ft =getFragmentManager().beginTransaction();
            InformationFragment ia = new InformationFragment();
            ia.setArguments(b);
            ft.replace(R.id.two_pane_information_container, ia);
            ft.commit();
        } else {
            // An activity that displays the inforrmationfragment
                    // this works 
            Intent i = new Intent(getActivity(), InformationActivity.class);
            i.putExtra("info", b);
            startActivity(i);
        }
    }
}

res/layout/fragment_main.xml

<android.support.v4.widget.DrawerLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/drawer_layout"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:background="@color/table_background" >

    <!-- The main content view -->

    <android.support.v4.view.ViewPager
        android:id="@+id/pager"
        android:layout_width="match_parent"
        android:layout_height="match_parent" >
    </android.support.v4.view.ViewPager>

    <!-- The navigation drawer -->

    <ListView
        android:id="@+id/left_drawer"
        android:layout_width="240dp"
        android:layout_height="match_parent"
        android:layout_gravity="start"
        android:background="@android:color/background_dark"
        android:choiceMode="singleChoice"
        android:divider="@android:color/transparent"
        android:dividerHeight="1dip" />

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

res/layout/main_twopane.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="horizontal" >

    <include
        android:layout_width="400dp"
        layout="@layout/fragment_main" />

    <LinearLayout
        android:id="@+id/two_pane_information_container"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:orientation="horizontal" >
    </LinearLayout>

</LinearLayout>

res/values/layouts.xml

<?xml version="1.0" encoding="utf-8"?>
<resources>
    <bool name="isTablet">false</bool>
    <item name="main_layout" type="layout">@layout/fragment_main</item>
</resources>

res/values-large/layouts.xml and res/values-sw600dp/layouts.xml

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

    <bool name="isTablet">true</bool>
    <item name="main" type="layout">@layout/main_twopane</item>

</resources>

Solution

  • Your layout name in the final 2 XML files differs.

    <item name="main" type="layout">
    

    should be

    <item name="main_layout" type="layout">