[英]ClassCastException when using FragmentTabHost
I am using FragmentTabHost with android support library, 我正在将FragmentTabHost与android支持库一起使用,
My layout file sub_main.xml : 我的布局文件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() : 但是,我在上面的代码中得到了ClassCastException异常,其中在onCreateView()中初始化了
mTabHost
:
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. 它抱怨说
TabHost
无法转换为FragmentTabHost
,为什么以及如何摆脱它。 Thanks. 谢谢。
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 在xml布局中,您应该指定
android:id="@android:id/tabhost"
as 如
android:id="@+id/tabhost"
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.