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); 
} 

我该如何达到我的要求?

回答

0

你不使用XML布局吗?这比你有什么上面更加高效和整洁......

您还没有任何的logcat所以没有太多去上,但在我看来,这个问题是你addTabItem试图使一些武断和使用它作为内容时,你真的想要使用frameLayout作为内容,所以尝试将其传入方法并将其设置为内容

相关问题