Reputation: 38882
I am using FragmentTabHost with android support library,
My layout file sub_main.xml:
<android.support.v4.app.FragmentTabHost
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@android:id/tabhost"
android:layout_width="fill_parent"
android:layout_height="fill_parent" >
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<TabWidget
android:id="@android:id/tabs"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_weight="0"
android:orientation="horizontal" />
<FrameLayout
android:id="@+id/tabFrameLayout"
android:layout_width="fill_parent"
android:layout_height="0dp"
android:layout_weight="1" />
</LinearLayout>
</android.support.v4.app.FragmentTabHost>
The above layout is used in my fragment:
public class MyFragment extends Fragment{
...
private FragmentTabHost mTabHost;
private View content;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
super.onCreateView(inflater, container, savedInstanceState);
View content = inflater.inflate(R.layout.sub_main, null);
// the following code throw ClassCastException, why?
mTabHost = (FragmentTabHost) content.findViewById(android.R.id.tabhost);
return content;
}
...
}
But, I got the ClassCastException exception in the above code where mTabHost
is initialized in onCreateView() :
07-03 16:35:09.704: E/AndroidRuntime(25350): java.lang.ClassCastException: android.widget.TabHost cannot be cast to android.support.v4.app.FragmentTabHost
It complains that TabHost
can not be cast to FragmentTabHost
, why & how to get rid of it. Thanks.
Upvotes: 1
Views: 1500
Reputation: 121
mTabHost = (FragmentTabHost) content.findViewById(android.R.id.tabhost);
should be
mTabHost = (FragmentTabHost) content.findViewById(R.id.tabhost);
and in the xml layout you should specify
android:id="@android:id/tabhost"
as
android:id="@+id/tabhost"
Upvotes: 1