这是片段的布局:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/lytContainer"
android:layout_width="fill_parent"
android:layout_height="match_parent"
android:background="@drawable/bg"
android:orientation="vertical" >
<include
android:id="@+id/include1"
layout="@layout/header" />
<RelativeLayout
android:padding="5dp"
android:layout_width="fill_parent"
android:layout_height="fill_parent" >
<FrameLayout
android:id="@+id/lytImage"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:background="@drawable/round" >
<ImageView
android:id="@+id/imgRecibeImage"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="10dp"
android:scaleType="fitXY"
android:src="@drawable/img5" />
</FrameLayout>
<TabHost
android:id="@+id/tabhost"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_alignParentRight="true"
android:layout_below="@+id/txtRecibeComment"
android:layout_marginTop="0dp" >
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<TabWidget
android:id="@android:id/tabs"
android:layout_width="match_parent"
android:layout_height="wrap_content" >
</TabWidget>
<FrameLayout
android:id="@android:id/tabcontent"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<LinearLayout
android:id="@+id/tab1"
android:layout_width="match_parent"
android:layout_height="match_parent" >
</LinearLayout>
<LinearLayout
android:id="@+id/tab2"
android:layout_width="match_parent"
android:layout_height="match_parent" >
</LinearLayout>
<LinearLayout
android:id="@+id/tab3"
android:layout_width="match_parent"
android:layout_height="match_parent" >
</LinearLayout>
</FrameLayout>
</LinearLayout>
</TabHost>
<TextView
android:id="@+id/txtRecibeComment"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_below="@+id/lytImage"
android:background="#000000"
android:gravity="right"
android:padding="5dp"
android:text="Crispy Chicken and Sandwich Crispy Chicken and Sandwich Crispy Chicken and Sandwich Crispy Chicken and Sandwich "
android:textColor="#ffffff"
android:textSize="10sp" />
</RelativeLayout>
</LinearLayout>
这是片段的类
public class Tab1 extends Fragment {
private TextView txtHeaderTitle;
private TextView txtRecibeComment;
private LinearLayout lytContainer;
private ImageView imgRecibeImage;
private ImageView imgHeader;
DBHelper db;
TabHost tabHost;
Resources res;
Intent i;
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle b) {
if (container == null) {
return null;
}
LinearLayout rl = (LinearLayout)inflater.inflate(R.layout.recipe, container, false);
db = new DBHelper(getActivity());
txtHeaderTitle = (TextView) rl.findViewById(R.id.txtHeaderTitle);
txtRecibeComment = (TextView) rl.findViewById(R.id.txtRecibeComment);
lytContainer = (LinearLayout)rl.findViewById(R.id.lytContainer);
imgRecibeImage = (ImageView) rl.findViewById(R.id.imgRecibeImage);
imgHeader = (ImageView) rl.findViewById(R.id.imgHeader);
imgHeader.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View arg0) {
// finish();
Intent i = new Intent(getActivity(),RecipesListActivity.class);
startActivity(i);
}
});
tabHost = (TabHost) container.findViewById(R.id.tabhost);
displayRecipe(getRecipe(getArguments().getInt("id")));
TabHost.TabSpec spec;
spec = tabHost.newTabSpec("Method");
spec.setIndicator(setMyIndictors("طريقة التحضير","L"));
i = new Intent(getActivity(),MethodActivity.class);
i.putExtra("rId", getArguments().getInt("id"));
spec.setContent(i);
tabHost.addTab(spec);
spec = tabHost.newTabSpec("Ingredients");
spec.setIndicator(setMyIndictors("مكونات الوصفة","R"));
i = new Intent(getActivity(),IngredientsActivity.class);
i.putExtra("rId", getArguments().getInt("id"));
spec.setContent(i);
tabHost.addTab(spec);
tabHost.setCurrentTab(1);
setTabColor(tabHost);
tabHost.setOnTabChangedListener(new OnTabChangeListener() {
@Override
public void onTabChanged(String arg0) {
setTabColor(tabHost);
}
});
return rl;
}
Taphost 为 NULL 的位置:错误为:
09-10 22:39:15.971: E/AndroidRuntime(1287): java.lang.NullPointerException
09-10 22:39:15.971: E/AndroidRuntime(1287): at mobile.bh.activities.Tab1.setTabColor(Tab1.java:109)
09-10 22:39:15.971: E/AndroidRuntime(1287): at mobile.bh.activities.Tab1.onCreateView(Tab1.java:82)
09-10 22:39:15.971: E/AndroidRuntime(1287): at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:871)
09-10 22:39:15.971: E/AndroidRuntime(1287): at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1083)
09-10 22:39:15.971: E/AndroidRuntime(1287): at android.support.v4.app.BackStackRecord.run(BackStackRecord.java:635)
09-10 22:39:15.971: E/AndroidRuntime(1287): at android.support.v4.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1431)
09-10 22:39:15.971: E/AndroidRuntime(1287): at android.support.v4.app.FragmentManagerImpl.executePendingTransactions(FragmentManager.java:431)
09-10 22:39:15.971: E/AndroidRuntime(1287): at android.support.v4.app.FragmentPagerAdapter.finishUpdate(FragmentPagerAdapter.java:141)
09-10 22:39:15.971: E/AndroidRuntime(1287): at android.support.v4.view.ViewPager.populate(ViewPager.java:895)
09-10 22:39:15.971: E/AndroidRuntime(1287): at android.support.v4.view.ViewPager.populate(ViewPager.java:772)
09-10 22:39:15.971: E/AndroidRuntime(1287): at android.support.v4.view.ViewPager.onMeasure(ViewPager.java:1234)
09-10 22:39:15.971: E/AndroidRuntime(1287): at android.view.View.measure(View.java:8313)
09-10 22:39:15.971: E/AndroidRuntime(1287): at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:3138)