Android:无法在主细节中加载细节片段

时间:2014-06-03 06:17:21

标签: android android-fragments master-detail

我有一个应用程序,目前可在手持设备上运行,我现在希望它使用主/详细信息流在平板电脑上工作。当您点击InformationFragment中的某个项目时,我会显示ListView。该应用程序适用于手持设备,但在平板电脑上,我得到一个例外,即找不到two_pane_information_container的视图。这是我的代码:

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 /布局/ 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 /布局/ 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 /值/ 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和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>

1 个答案:

答案 0 :(得分:1)

最终2个XML文件中的布局名称不同。

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

应该是

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