I am attempting to create custom colors of the tabs in a TabHost.
I have impletmented OnTabChangeListener
and the MainActivity loads up correctly. However when I click on a new tab I get a null pointer. Where is my error located? I cannot understand what the issue is.
Here is the offending code loosely based off of this example
@Override
public void onTabChanged(String tabId) {
for(int i=0;i<tabHost.getTabWidget().getChildCount();i++)
{
tabHost.getTabWidget().getChildAt(i).setBackgroundDrawable(getResources().getDrawable(R.drawable.greygradient));
}
tabHost.getTabWidget().getChildAt((tabHost.getCurrentTab())).setBackgroundDrawable(getResources().getDrawable(R.drawable.bluegradient));
}
Here is the trace:
06-20 14:27:42.770: E/AndroidRuntime(1490): java.lang.NullPointerException
06-20 14:27:42.770: E/AndroidRuntime(1490): at com.company.app.MainActivity.onTabChanged(MainActivity.java:72)
06-20 14:27:42.770: E/AndroidRuntime(1490): at android.widget.TabHost.invokeOnTabChangeListener(TabHost.java:359)
06-20 14:27:42.770: E/AndroidRuntime(1490): at android.widget.TabHost.setCurrentTab(TabHost.java:344)
06-20 14:27:42.770: E/AndroidRuntime(1490): at android.widget.TabHost$2.onTabSelectionChanged(TabHost.java:132)
06-20 14:27:42.770: E/AndroidRuntime(1490): at android.widget.TabWidget$TabClickListener.onClick(TabWidget.java:456)
06-20 14:27:42.770: E/AndroidRuntime(1490): at android.view.View.performClick(View.java:2485)
06-20 14:27:42.770: E/AndroidRuntime(1490): at android.view.View$PerformClick.run(View.java:9080)