Android -- Changing Tab Visibility from Inside Tab Activities
I'm currently attempting to change tabs from inside of other activities -- that much is straight forward. However, I'm having trouble when attempting to set the visibility of other tabs.
Essentially, I have an application load to a tab (login page) and the other tabs are invisible until the user logs in.
When the user logs in, I want to make the other tabs visible and the login tab invisible.
If there is a simple way of doing this, please make me aware of it--I'm currently running around in circles:
Code Snippets:
In the TabHost's OnCreate:
.....
tabHost.setCurrentTab(3);
tabHost.getTabWidget().getChildAt(0).setVisibility(View.GONE);
tabHost.getTabWidget().getChildAt(1).setVisibility(View.GONE);
tabHost.getTabWidget().getChildAt(2).setVisibility(View.GONE);
....
The TabHost's Extra Methods:
public void switchTab(int tab){
tabHost.setCurrentTab(tab);
}
public void visibleTabs(){
tabHost.getTabWidget().getChildAt(0).setVisibility(View.VISIBLE);
tabHost.getTabWidget().getChildAt(1).setVisibility(View.VISIBLE);
tabHost.getTabWidget().getChildAt(2).setVisibility(View.VISIBLE);
tabHost.getTabWidget().getChildAt(3).setVisibility(View.GONE);
}
public void invisibleTabs(){
tabHost.getTabWidget().getChildAt(0).setVisibility(View.GONE);
tabHost.getTabWidget().getChildAt(1).setVisibility(View.GONE);
tabHost.getTabWidget().getChildAt(2).setVisibility(View.GONE);
}
The Tab Activity:
public class LoginActivity extends Activity {
EditText txt_username;
EditText txt_password;
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.login);
Button btn = (Button)this.findViewById(R.id.login_button);
final EditText txt_username = (EditText) findViewById(R.id.txt_username);
final EditText txt_password = (EditText) findViewById(R.id.txt_username);
btn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
String username = txt_username.getText().toString();
String password = txt_password.getText().toString();
switchTabInActivity(0);
}
});
}
public void switchTabInActivity(long indexTabToSwitchTo){
waiter ParentActivity = (waiter) this.getParent();
ParentActivity.visibleTabs();
ParentActivity.switchTab(0);
}
}
LogCat is As Follows:
04-12 19:37:49.825: ERROR/AndroidR开发者_开发技巧untime(2744): FATAL EXCEPTION: main
04-12 19:37:49.825: ERROR/AndroidRuntime(2744): java.lang.NullPointerException
04-12 19:37:49.825: ERROR/AndroidRuntime(2744): at android.waiter.waiter.visibleTabs(waiter.java:130)
04-12 19:37:49.825: ERROR/AndroidRuntime(2744): at android.waiter.LoginActivity.switchTabInActivity(LoginActivity.java:56)
04-12 19:37:49.825: ERROR/AndroidRuntime(2744): at android.waiter.LoginActivity$1.onClick(LoginActivity.java:44)
Its obvious that the exception is coming from the invisible/visible settings.
I don't know if tabHost.getTabWidget().getChildAt(0)
is the right way to do it. Have you tried giving the tabs an individual ID?
alternatively, if you want to try and track the exact location of the error throw a bunch of breakpoints and run it in Debug mode.
Oh Button btn = (Button)this.findViewById(R.id.login_button);
I believe can do without a "this" (although I could be wrong.... fwiw i never used this
in a button declaration)
精彩评论