【问题标题】:Android: can't load in a detail fragment in master detailAndroid:无法在主详细信息中加载详细信息片段
【发布时间】:2014-07-23 09:13:14
【问题描述】:

我有一个当前可以在手持设备上运行的应用,我现在希望它可以在使用主/详细信息流的平板电脑上运行。我有一个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/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 和 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>

【问题讨论】:

    标签: android android-fragments master-detail


    【解决方案1】:

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

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

    应该是

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

    【讨论】:

    • 感谢修复。但是现在信息片段没有出现在导航内容旁边。我该如何修改布局来解决这个问题?
    猜你喜欢
    • 2015-05-13
    • 2010-12-06
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-08-20
    • 1970-01-01
    相关资源
    最近更新 更多