ANDROID:自定义选项卡设计在应用程序启动时崩溃
我有一个bin试图自定义我的标签的外观,我没有得到任何错误。但当我尝试运行应用程序时,它崩溃了。这就是我得到的:ANDROID:自定义选项卡设计在应用程序启动时崩溃,android,layout,tabs,Android,Layout,Tabs,我有一个bin试图自定义我的标签的外观,我没有得到任何错误。但当我尝试运行应用程序时,它崩溃了。这就是我得到的: setContentView(R.layout.activity_main); tabHost = (TabHost) findViewById(android.R.id.tabhost); 以及: 注释掉的部分是我在使用标准选项卡设计之前所做的。 到目前为止,.xml部分应该没有问题,因为到目前为止它100%基于我在网络中挖掘的其他内容。您必须在createTabContent{
setContentView(R.layout.activity_main);
tabHost = (TabHost) findViewById(android.R.id.tabhost);
以及:
注释掉的部分是我在使用标准选项卡设计之前所做的。
到目前为止,.xml部分应该没有问题,因为到目前为止它100%基于我在网络中挖掘的其他内容。您必须在createTabContent{}方法中指定视图。……视图返回null,这就是它崩溃的原因。…私有void setupTab(最终视图,最终字符串标签){ } 像这样
private void setUpTabs() {
// tabHost.setup();
//
// TabSpec spec1 = tabHost.newTabSpec("TAB 1");
// spec1.setContent(R.id.tab_1);
// spec1.setIndicator("", getResources().getDrawable(R.raw.home));
//
// TabSpec spec2 = tabHost.newTabSpec("TAB 2");
// spec2.setContent(R.id.tab_2);
// spec2.setIndicator("", getResources().getDrawable(R.raw.most));
// TabSpec spec3 = tabHost.newTabSpec("TAB 3");
// spec3.setContent(R.id.tab_3);
// spec3.setIndicator("", getResources().getDrawable(R.raw.favorites));
// TabSpec spec4 = tabHost.newTabSpec("TAB 4");
// spec4.setContent(R.id.tab_4);
// spec4.setIndicator("",
// getResources().getDrawable(R.raw.search_icon));
// tabHost.addTab(spec1);
// tabHost.addTab(spec2);
// tabHost.addTab(spec3);
// tabHost.addTab(spec4);
// tabHost.setOnTabChangedListener(this);
setContentView(R.layout.activity_main);
tabHost = (TabHost) findViewById(android.R.id.tabhost);
setupTab(new TextView(this), "Tab 1");
setupTab(new TextView(this), "Tab 2");
setupTab(new TextView(this), "Tab 3");
}
private void setupTab(final View view, final String tag) {
View tabview = createTabView(tabHost.getContext(), tag);
TabSpec setContent = tabHost.newTabSpec(tag).setIndicator(tabview).setContent(new TabContentFactory() {
public View createTabContent(String tag) {
return view;
}
});
tabHost.addTab(setContent);
}
private static View createTabView(final Context context, final String text) {
View view = LayoutInflater.from(context)
.inflate(R.xml.tabs_bg, null);
TextView tv = (TextView) view.findViewById(R.id.tabsText);
tv.setText(text);
return view;
}
View tabview = createTabView(tabHost.getContext(), tag);
TabSpec setContent = tabHost.newTabSpec(tag).setIndicator(tabview).setContent(new TabContentFactory() {
public View createTabContent(String tag) {
final TextView tv = new TextView(this);
tv.setText("Content for tab with tag " + tag);
return view;
}
});
tabHost.addTab(setContent);