0

I'm trying to implement a nested fragment with the FragmentTabHost, however I keep getting an error with the setup() function. Code is below.

This is My Class File

public class ForumFragment extends Fragment{
private FragmentTabHost mTabHost;
public ForumFragment() {
    // Required empty public constructor
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)
{
    View view = inflater.inflate(R.layout.fragment_forum,container,false);


    mTabHost = (FragmentTabHost) view.findViewById(android.R.id.tabhost);
    mTabHost.setup(getActivity(), getChildFragmentManager(),android.R.id.tabcontent);

    mTabHost.addTab(mTabHost.newTabSpec("Tab1").setIndicator("DBoard"), DiscussionBoardFragment.class, null);
    mTabHost.addTab(mTabHost.newTabSpec("Tab2").setIndicator("Events"), EventsFragment.class, null);
    mTabHost.addTab(mTabHost.newTabSpec("Tab3").setIndicator("HW Help"), HWHelpFragment.class, null);
    mTabHost.addTab(mTabHost.newTabSpec("Tab4").setIndicator("Classifieds"), ClassifiedsFragment.class, null);

    return view;

}

Here is my XML File

<TabHost
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/tabhost">


    <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">

        </TabWidget>

        <FrameLayout
            android:id="@android:id/tabcontent"
            android:layout_width="match_parent"
            android:layout_height="match_parent">

            <RelativeLayout
                android:id="@+id/DBoard"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:orientation="vertical"></RelativeLayout>

            <RelativeLayout
                android:id="@+id/Events"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:orientation="vertical"></RelativeLayout>

            <RelativeLayout
                android:id="@+id/HWHelp"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:orientation="vertical"></RelativeLayout>
            <RelativeLayout
                android:id="@+id/Classifieds"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:orientation="vertical"></RelativeLayout>

        </FrameLayout>
    </LinearLayout>

And here is the error I'm receiving

java.lang.NullPointerException: Attempt to invoke virtual method 'void android.support.v4.app.FragmentTabHost.setup(android.content.Context, android.support.v4.app.FragmentManager, int)' on a null object reference
                                                                                     at com.ForumFragment.onCreateView(ForumFragment.java:30)
                                                                                     at android.support.v4.app.Fragment.performCreateView(Fragment.java:1962)
                                                                                     at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1067)
                                                                                     at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1248)
                                                                                     at android.support.v4.app.BackStackRecord.run(BackStackRecord.java:738)
                                                                                     at android.support.v4.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1613)
                                                                                     at android.support.v4.app.FragmentManagerImpl$1.run(FragmentManager.java:517)
                                                                                     at android.os.Handler.handleCallback(Handler.java:739)
                                                                                     at android.os.Handler.dispatchMessage(Handler.java:95)
                                                                                     at android.os.Looper.loop(Looper.java:145)
                                                                                     at android.app.ActivityThread.main(ActivityThread.java:6897)
                                                                                     at java.lang.reflect.Method.invoke(Native Method)
                                                                                     at java.lang.reflect.Method.invoke(Method.java:372)
                                                                                     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1404)
                                                                                     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1199)
Damian Kozlak
  • 7,065
  • 10
  • 45
  • 51

4 Answers4

0

Change

mTabHost.setup(getActivity(), getChildFragmentManager(),android.R.id.tabcontent);

to

mTabHost.setup(getActivity(), getSupportFragmentManager(),android.R.id.tabcontent);
Damian Kozlak
  • 7,065
  • 10
  • 45
  • 51
0

The problem is in this line:
mTabHost = (FragmentTabHost) view.findViewById(android.R.id.tabhost);

You need to inflate your own R file and not android.R. Just change it to:

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

and in XML file change the <TabHost to <android.support.v4.app.FragmentTabHost

Evgeniy Mishustin
  • 3,343
  • 3
  • 42
  • 81
  • When I change android.R to just R.id. I get a typecast error. – AnalyticalQ Dec 29 '15 at 08:54
  • Yes, you need to cast to TabHost, not FragmentTabHost. See the edited answer. Also declare mTabHost as TabHost – Evgeniy Mishustin Dec 29 '15 at 08:56
  • @LuciusHipan he is using `FragmentTabHost` and according to that he has written write code see this link: http://developer.android.com/reference/android/support/v4/app/FragmentTabHost.html There is one mistake, instead of `getChildFragmentManager()`, he should use `getSupportFragmentManager()` – Anand Singh Dec 29 '15 at 08:57
  • you're right, can you add your answer ? or edit mine? – Evgeniy Mishustin Dec 29 '15 at 09:00
  • Using getSupportFragmentManager() I get an error as well , is there anything I should import? – AnalyticalQ Dec 29 '15 at 09:11
  • are your Fragment is from support library ? Be sure you imported correct Fragment class – Evgeniy Mishustin Dec 29 '15 at 09:21
  • Yes, I imported import android.support.v4.app.Fragment – AnalyticalQ Dec 29 '15 at 09:44
  • I'm still receiving the same error @LuciusHipan ! -_- , I can't use the getSupportFragmentManger() because I'm not deriving FragmentActivity within my class. I'm using the nested fragment way with my app. – AnalyticalQ Dec 29 '15 at 10:05
  • try the fix I put now in answer, please. – Evgeniy Mishustin Dec 29 '15 at 10:09
  • @LuciusHipan Ok I tried that it gives me a rendering problem and will not display in the design view, however when I run the app it compiles with no errors BUT the tabs are not being displayed. The text of what suppose to be displayed in the tab is displayed. – AnalyticalQ Dec 29 '15 at 10:13
  • oh, now I've got you. please look here : http://stackoverflow.com/a/13461126/2031068 – Evgeniy Mishustin Dec 29 '15 at 10:18
  • Thanks , that did help with the setup() error problem but the tabs are still not being displayed on the screen when I run the app. – AnalyticalQ Dec 29 '15 at 10:47
0

you have to convert your fragment to android.support.v4.app.Fragment because you are using android.support.v4.app.FragmentTabHost and it is support only android.support.v4.app.Fragment.

read this line android.support.v4.app.FragmentTabHost.setup(android.content.Context, android.support.v4.app.FragmentManager, int)

Sameer Donga
  • 988
  • 9
  • 24
0

Android Studio does not provide dependencies so you will have to import them manually. File > Project Structure > app (under “Modules) > Dependencies tab. Click the “+” at the bottom to add a Library Dependency; in this case “support-v4“.