0

I am trying to create an application with a login screen that appears and has effects among various other tabs.

I have the tabbed implementation working fine: I can hide tabs and view the layouts of other tabs -- however, I cannot make them reappear. The program runs fine when I do not attempt button handling, but whenever I start my listener, I force-close.

Attached is the problem code: I can't find exactly where I'm getting the force close from, but I do know it is in the button listener area.

Essentially, I want the tabs to appear if the user's name and password are correct.

What you are seeing is one of four tabs. Specifically, you are looking at the login screen tab.

CODE:

public class LoginActivity extends Activity {

TabHost tabHost = (TabHost) findViewById(android.R.id.tabhost);

public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    setContentView(R.layout.login);

    final Button btn = (Button) findViewById(R.id.login_button);

    btn.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View v) {

            EditText txt_username = (EditText) findViewById(R.id.txt_username);
            EditText txt_password = (EditText) findViewById(R.id.txt_username);

            if (txt_username.toString().equals("Bob")
                    && txt_password.toString().equals("123")) {
                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);
            }
        }
    });
}
}

Attached is the logcat (very long)

04-12 17:07:31.465: ERROR/AndroidRuntime(1480): FATAL EXCEPTION: main
04-12 17:07:31.465: ERROR/AndroidRuntime(1480): java.lang.RuntimeException: Unable to start activity ComponentInfo{android.waiter/android.waiter.waiter}: java.lang.RuntimeException: Unable to start activity ComponentInfo{android.waiter/android.waiter.LoginActivityGroup}: java.lang.RuntimeException: Unable to instantiate activity ComponentInfo{android.waiter/android.waiter.LoginActivity}: java.lang.NullPointerException

04-12 17:07:31.465: ERROR/AndroidRuntime(1480):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2663)

04-12 17:07:31.465: ERROR/AndroidRuntime(1480):     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2679)

04-12 17:07:31.465: ERROR/AndroidRuntime(1480):     at android.app.ActivityThread.access$2300(ActivityThread.java:125)

04-12 17:07:31.465: ERROR/AndroidRuntime(1480):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2033)

04-12 17:07:31.465: ERROR/AndroidRuntime(1480):     at android.os.Handler.dispatchMessage(Handler.java:99)

04-12 17:07:31.465: ERROR/AndroidRuntime(1480):     at android.os.Looper.loop(Looper.java:123)

04-12 17:07:31.465: ERROR/AndroidRuntime(1480):     at android.app.ActivityThread.main(ActivityThread.java:4627)

04-12 17:07:31.465: ERROR/AndroidRuntime(1480):     at java.lang.reflect.Method.invokeNative(Native Method)

04-12 17:07:31.465: ERROR/AndroidRuntime(1480):     at java.lang.reflect.Method.invoke(Method.java:521)

04-12 17:07:31.465: ERROR/AndroidRuntime(1480):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868)

04-12 17:07:31.465: ERROR/AndroidRuntime(1480):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)

04-12 17:07:31.465: ERROR/AndroidRuntime(1480):     at dalvik.system.NativeStart.main(Native Method)
tshepang
  • 12,111
  • 21
  • 91
  • 136
BPuryear
  • 53
  • 1
  • 3
  • 7
  • can you show us your `logcat` output – Tanmay Mandal Apr 12 '11 at 05:33
  • @Tanmay-Mandal, It appears I am having the same problem as: http://stackoverflow.com/questions/5316722/using-button-in-tab-layout-force-closes-application – BPuryear Apr 12 '11 at 22:13
  • `TabHost tabHost = (TabHost) findViewById(android.R.id.tabhost);` you can not access tab host like this you need to `extends` `TabActivity` and then use `TabHost tabHost = getTabHost();` – Tanmay Mandal Apr 13 '11 at 05:15

1 Answers1

0

I suggest you to have two seperate activities as login and other activity with tabs. So your code will have just a simple change:

btn.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View v) {

            EditText txt_username = (EditText) findViewById(R.id.txt_username);
            EditText txt_password = (EditText) findViewById(R.id.txt_username);

            if (txt_username.toString().equals("Bob")
                    && txt_password.toString().equals("123")) {
                startActivity(new Intent(this,YourTabActivity.class));
            }
        }
    });

I use TabLayout as tab-style view that is quite easy to implement. But feel free to use which view you prefer and you know better

Cenk ŞİMŞEK
  • 371
  • 2
  • 4