124697
124697

Reputation: 21893

android.support.v4.app.FragmentTabHost cannot be cast to android.widget.LinearLayout

This code use to work fine but after updating my build tools and sdk I started getting this cast exception. anyone know how to fix it?

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main_tab_view);

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

    TabWidget tw = (TabWidget) findViewById(android.R.id.tabs);
    LinearLayout ll = (LinearLayout) tw.getParent(); //error happens on this line
     ...
    }

main_tab_view.xml

<?xml version="1.0" encoding="utf-8"?>
<android.support.v4.widget.DrawerLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/drawer_layout"
    android:layout_width="match_parent"
    android:layout_height="match_parent" >

    <LinearLayout
        xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical" >

        <FrameLayout
            android:id="@+id/realtabcontent"
            android:layout_width="match_parent"
            android:layout_height="0dip"
            android:layout_weight="1" />

        <android.support.v4.app.FragmentTabHost
            android:id="@android:id/tabhost"
            android:layout_width="match_parent"

            android:layout_height="wrap_content" >

            <TabWidget
                android:id="@android:id/tabs"

                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:orientation="horizontal" />

            <FrameLayout
                android:id="@android:id/tabcontent"
                android:layout_width="0dp"
                android:layout_height="0dp"
                android:layout_weight="0" />
        </android.support.v4.app.FragmentTabHost>
    </LinearLayout>

    <ListView
        android:id="@+id/left_drawer"
        android:layout_width="240dp"
        android:layout_height="match_parent"
        android:layout_gravity="start"
        android:background="@color/blue_actionbar_light"
        android:choiceMode="singleChoice"
        android:divider="@android:color/transparent"
        android:dividerHeight="10dp"
        android:listSelector="@drawable/selectable_background_darkblue" />

</android.support.v4.widget.DrawerLayout>

java.lang.RuntimeException: Unable to start activity java.lang.ClassCastException: android.support.v4.app.FragmentTabHost cannot be cast to android.widget.LinearLayout

Upvotes: 2

Views: 1030

Answers (1)

124697
124697

Reputation: 21893

I finally managed to fix it.

I had to revert back to the version of the support library that used to work. luckily i had a back up. I also used the same android sdk to build it that used to work before.

Upvotes: 1

Related Questions