Jack
Jack

Reputation: 115

TabActivities with Fragments

Introduction

I wanted to use implement use Tabhost, but I faced some crashes not really sure why. I tried to look for google developer's guide but can't really find the reason why my apps crash

My codes

public class TabActivities extends FragmentActivity  {

    private FragmentTabHost mTabHost;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.frequencies);

        mTabHost = (FragmentTabHost) findViewById(android.R.id.tabhost);
        mTabHost.addTab(mTabHost.newTabSpec("tab1").setIndicator("Tab1"),
                MyLayout1.class, null);
        mTabHost.addTab(mTabHost.newTabSpec("tab2").setIndicator("Tab2"),
                MyLayout.class, null);
        mTabHost.addTab(mTabHost.newTabSpec("tab3").setIndicator("Tab3"),
                MyLayout2.class, null); 
        mTabHost.addTab(mTabHost.newTabSpec("tab3").setIndicator("Tab4"),
                MyLayout3.class, null);  
        }
}

MyLayout1, 2, 3 contains only the layout

public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.mylayout);
}

I already include the classes in the android manifest.

Here's my logcat

07-29 22:50:58.226: E/AndroidRuntime(25877): FATAL EXCEPTION: main
07-29 22:50:58.226: E/AndroidRuntime(25877): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.Test.projecttest/com.Test.projecttest.TabActivities}: java.lang.ClassCastException: android.widget.TabHost cannot be cast to android.support.v4.app.FragmentTabHost
07-29 22:50:58.226: E/AndroidRuntime(25877):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2081)
07-29 22:50:58.226: E/AndroidRuntime(25877):    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2106)
07-29 22:50:58.226: E/AndroidRuntime(25877):    at android.app.ActivityThread.access$700(ActivityThread.java:134)
07-29 22:50:58.226: E/AndroidRuntime(25877):    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1217)
07-29 22:50:58.226: E/AndroidRuntime(25877):    at android.os.Handler.dispatchMessage(Handler.java:99)
07-29 22:50:58.226: E/AndroidRuntime(25877):    at android.os.Looper.loop(Looper.java:137)
07-29 22:50:58.226: E/AndroidRuntime(25877):    at android.app.ActivityThread.main(ActivityThread.java:4856)
07-29 22:50:58.226: E/AndroidRuntime(25877):    at java.lang.reflect.Method.invokeNative(Native Method)
07-29 22:50:58.226: E/AndroidRuntime(25877):    at java.lang.reflect.Method.invoke(Method.java:511)
07-29 22:50:58.226: E/AndroidRuntime(25877):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1007)
07-29 22:50:58.226: E/AndroidRuntime(25877):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:774)
07-29 22:50:58.226: E/AndroidRuntime(25877):    at dalvik.system.NativeStart.main(Native Method)
07-29 22:50:58.226: E/AndroidRuntime(25877): Caused by: java.lang.ClassCastException: android.widget.TabHost cannot be cast to android.support.v4.app.FragmentTabHost
07-29 22:50:58.226: E/AndroidRuntime(25877):    at com.Test.projecttest.TabActivities.onCreate(TabActivities.java:22)
07-29 22:50:58.226: E/AndroidRuntime(25877):    at android.app.Activity.performCreate(Activity.java:5047)
07-29 22:50:58.226: E/AndroidRuntime(25877):    at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1094)
07-29 22:50:58.226: E/AndroidRuntime(25877):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2045)
07-29 22:50:58.226: E/AndroidRuntime(25877):    ... 11 more

My frequencies.xml

<?xml version="1.0" encoding="utf-8"?>
<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:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent">

    <FrameLayout
        android:id="@android:id/tabcontent"
        android:layout_width="0dp"
        android:layout_height="0dp"
        android:layout_weight="0"/>

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

    <TabWidget
        android:id="@android:id/tabs"
        android:orientation="horizontal"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_weight="0"/>

</LinearLayout>
</android.support.v4.app.FragmentTabHost>

Upvotes: 0

Views: 644

Answers (1)

Bram
Bram

Reputation: 4613

Do you declare TabHost in your xml-file instead of

<android.support.v4.app.FragmentTabHost ...  

?

The error occurs because your compiler tries to find TabHost but finds FragmentTabHost.

EDIT:

I am sorry, I thought that you defined your TabHost in an xml-file but you define it directly in the findViewById.

Take a look at your imports. What R-file do you import? You are trying to retrieve the android.R.tabhost xml-file (that can be converted to FragmentTabHost) that is defined in the specific support-library R and not in the standard android.R file.

ANOTHER EDIT:

If you did import the right R-file. I would like to refer to my first question.

Could you replace the

TabHost

part in your xml-file with

android.support.v4.app.FragmentTabHost

Upvotes: 2

Related Questions