以下は私のTabクラスです。選択した場合と選択しない場合のタブの色を変更しようとしています。しかし、tabHost.setOnTabChangedListener(MyOnTabChangeListener)?を呼び出すと、アプリがクラッシュします。そのメソッドでアプリを起動することすらできず、nullpointerexceptionが発生します。どうしたらいいのかわからない?何か案は?/よろしく
@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 ) );
}
}
}
}