我创建了一个应用程序,其中包含四个加载了特定 web 视图的选项卡。我计划在标签下方添加广告。在我的代码中,我将内容视图设置为创建为 TabHost 的 ViewGroup。如果我将此视图组添加到线性布局,则应用程序由于 TabHost.add(TabSpec) 获得 NullPointer 异常而崩溃。这是代码。
public View addTabBarView(Context context)
{
m_vForm = _createTABForm(context);
return m_vForm;
}
private ViewGroup _createTABForm(Context context) {
sTabHost = new TabHost(context,null);
sTabHost.setLayoutParams(
new LinearLayout.LayoutParams(
LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT));
HorizontalScrollView sScrollView = new HorizontalScrollView(context);
LinearLayout.LayoutParams sScrollViewParams = new LinearLayout.LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT);
sScrollView.setVerticalScrollBarEnabled(false);
sScrollView.setHorizontalScrollBarEnabled(false);
sScrollView.setScrollBarStyle(TRIM_MEMORY_UI_HIDDEN);
sScrollView.setFillViewport(true);
TabWidget tabWidget = new TabWidget(context);
LinearLayout.LayoutParams sTabWidgetParams = new LinearLayout.LayoutParams(
LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
tabWidget.setId(android.R.id.tabs);
sScrollView.addView(tabWidget, sTabWidgetParams);
sTabHost.addView(sScrollView, sScrollViewParams);
FrameLayout frameLayout = new FrameLayout(context);
frameLayout.setId(android.R.id.tabcontent);
final float scale = context.getResources().getDisplayMetrics().density;
int paddingtop = (int) (64 * scale + 0.5f);
frameLayout.setPadding(0, paddingtop, 0, 0);
sTabHost.addView(frameLayout, new LinearLayout.LayoutParams(
LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT));
sTabHost.setup();
return sTabHost;
}
public addTabItem(final String url, String tabTitle, Drawable tabIcon)
{
TabSpec ts1 = sTabHost.newTabSpec(tabTitle);
if(tabIcon==null)
ts1.setIndicator(tabTitle);
else
ts1.setIndicator(tabTitle,tabIcon);
ts1.setContent(new TabHost.TabContentFactory(){
@SuppressWarnings("deprecation")
public View createTabContent(String tag)
{
//Creating webview inside a layout
}
});
sTabHost.addTab(ts1); //Here throws NullPointer exception.
sTabHost.setOnTabChangedListener(this);
}
我怎样才能达到我的要求。?