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作爲內容,所以嘗試將其傳入方法並將其設置爲內容

相關問題