0

I am trying to create center raised tabbar.I searched a lot finally i got this link.I followed the answer but getting null pointer exception:

Here is the code:

public class Main extends TabActivity {
    /** Called when the activity is first created. */
    TabHost tabHost;
    TabHost.TabSpec spec;
    Intent intent;


    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        tabHost = getTabHost();
        setTabs();
        tabHost.getTabWidget().getChildAt(0).setBackgroundResource(R.drawable.home);
        tabHost.getTabWidget().getChildAt(1).setBackgroundResource(R.drawable.selling);
        tabHost.getTabWidget().getChildAt(2).setBackgroundResource(R.drawable.photo);
        tabHost.getTabWidget().getChildAt(3).setBackgroundResource(R.drawable.camera);
        tabHost.getTabWidget().getChildAt(4).setBackgroundResource(R.drawable.setting);



    }
    private void setTabs()
    {
        addTab("Home",R.drawable.home, FragmentsTab1.class);
        addTab("Search", R.drawable.selling, FragmentsTab2.class);
        addTab("Home", R.drawable.photo, FragmentsTab3.class);
        addTab("Search",R.drawable.camera, FragmentsTab2.class);
        addTab("Search",R.drawable.setting, FragmentsTab2.class);
    }

    private void addTab(String labelId, int drawableId, Class<?> c)
    {

        intent = new Intent(this, c);

         spec = tabHost.newTabSpec("tab" + labelId);    

        View tabIndicator = LayoutInflater.from(this).inflate(R.layout.tab_indicator, getTabWidget(), false);
        /*TextView title = (TextView) tabIndicator.findViewById(R.id.title);
        title.setText(labelId);*/
        ImageView icon = (ImageView) tabIndicator.findViewById(R.id.icon);
        icon.setImageResource(drawableId);

        spec.setIndicator(tabIndicator);
        spec.setContent(intent);
        tabHost.addTab(spec);
    }
} 

here is the error:

08-29 14:34:52.805: E/AndroidRuntime(1864): FATAL EXCEPTION: main
08-29 14:34:52.805: E/AndroidRuntime(1864): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.virtualstore/com.virtualstore.Main}: java.lang.NullPointerException
08-29 14:34:52.805: E/AndroidRuntime(1864):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1647)
08-29 14:34:52.805: E/AndroidRuntime(1864):     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1663)
08-29 14:34:52.805: E/AndroidRuntime(1864):     at android.app.ActivityThread.access$1500(ActivityThread.java:117)
08-29 14:34:52.805: E/AndroidRuntime(1864):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:931)
08-29 14:34:52.805: E/AndroidRuntime(1864):     at android.os.Handler.dispatchMessage(Handler.java:99)
08-29 14:34:52.805: E/AndroidRuntime(1864):     at android.os.Looper.loop(Looper.java:123)
08-29 14:34:52.805: E/AndroidRuntime(1864):     at android.app.ActivityThread.main(ActivityThread.java:3683)
08-29 14:34:52.805: E/AndroidRuntime(1864):     at java.lang.reflect.Method.invokeNative(Native Method)
08-29 14:34:52.805: E/AndroidRuntime(1864):     at java.lang.reflect.Method.invoke(Method.java:507)
08-29 14:34:52.805: E/AndroidRuntime(1864):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)
08-29 14:34:52.805: E/AndroidRuntime(1864):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
08-29 14:34:52.805: E/AndroidRuntime(1864):     at dalvik.system.NativeStart.main(Native Method)
08-29 14:34:52.805: E/AndroidRuntime(1864): Caused by: java.lang.NullPointerException
08-29 14:34:52.805: E/AndroidRuntime(1864):     at com.virtualstore.Main.onCreate(Main.java:25)
08-29 14:34:52.805: E/AndroidRuntime(1864):     at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
08-29 14:34:52.805: E/AndroidRuntime(1864):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1611)
08-29 14:34:52.805: E/AndroidRuntime(1864):     ... 11 more
11
  • what do you have on line 25 in Main.java? Commented Aug 29, 2013 at 9:11
  • Have you add this activity with manifest file?? Commented Aug 29, 2013 at 9:12
  • I think you should call the addTab method before this line! Commented Aug 29, 2013 at 9:13
  • @AndroSelva i tried both but no changes Commented Aug 29, 2013 at 9:14
  • 1
    call this setTabs(); before getting child..... Commented Aug 29, 2013 at 9:14

2 Answers 2

0

@Priya try this code

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

    //setContentView(R.layout.activity_main);

    TabHost mTabHost = getTabHost();

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


}
Sign up to request clarification or add additional context in comments.

2 Comments

might be get tab before add tabs.
Actually i am trying to create center raised tab ,i followed this link stackoverflow.com/questions/10764046/…
0

I got Solution..... Use this:

private void addTab(String labelId, int drawableId, Class<?> c)
{
    TabHost mTabHost = getTabHost();
    intent = new Intent(this, c);

     spec = tabHost.newTabSpec("tab" + labelId);    

    View tabIndicator = LayoutInflater.from(this).inflate(R.layout.tab_indicator, getTabWidget(), false);
    /*TextView title = (TextView) tabIndicator.findViewById(R.id.title);
    title.setText(labelId);*/
    ImageView icon = (ImageView) tabIndicator.findViewById(R.id.icon);
    icon.setImageResource(drawableId);

    spec.setIndicator(tabIndicator);
    spec.setContent(intent);
    tabHost.addTab(spec);
}
} 

find Tab host id in addTab method and remove from onCreate() method...

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.