2012-09-06 79 views
1

以下是我的Tab類。我試圖改變選項卡的顏色,當選中和取消選中。但是,當我撥打tabHost.setOnTabChangedListener(MyOnTabChangeListener)?時,該應用程序崩潰。我甚至無法用該方法啓動應用程序,並且它給出了一個nullpointerexception。我不知道該怎麼辦?有任何想法嗎? /問候試圖更改Android中的選項卡上的顏色

@SuppressWarnings("deprecation") 
public class Tabs extends TabActivity 
{ 
private static final String TAG = "TabHostActivity"; 
private boolean mHaveShownStartDialog = false; 
static TabHost tabHost; 

@Override 
public void onCreate(Bundle savedInstanceState) 
{ 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.tab_host); 
    setOnCreatePreferences(); 

    try 
    { 

     addTab(getString(R.string.Search), R.drawable.searchtab, SearchTask.class); 
     addTab(getString(R.string.Bookmarks), R.drawable.blackheart1, Bookmarks.class); 
     addTab(getString(R.string.Latest), R.drawable.clock3, Latest.class); 
     addTab(getString(R.string.QAndA), R.drawable.pen, LatestFeedback.class); 

     getTabHost().setCurrentTab(0); 
     tabHost.setOnTabChangedListener(MyOnTabChangeListener); 


    } 
    catch(Exception e) 
    { 
     Log.e(TAG, e.getMessage()); 
    } 


} 

public static OnTabChangeListener MyOnTabChangeListener = new OnTabChangeListener(){ 

    public void onTabChanged(String tabId) { 
     for(int i=0;i<tabHost.getTabWidget().getChildCount();i++) 
     { 
      tabHost.getTabWidget().getChildAt(i).setBackgroundColor(Color.WHITE); 
     } 

     tabHost.getTabWidget().getChildAt(tabHost.getCurrentTab()).setBackgroundColor(Color.GRAY); 
    } 
}; 





//public static void setTabColor(TabHost tabhost) { 
// for(int i=0;i<tabhost.getTabWidget().getChildCount();i++) 
// { 
//  tabhost.getTabWidget().getChildAt(i).setBackgroundColor(Color.parseColor("#FF0000")); //unselected 
// } 
// tabhost.getTabWidget().getChildAt(tabhost.getCurrentTab()).setBackgroundColor(Color.parseColor("#0000FF")); // selected 
//} 


private void addTab(CharSequence label, int drawable_id, Class<?> c) 
{ 
    TabHost.TabSpec spec = getTabHost().newTabSpec("tab" + " "+ label); 

    spec.setIndicator(label, getResources().getDrawable(drawable_id)); 

    spec.setContent(new Intent().setClass(this, c)); 

    getTabHost().addTab(spec); 
} 

@Override 
public boolean onCreateOptionsMenu(Menu menu) 
{ 
    MenuInflater inflater = getMenuInflater(); 
    inflater.inflate(R.menu.tabs_menu, menu); 
    return true; 
} 

@Override 
public boolean onOptionsItemSelected(MenuItem item) 
{ 
    switch (item.getItemId()) 
    { 
     case R.id.tabs_menu_options_item: 
      //startActivityForResult(new Intent(this, Options.class) , 0); 
      return true; 

     default: return super.onOptionsItemSelected(item); 
    } 
} 

private void setOnCreatePreferences() 
{ 
    SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(getBaseContext()); 

    boolean mUseStartDialog = preferences.getBoolean("use_dialog", true); 
    if(mUseStartDialog) 
    { 
     if(!mHaveShownStartDialog) 
     { 
      mHaveShownStartDialog = true; 
      startActivity(new Intent(this, WelcomeDialog.class)); 
     } 
    } 
} 

}

回答

2

我不能看到你初始化tabHost變量。

+0

(Ha!Good catch。)這是正確的,NullPointerException是由嘗試調用'null.setOnTabChangedListener()'引起的。 – Sam

+0

乾杯,那真是一個愚蠢的小姐。我現在調用getTabHost.setOnTabChangedListener()。有時你會對自己的代碼失明;)。現在我可以啓動應用程序,但是當我嘗試更改選項卡時,我得到一個新的nullpointerexception,並在行上崩潰:** for(int i = 0; i

+0

請更新您的問題代碼,以瞭解何時以及如何初始化tabhost變量。 另外我不會去TabActivity首先。它缺少平板電腦屏幕所需的片段支持。改爲使用tabhost作爲單獨的小部件。在這裏你可以找到非常容易和可讀的示例代碼如何完成[TabActivity](http://developer.android.com/reference/android/app/TabActivity.html) –

0

不要通過OnTabChangeListener來完成。你不想要調用每次用戶按下選項卡中的代碼;)您也可以直接後

getTabHost().setCurrentTab(0); 

這是寫我用此方案的代碼:

TabWidget widget = tabHost.getTabWidget(); 
int size = widget.getChildCount(); 
for (int i = 0; i < size; i++) { 

    // Hail to the master of unreadable code! 
    ((TextView)((ViewGroup)widget.getChildAt(i)).findViewById(android.R.id.title)).setTextColor(0xffffffff); 
    widget.getChildAt(i).setBackgroundResource(R.drawable.tab_widget_background_selector); 
} 

我是什麼這表明你應該使用選擇器,讓平臺爲你處理所有事情。

<?xml version="1.0" encoding="utf-8"?> 
<selector 
    xmlns:android="http://schemas.android.com/apk/res/android"> 
    <item 
    android:state_selected="true" 
    android:drawable="@drawable/tab_widget_background_selected"/> 
    <item 
    android:drawable="@drawable/tab_widget_background_casual"/> 
    </selector> 
相關問題