Android — Changing Tab Visibility from Inside Tab Activities

泪湿孤枕 提交于 2020-01-15 10:53:47

问题


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/AndroidRuntime(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.


回答1:


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)



来源:https://stackoverflow.com/questions/5643226/android-changing-tab-visibility-from-inside-tab-activities

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!