Hide tabs in Android tabhost
•
Android
if( ......)
{
tabHost.getTabWidget().getChildAt(0).setVisibility(View.GONE);
//to hide the first tab in the TabHost
}
Is there anything wrong with this code? When I added this code in the oncreate() method, the application crashed. Any ideas?
My logcat:
05-31 22:03:38.471: E/AndroidRuntime(598): Caused by: java.lang.NullPointerException 05-31 22:03:38.471: E/AndroidRuntime(598): at swayam.dev.mushtouch.MushTouchActivity.setVisibilityControls(MushTouchActivity.java:75) 05-31 22:03:38.471: E/AndroidRuntime(598): at swayam.dev.mushtouch.MushTouchActivity.onCreate(MushTouchActivity.java:220) 05-31 22:03:38.471: E/AndroidRuntime(598): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047) 05-31 22:03:38.471: E/AndroidRuntime(598): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1611)
I also tried this code. It still crashes
getTabHost().getTabWidget().removeViewAt(0);
resolvent:
My answer was too long, so I got sick
So far you have
TabHost tabHost = (TabHost)findViewById(android.R.id.tabhost);
tabHost.getTabWidget().getChildAt(0).setVisibility(View.GONE);
You are getting a NullPointerException, which means that whenever you use this line of code, you will try to change something that does not exist on the screen, or maybe something off the screen
Check the import statement of R
Under your package declaration, you should have the following:
import your.package.R;
Not at all
import android.R;
After repair, when you reference the label host, use the following:
TabHost tabHost = (TabHost)findViewById(R.id.tabhost);
If this does not work, make sure that tabhost is actually on the screen and that you are not in a separate activity
The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
二维码