Jd Baba
Jd Baba

Reputation: 6118

TabActivity error in Android

Hi I am trying to use tabactivity but I can't seem to make it work. I tried to extend the MainActivity to TabActivity but it seems to be deprecated.

The code I am using right now is as follows:

package jdexamples.app13_exploringtabs;
import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.widget.TabHost;

public class MainActivity extends AppCompatActivity {

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


        //setContentView(R.layout.activity_main);

        // tabhost


        TabHost mTabHost = (TabHost) findViewById(R.id.tabHost);
        mTabHost.setup();

        mTabHost.addTab(mTabHost.newTabSpec("first").setIndicator("First").setContent(new Intent(this  ,first.class )));
        mTabHost.addTab(mTabHost.newTabSpec("second").setIndicator("Second").setContent(new Intent(this , second.class )));
        mTabHost.addTab(mTabHost.newTabSpec("third").setIndicator("Third").setContent(new Intent(this, third.class)));
        mTabHost.setCurrentTab(0);

    }


}

Any suggestions on how to fix this ?

I tried suggestions from Android Maps within TabHost. getTabHost() return error but when I try to launch the application on the emulator, it always crashes.

The logcat is as follows:

10-16 22:09:01.375  11571-11571/? I/art﹕ Not late-enabling -Xcheck:jni (already on)
10-16 22:09:01.375  11571-11571/? I/art﹕ Late-enabling JIT
10-16 22:09:01.377  11571-11571/? I/art﹕ JIT created with code_cache_capacity=2MB compile_threshold=1000
10-16 22:09:01.537  11571-11571/jdexamples.app13_exploringtabs W/System﹕ ClassLoader referenced unknown path: /data/app/jdexamples.app13_exploringtabs-2/lib/x86
10-16 22:09:01.565  11571-11571/jdexamples.app13_exploringtabs D/AndroidRuntime﹕ Shutting down VM
10-16 22:09:01.565  11571-11571/jdexamples.app13_exploringtabs E/AndroidRuntime﹕ FATAL EXCEPTION: main
    Process: jdexamples.app13_exploringtabs, PID: 11571
    java.lang.RuntimeException: Unable to start activity ComponentInfo{jdexamples.app13_exploringtabs/jdexamples.app13_exploringtabs.MainActivity}: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.TabHost.setup()' on a null object reference
            at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2416)
            at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2476)
            at android.app.ActivityThread.-wrap11(ActivityThread.java)
            at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1344)
            at android.os.Handler.dispatchMessage(Handler.java:102)
            at android.os.Looper.loop(Looper.java:148)
            at android.app.ActivityThread.main(ActivityThread.java:5417)
            at java.lang.reflect.Method.invoke(Native Method)
            at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726)
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)
     Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.TabHost.setup()' on a null object reference
            at jdexamples.app13_exploringtabs.MainActivity.onCreate(MainActivity.java:20)
            at android.app.Activity.performCreate(Activity.java:6237)
            at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1107)
            at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2369)
            at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2476)
            at android.app.ActivityThread.-wrap11(ActivityThread.java)
            at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1344)
            at android.os.Handler.dispatchMessage(Handler.java:102)
            at android.os.Looper.loop(Looper.java:148)
            at android.app.ActivityThread.main(ActivityThread.java:5417)
            at java.lang.reflect.Method.invoke(Native Method)
            at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726)
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)
10-16 22:09:05.029  11571-11571/? I/Process﹕ Sending signal. PID: 11571 SIG: 9

Upvotes: 0

Views: 807

Answers (3)

Chordin4tion
Chordin4tion

Reputation: 984

Android has now provided new way to implement tabs in your app which are really simple to use...

First you have to add a dependency to your project:

compile 'com.android.support:design:22.2.1'

After that you have to declare your tabLayout in your activity's xml file:

<android.support.design.widget.TabLayout
            android:id="@+id/tabs"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:background="#FFC107"
            app:tabMinWidth="30dp"
            app:tabMode="scrollable"
            app:tabSelectedTextColor="#FFFFFF" />

Now in your activity class do something like this:

    TabLayout tabLayout = (TabLayout) findViewById(R.id.tabs);

    tabLayout.setupWithViewPager(your_view_pager);

Upvotes: 0

IntelliJ Amiya
IntelliJ Amiya

Reputation: 75788

First Of all Enable

setContentView(R.layout.activity_main);

TabActivity This class was deprecated in API level 13. New applications should use Fragments instead of this class; to continue to run on older devices, you can use the v4 support library .

Bad Approach

Using deprecated code .

Switch to FragmentActivity Or AppCompatActivity

Have a look here

  1. How to add swipe tab feature with existing FragmentTabHost?

  2. Android app keeps crashing with tabhost

http://developer.android.com/intl/es/reference/android/widget/TabHost.html#setup(android.app.LocalActivityManager)

Upvotes: 3

Ravi Bhandari
Ravi Bhandari

Reputation: 4698

I suggest you to use PagerSlidingTabs. Check this link here

By using above library you can swipe pages using ViewPager. You can see working example on Play Store here. Hope this will help you.

Upvotes: 1

Related Questions