我有這個工作順順當當,它的正是我需要的:Android的刷卡查看與標籤添加額外的片段標籤片段
http://developer.android.com/training/implementing-navigation/lateral.html
我的設置是基本相同的;具有兩個嵌套的接片(片段),像這樣的活性:
public class StopActivity extends Activity
{
...
public static class EnRouteFragment extends Fragment
{
...
}
public static class OnSiteFragment extends Fragment
{
...
}
}
然而,現在我想一些可重複使用的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>
......並把它添加到第一個選項卡/片段(對應佈局「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:無法啓動活動 ComponentInfo {com.me.mobile/com.me.mobile.StopActivity}: android.view.InflateException:二進制XML文件行#8:錯誤 膨脹類片段
...
致:java.lang.IllegalStateException:片段 com.me.mobile.fragment.DetailsFragment沒有創建視圖。在 android.app.Activity.onCreateView(Activity.java:4809)在 android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:689)
任何線索將不勝感激!
這樣做的竅門!很好,很簡單,謝謝。 – 2015-03-19 12:41:34