Android Tabbar中的NullPointer异常

时间:2013-08-29 09:10:53

标签: android nullpointerexception tabbar

我正在尝试创建中心凸起的tabbar。我搜索了很多,最后得到了这个link。我按照答案但得到空指针异常:

以下是代码:

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);
    }
} 

这是错误:

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

2 个答案:

答案 0 :(得分:1)

我得到了解决方案......使用此:

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);
}
} 

在addTab方法中找到Tab主机ID,并从onCreate()方法中删除...

答案 1 :(得分:0)

@Priya 试试这段代码

@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);


}