4
 public class SuperActivity extends Activity{

    @Override
    public void onCreate(Bundle savedInstanceState) {
         super.onCreate(savedInstanceState);
         setContentView(R.layout.main);

         Button registerButton = (Button) findViewById(R.id.register_button);
         registerButton.setOnClickListener(new View.OnClickListener() {
            public void onClick(View view) {
                 Intent myIntent = new Intent(SuperActivity.this, Register.class);
                 startActivity(myIntent);
            }

         });

         Button loginButton = (Button) findViewById(R.id.login_button);
         loginButton.setOnClickListener(new View.OnClickListener() {
            public void onClick(View view) {
                 Intent myIntent = new Intent(SuperActivity.this, Login.class);
                 startActivity(myIntent);
            }
         });

     }
  }

My register buttons works but not the login button. Is something wrong with my code?

mazaneicha
  • 8,794
  • 4
  • 33
  • 52
  • Can you elaborate on "doesnt work"? – mazaneicha Nov 05 '11 at 20:44
  • @mazaneicha no errors in the code but when I run it in the emulator I press the register button and it shows the register page but when I press the login button it shuts the emulator down unexpectedly – Shaye Robinson Nov 05 '11 at 20:47
  • What does your logcat shows? Can you post that? – dark_shadow Nov 05 '11 at 21:04
  • My logcat is an error at theis line:loginButton.setOnClickListener(new View.OnClickListener() { – Shaye Robinson Nov 05 '11 at 21:18
  • @dark_shadow the logcat says Login.OnCreate at this line:loginButton.setOnClickListener(new View.OnClickListener() { is causing an error – Shaye Robinson Nov 05 '11 at 21:20
  • @dark_shadow the other errors are:android.app.ActivityThread.performLaunchActivity & android.app.Instrumentation.callActivityOnCreate – Shaye Robinson Nov 05 '11 at 21:22
  • Can you show me the logcat? I'm still not getting you. – dark_shadow Nov 05 '11 at 21:29
  • I'm guessing that you *did* declare the login button in an xml file, but just *not* in `main.xml`, as some have already suggested below. Without the full LogCat error the most logical flaw is a `NullPointerException` on loginButton, which means `findViewById(R.id.login_button)` failed to find your button in the Activity's layout. – MH. Nov 06 '11 at 01:03
  • @MH how do I display the logcat? – Shaye Robinson Nov 06 '11 at 01:29
  • Are you using Eclipse? If so, please read [this](http://stackoverflow.com/questions/3280051/how-to-enable-logcat-console-in-eclipse-for-android) on how to enabled the LogCat view. Alternatively you can use `adb logcat` to display it on a command line, or `adb logcat E` if you just want to see the errors logged. – MH. Nov 06 '11 at 01:36

4 Answers4

0

The code posted looks fine. What exactly doesn't work? Do you get an error?

Since you mention it works for 'register' but not for 'login', you may want to double check if you didn't forget to add Login to your manifest.

MH.
  • 45,303
  • 10
  • 103
  • 116
0

Is button with id login_button declared in main layout? If I had to guess this button is not declared in main layout is declared somewhere else.

double check if button with id login_button declared in main layout

Pedro Rainho
  • 4,234
  • 1
  • 19
  • 21
0

Since you are not posting your logcat view,it's very difficult to see where the problem this.I'm suggesting you few things which you should make sure that are inplace as said by me.If not then please correct them and let me know if that solved your problem or not.

Firstly,you should make sure that you have declared a button with same id in your main.xml file.It will look something like this:

<button android:id="@+id/login_button" >
</button>

Secondly you should make sure that you have declared your Login.class in your AndroidManifest.xml file.It will look something like this:

<application>

<activity android:name=".Login"></activity>

</application>

Check if these things are in place or not.

dark_shadow
  • 3,503
  • 11
  • 56
  • 81
  • I have everything declared so I still dnt know what the problem is... How do you show the logcat? It would it help if I told you I have 5 buttons on my menu but I'm only trying to get login/register buttons to work – Shaye Robinson Nov 05 '11 at 23:26
  • You can enable logcat by following the steps given here : http://stackoverflow.com/questions/3280051/how-to-enable-logcat-console-in-eclipse-for-android And after you enable your logcat paste the output of that so that I can help you. – dark_shadow Nov 06 '11 at 08:26
  • Thanks everyone for your help it finally works now I dont know why it wasn't working before I didn't change anything.... – Shaye Robinson Nov 06 '11 at 18:45
0

Have you declared all the activities? ;)

ArcDare
  • 3,106
  • 4
  • 27
  • 38