我正在尝试创建中心凸起的标签栏。我搜索了很多最后我得到了这个链接。我按照答案但是得到了空指针异常:
这是代码:
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