battlenub
battlenub

Reputation: 187

FragmentTabHost in Android Studio

I'm trying to implement a 3 tab system in my app.

Index.java ( Where the 3 tabs should be located )

public class Index extends FragmentActivity{
private FragmentTabHost mTabHost;
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_index);
    mTabHost = (FragmentTabHost) findViewById(android.R.id.tabhost);
    mTabHost.setup(this, getSupportFragmentManager(),         android.R.id.tabcontent);
    mTabHost.addTab(
            mTabHost.newTabSpec("tab1").setIndicator("Tab 1", null),
            FragmentTab.class, null);
    mTabHost.addTab(
            mTabHost.newTabSpec("tab2").setIndicator("Tab 2", null),
            FragmentTab.class, null);
    mTabHost.addTab(
            mTabHost.newTabSpec("tab3").setIndicator("Tab 3", null),
            FragmentTab.class, null);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    MenuInflater inflater = getMenuInflater();
    inflater.inflate(R.menu.menu_index,menu);

    return super.onCreateOptionsMenu(menu);
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
    // Handle action bar item clicks here. The action bar will
    // automatically handle clicks on the Home/Up button, so long
    // as you specify a parent activity in AndroidManifest.xml.
    int id = item.getItemId();

    //noinspection SimplifiableIfStatement
    if (id == R.id.action_settings) {
        return true;
    }
    return super.onOptionsItemSelected(item);
}

}

Index.xml

<RelativeLayout 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:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
android:paddingBottom="@dimen/activity_vertical_margin"
tools:context="com.example.vincent.tennispartner2.Index">

<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="fill_parent"
            android:layout_height="wrap_content"
            android:layout_gravity="bottom"/>

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

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

FragmentTab.java

/**
 * Created by Vincent on 2015-08-25.
 */
public class FragmentTab extends Fragment {

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
}

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {
    View v = inflater.inflate(R.layout.fragment_layout, container, false);
    TextView tv = (TextView) v.findViewById(R.id.text);
    tv.setText(this.getTag() + " Content");
    return v;
}
}

Ive been stuck on this part for hours, going from the actionbar to fragments to toolbar to other stuff but i can't find anything that works for me. Seeing as I just started coding in java/android I could really use some help with this.

Full Error :

08-25 15:41:22.085  13008-13008/com.example.vincent.tennispartner2     E/AndroidRuntime﹕ FATAL EXCEPTION: main
    Process: com.example.vincent.tennispartner2, PID: 13008
    java.lang.RuntimeException: Unable to start activity     ComponentInfo{com.example.vincent.tennispartner2/com.example.vincent.tennispartn    er2.Index}: 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         android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2693)
                at       android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2760)
                at     android.app.ActivityThread.access$900(ActivityThread.java:177)
                at     android.app.ActivityThread$H.handleMessage(ActivityThread.java:1448)
                at android.os.Handler.dispatchMessage(Handler.java:102)
                at android.os.Looper.loop(Looper.java:145)
                at android.app.ActivityThread.main(ActivityThread.java:5944)
                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:1389)
                at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1184)
     Caused by: 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.example.vincent.tennispartner2.Index.onCreate(Index.java:25)
                at android.app.Activity.performCreate(Activity.java:6289)
                at     android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1119)
                at     android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2646)
                at     android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2760)
                at android.app.ActivityThread.access$900(ActivityThread.java:177)
                at     android.app.ActivityThread$H.handleMessage(ActivityThread.java:1448)
                at android.os.Handler.dispatchMessage(Handler.java:102)
                at android.os.Looper.loop(Looper.java:145)
                at android.app.ActivityThread.main(ActivityThread.java:5944)
                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:1389)
                at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1184)

Any tips are greatly appreciated!

Upvotes: 2

Views: 1812

Answers (2)

David Jesus
David Jesus

Reputation: 2111

Change:

setContentView(R.layout.activity_index);

To:

setContentView(R.layout.index);

Upvotes: 1

Kristy Welsh
Kristy Welsh

Reputation: 8530

Change:

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

To:

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

Upvotes: 0

Related Questions