Android 使用选项卡滑动视图向选项卡片段添加额外的片段

Android Swipe View with Tabs adding additional fragments to tab fragments

我可以流畅地使用它,这正是我所需要的:

http://developer.android.com/training/implementing-navigation/lateral.html

我的设置基本相同;带有两个嵌套选项卡(片段)的 activity,如下所示:

public class StopActivity extends Activity
{
  ...
    public static class EnRouteFragment extends Fragment
    {
      ...
    }

    public static class OnSiteFragment extends Fragment
    {
      ...
    }
}

但是,现在我想在其中的 tab/view 片段中添加一些可重复使用的 UI 片段。我对 Android 比较陌生,但在将片段添加到普通视图时没有遇到任何问题,否则。除了上面的设置之外,我还创建了一个简单的片段:

public class DetailsFragment extends Fragment
{
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)
    {
    final View view = inflater.inflate(R.layout.fragment_details, container, false);

    return super.onCreateView(inflater, container, savedInstanceState);
    }
}

相当基本的布局:

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

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_alignParentTop="true" >

        <TextView
            android:id="@+id/tripText"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Trip"
            android:textAppearance="?android:attr/textAppearanceLarge" />

        <Spinner
            android:id="@+id/tripSpinner"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_weight="1" />

        <Space android:layout_width="20dp" android:layout_height="match_parent" />

        <TextView
            android:id="@+id/stopNoText"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Stop #"
            android:textAppearance="?android:attr/textAppearanceLarge" />

        <Spinner
            android:id="@+id/stopNoSpinner"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_weight="1" />

    </LinearLayout>

</RelativeLayout>

...并将其添加到第一个tab/fragment(对应上面"EnRouteFragment"的布局):

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="wrap_content"
    android:layout_height="match_parent"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context="com.me.mobile.StopActivity$PlaceholderFragment" >


    <fragment android:name="com.me.mobile.fragment.DetailsFragment"
              android:id="@+id/detailsFragment"
              android:layout_weight="1"
              android:layout_width="0dp"
              android:layout_height="match_parent" />

</RelativeLayout>

我得到以下异常:

java.lang.RuntimeException: Unable to start activity ComponentInfo{com.me.mobile/com.me.mobile.StopActivity}: android.view.InflateException: Binary XML file line #8: Error inflating class fragment

...

Caused by: java.lang.IllegalStateException: Fragment com.me.mobile.fragment.DetailsFragment did not create a view. at android.app.Activity.onCreateView(Activity.java:4809) at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:689)

任何线索将不胜感激!

如果您 return 超类视图,为什么要在 DetailsFragment 中膨胀您的视图?这没有任何意义。你应该做以下事情

public class DetailsFragment extends Fragment
{
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)
    {
    final View view = inflater.inflate(R.layout.fragment_details, container, false);

    return view;
    }
}