Question

I'm trying to develop a code like when I like on the Add button it will Add the Tab and and there is Del button to Delete the tab..But It work fine for 3 to 4 time but after that it throws the error...

and Log-Cat look Like this:-

02-28 16:33:37.832: E/AndroidRuntime(7830): FATAL EXCEPTION: main
02-28 16:33:37.832: E/AndroidRuntime(7830): java.lang.NullPointerException
02-28 16:33:37.832: E/AndroidRuntime(7830):     at android.widget.TabWidget.focusCurrentTab(TabWidget.java:461)
02-28 16:33:37.832: E/AndroidRuntime(7830):     at android.widget.TabHost.setCurrentTab(TabHost.java:410)
02-28 16:33:37.832: E/AndroidRuntime(7830):     at android.widget.TabHost$2.onTabSelectionChanged(TabHost.java:154)
02-28 16:33:37.832: E/AndroidRuntime(7830):     at android.widget.TabWidget$TabClickListener.onClick(TabWidget.java:546)
02-28 16:33:37.832: E/AndroidRuntime(7830):     at android.view.View.performClick(View.java:4223)
02-28 16:33:37.832: E/AndroidRuntime(7830):     at android.view.View$PerformClick.run(View.java:17487)
02-28 16:33:37.832: E/AndroidRuntime(7830):     at android.os.Handler.handleCallback(Handler.java:800)
02-28 16:33:37.832: E/AndroidRuntime(7830):     at android.os.Handler.dispatchMessage(Handler.java:100)
02-28 16:33:37.832: E/AndroidRuntime(7830):     at android.os.Looper.loop(Looper.java:194)
02-28 16:33:37.832: E/AndroidRuntime(7830):     at android.app.ActivityThread.main(ActivityThread.java:5371)
02-28 16:33:37.832: E/AndroidRuntime(7830):     at java.lang.reflect.Method.invokeNative(Native Method)
02-28 16:33:37.832: E/AndroidRuntime(7830):     at java.lang.reflect.Method.invoke(Method.java:525)
02-28 16:33:37.832: E/AndroidRuntime(7830):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:833)
02-28 16:33:37.832: E/AndroidRuntime(7830):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:600)
02-28 16:33:37.832: E/AndroidRuntime(7830):     at dalvik.system.NativeStart.main(Native Method)

and

MainActivity.java

package com.example.sampletab;

import android.app.TabActivity;
import android.content.Intent;
import android.graphics.Color;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.Button;
import android.widget.TabHost;
import android.widget.TabHost.OnTabChangeListener;
import android.widget.TabHost.TabSpec;

@SuppressWarnings("deprecation")
public class MainActivity extends TabActivity {
    private static int tabIndex = 0;
    private TabHost tabHost;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        tabHost = getTabHost();

        tabHost.setOnTabChangedListener(new OnTabChangeListener() {

            @Override
            public void onTabChanged(String tabId) {

                // set the First Tab as selected Tab.
                setSelectedTabColor();
                // setTabColor(tabHost);
            }
        });
        // setTabColor(tabHost);

        addTabs();
        delTabs();

        ((Button) findViewById(R.id.add_tab))
                .setOnClickListener(new View.OnClickListener() {

                    @Override
                    public void onClick(View v) {
                        // TODO Auto-generated method stub
                        tabIndex++;
                        addTabs();
                    }
                });
        ((Button) findViewById(R.id.del_tab))
                .setOnClickListener(new View.OnClickListener() {

                    @Override
                    public void onClick(View v) {
                        // TODO Auto-generated method stub
                        delTabs();
                    }
                });
    }

    protected void setSelectedTabColor() {
        // TODO Auto-generated method stub
        for (int i = 0; i < tabHost.getTabWidget().getChildCount(); i++) {
            tabHost.getTabWidget().getChildAt(i)
                    .setBackgroundColor(Color.LTGRAY);
        }
        tabHost.getTabWidget().getChildAt(tabHost.getCurrentTab())
                .setBackgroundColor(Color.RED);
    }

    private void delTabs() {
        tabHost.getTabWidget().removeView(
                tabHost.getTabWidget().getChildTabViewAt(
                        tabHost.getCurrentTab()));

    }
    private void addTabs() {
        // TODO Auto-generated method stub
        LayoutInflater layoutInflater = LayoutInflater.from(MainActivity.this);
        Button tabBtn = (Button) layoutInflater.inflate(R.layout.tab_event,
                null);
        tabBtn.setText("Tabs" + tabIndex);
        Intent tabintent = new Intent(MainActivity.this, BlankActivity.class);
        setupTab(tabBtn, tabintent, "Tab" + tabIndex);
    }

    private void setupTab(View tabBtn, Intent setIntent, String tag) {
        // TODO Auto-generated method stub
        TabSpec setContent = tabHost.newTabSpec(tag).setIndicator(tabBtn)
                .setContent(setIntent);
        tabHost.addTab(setContent);
    }
}
Was it helpful?

Solution

You need to set the divider drawable BEFORE you add tabs to the TabHost.

So the following line:

tabHost.getTabWidget().setDividerDrawable(R.color.light);

should be moved up. You can call if directly after:

TabHost tabHost = getTabHost();
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top