3
我创建了一个应用程序,其中包含四个加载了特定WebView的选项卡。我计划在标签下添加广告。在我的代码中,我将内容视图设置为创建为TabHost的ViewGroup。如果我将这个Viewgroup添加到线性布局,由于TabHost.add(TabSpec)得到NullPointer异常,应用程序已经崩溃。这是代码。将TabHost控件添加到Linearlayout /在tabhost选项卡下添加布局
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);
}
我该如何达到我的要求?