0

I am using android.support.v4.app for Fragment, FragmentTabHost and Fragment. My goal is to show two tabs inside a fragment itself. So I followed the following process:

ListingFragment

public class ListingFragment extends Fragment {
private FragmentTabHost mTabHost;

public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {
  View rootView = inflater.inflate(R.layout.fragment_listing_home,container, false);
  mTabHost = (FragmentTabHost)rootView.findViewById(R.id.tabHost);

  mTabHost.setup(getActivity(), getChildFragmentManager(), R.id.realtabcontent);
  mTabHost.addTab(mTabHost.newTabSpec("listfragment").setIndicator("Fragment List"), ListingFragmentTabHost.class, null);
  return rootView;
}

ListingFrgmentTabHost

public class ListingFragmentTabHost extends FragmentActivity {
   ....
   ....
}

whereas FragmentActivity is import android.support.v4.app.FragmentActivity;

fragment_listing_home

<android.support.v4.app.FragmentTabHost
 xmlns:android="http://schemas.android.com/apk/res/android"
 android:id="@+id/tabHost"
 android:layout_width="match_parent"
 android:layout_height="match_parent">

<LinearLayout
    android:orientation="vertical"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

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

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

But when I am running the above snippets. It's throwing me cast exception i.e. java.lang.ClassCastException: com.app.grabhouse.fragment.ListingFragmentTabHost cannot be cast to android.support.v4.app.Fragment

Why is this happening?

Edit

After the suggestion I have converted my FragmentActivity to Fragment

public class ListingFragmentTabHost extends Fragment {

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {
    View rootView = inflater.inflate(R.layout.fragment_listing_home, container, false);
    return rootView;
}

@Override
public void onActivityCreated(@Nullable Bundle savedInstanceState) {
    recyclerView = (RecyclerView) getView().findViewById(R.id.mRecyclerView);
    super.onActivityCreated(savedInstanceState);
    LinearLayoutManager llm = new LinearLayoutManager(getActivity());
    llm.setOrientation(LinearLayoutManager.VERTICAL);
    recyclerView.setLayoutManager(llm);

    HomeRecyclerViewAdapter homeRecyclerViewAdapter = new HomeRecyclerViewAdapter(getActivity().getApplicationContext());
    recyclerView.setAdapter(homeRecyclerViewAdapter);
}
}
Amit Pal
  • 10,604
  • 26
  • 80
  • 160

1 Answers1

0

Because you need to pass a fragment into addTab and ListingFragmentTabHost is not a fragment, it is an activity

tyczj
  • 71,600
  • 54
  • 194
  • 296
  • That means the first answer of this question is wrong? http://stackoverflow.com/questions/20469877/adding-tab-inside-fragment-in-android – Amit Pal Mar 10 '16 at 21:48
  • Ah! My bad. Please checkout the above comment – Amit Pal Mar 10 '16 at 21:50
  • if you are talking about the one with 52 upvotes no its correct, the difference is he is using nested fragments all being hosted by an activity – tyczj Mar 10 '16 at 21:53
  • Thanks for pointing this out. Now I am using `Fragment` instead of `FragmentActivity` but now it's showing `No tab known for tag null`. I googled and know the cause and defined everything in `OnCreateView` but still it's throwing the same error. – Amit Pal Mar 10 '16 at 22:24
  • Please have a look at the Edit-1 section in question itself – Amit Pal Mar 10 '16 at 22:26