2016-03-31 17 views
0

私のアプリケーションでTabHostを使用してタブを実装しようとしていますが、例外。下に私はコードを持っています。誰でも助けてくれますか?nullオブジェクトリファレンスで仮想メソッド 'void android.widget.TabWidget.addView(android.view.View)'を呼び出そうとしました

final TabHost tabHost = (TabHost)findViewById(android.R.id.tabhost); 

    final TabHost.TabSpec tab1 = tabHost.newTabSpec("First Tab"); 
    final TabHost.TabSpec tab2 = tabHost.newTabSpec("Second Tab"); 


    // Set the Tab name and Activity 
    // that will be opened when particular Tab will be selected 

    tab1.setIndicator("Upcoming Matches").setContent(new Intent(this, ViewOtherExpense.class)); 
    tab2.setIndicator("Recent Matches").setContent(new Intent(this, viewExpenseTypes.class)); 

    /** Add the tabs to the TabHost to display. */ 
    tabHost.addTab(tab1); 
    tabHost.addTab(tab2); 
    tabHost.setup(); 
    tabHost.getTabWidget().getChildAt(0).setBackgroundColor(Color.parseColor("#3399FF")); //selected 
    tabHost.getTabWidget().getChildAt(1).setBackgroundColor(Color.parseColor("#ADD6FF")); //unselected 
    //To change the Tab color 
    tabHost.setOnTabChangedListener(new TabHost.OnTabChangeListener() { 
     @Override 
     public void onTabChanged(String tabId) { 
      // TODO Auto-generated method stub 
      for (int i = 0; i < tabHost.getTabWidget().getChildCount(); i++) { 
       tabHost.getTabWidget().getChildAt(i).setBackgroundColor(Color.parseColor("#ADD6FF"));//unselected 

      } 
      tabHost.getTabWidget().getChildAt(tabHost.getCurrentTab()).setBackgroundColor(Color.parseColor("#3399FF")); // selected 

     } 


    }); 
+0

を呼び出す前にtabHost.setup();を追加することをお勧め;'初期化後。 –

答えて

1

あなたはロードTabHostfindViewById()を使用している場合は、タブを追加する前にsetup()を呼び出す必要があります。

だから私は、あなたが初期化後と `tabHost.setup()を呼び出してみtabHost.addTab()

ここのような
final TabHost tabHost = (TabHost)findViewById(android.R.id.tabhost); 
tabHost.setup(); 
関連する問題

 関連する問題