这是我的代码:
public class Main extends Activity {
@Override
public void onCreate(Bundle savedInstanceState) {
TabHost tabHost = new TabHost(this);
TabHost.TabSpec tab = tabHost.newTabSpec("tab1");
tab.setIndicator("Tab 1");
tab.setContent(new TabHost.TabContentFactory() {
@Override
public View createTabContent(String tag) {
TextView tv = new TextView(Main.this);
tv.setText("tab 1 content");
return tv;
}
});
tabHost.addTab(tab);
setContentView(tabHost);
}
}
我遇到了这个错误:
[...]
07-13 20:26:49.261: ERROR/AndroidRuntime(625): Caused by: java.lang.NullPointerException
07-13 20:26:49.261: ERROR/AndroidRuntime(625): at android.widget.TabHost.addTab(TabHost.java:206)
07-13 20:26:49.261: ERROR/AndroidRuntime(625): at test.test.Main.onCreate(Main.java:27)
[...]
我需要通过代码实现这个功能,不能使用XML。请问有谁可以帮我修复一下这段代码吗?