Reputation: 471
Just following the basic android documentation on FragmentTabHost (top example - FragmentTabHost in an Activity) and received the following error:
10-21 02:30:07.409 26265-26265/com.example E/AndroidRuntime﹕ FATAL EXCEPTION: main
java.lang.ClassCastException: com.example.Main cannot be cast to android.support.v4.app.Fragment
What's strange is that I've followed everything properly and installed the necessary packages.
Main.java
import android.os.Bundle;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.FragmentTabHost;
public class Main extends FragmentActivity {
// Fragment TabHost as mTabHost
private FragmentTabHost mTabHost;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
mTabHost = (FragmentTabHost)findViewById(android.R.id.tabhost);
mTabHost.setup(this, getSupportFragmentManager(), R.id.realtabcontent);
mTabHost.addTab(mTabHost.newTabSpec("tab1").setIndicator("Tab1"),
Main.class, null);
mTabHost.addTab(mTabHost.newTabSpec("tab2").setIndicator("Tab2"),
ListNewItem.class, null);
mTabHost.addTab(mTabHost.newTabSpec("tab3").setIndicator("Tab3"),
Favourites.class, null);
mTabHost.addTab(mTabHost.newTabSpec("tab4").setIndicator("Tab4"),
Notifications.class, null);
}
}
main.xml
Note: Intellij displays the following in the side-window when attempting to display a preview:
Rendering Problems Exception raised during rendering: No tab known for tag null
Library bug maybe?
<android.support.v4.app.FragmentTabHost
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@android:id/tabhost"
android:layout_width="match_parent"
android:layout_height="match_parent">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<TabWidget
android:id="@android:id/tabs"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="0"
android:orientation="horizontal" />
<FrameLayout
android:id="@android:id/tabcontent"
android:layout_width="0dp"
android:layout_height="0dp"
android:layout_weight="0" />
<FrameLayout
android:id="@+id/realtabcontent"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1" />
</LinearLayout>
</android.support.v4.app.FragmentTabHost>
Manifest.xml
<activity android:name=".Main"
android:label="@string/main"
android:uiOptions="splitActionBarWhenNarrow" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER"/>
</intent-filter>
</activity>
Notifications.java (badly named fragment)
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
public class Favourites extends Fragment {
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
View V = inflater.inflate(R.layout.favourites, container, false);
return V;
}
}
Does anyone know why I'm having the problems with both the rendering and why the class conversion isn't working?
Upvotes: 1
Views: 2961
Reputation: 11122
Like what you see in the documentation we need to pass Fragment class, not FragmentActivity to the addTab() function.
in here you are passing Main.Class
mTabHost.addTab(mTabHost.newTabSpec("tab1").setIndicator("Tab1"),
Main.class, null);
Which is a FragmentActivity Class not a Fragment
public class Main extends FragmentActivity
See this part
mTabHost.addTab(mTabHost.newTabSpec("simple").setIndicator("Simple"),
FragmentStackSupport.CountingFragment.class, null);
They are passing FragmentStackSupport.CountingFragment.class which is a Fragment
To make you more sure see this grepcode link of android source code for FragmentStackSupport.CountingFragment -> link
That will explain why you are getting java.lang.ClassCastException because you are passing wrong type of class.
Upvotes: 1