1

AndroidサポートライブラリでFragmentTabHostを使用していますが、

私のレイアウトファイル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>

上記のレイアウトは私のフラグメントで使用されています:

   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;
        }
   ...
}

しかし、onCreateView() で初期化されている上記のコードでClassCastException例外が発生しました。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

TabHostにキャストできないFragmentTabHost、その理由とそれを取り除く方法について不平を言います。ありがとう。

4

1 に答える 1

1
mTabHost = (FragmentTabHost) content.findViewById(android.R.id.tabhost);

する必要があります

mTabHost = (FragmentTabHost) content.findViewById(R.id.tabhost);

およびxmlレイアウトで指定する必要があります

android:id="@android:id/tabhost"

なので

android:id="@+id/tabhost"
于 2013-07-03T14:19:31.717 に答える