开发者

Beginner Android Dev question navigating through intents, getting errors not sure how to fix it. I've tried rearranging and everything even tabbing

I created this Sign-In page. I start by declaring variables for username/password & buttons. If user enters "test" as username & "test" as password and hits the login button, its supposed to go to the DrinksTwitter.class activity, else throw error message I created. To me the code and login makes perfect sense. I'm not sure why it wont go to the next activity I want it to go to

package com.android.drinksonme;

import android.app.Activity;

import android.content.Intent;

import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;


public class Screen2 extends Activity {

    // Declare our Views, so we can access them later
    private EditText etUsername;
    private EditText etPassword;
    private Button btnLogin;
    private Button btnSignUp;
    private TextView lblResult;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceSt开发者_如何学运维ate);
        setContentView(R.layout.main);

        // Get the EditText and Button References
                etUsername = (EditText)findViewById(R.id.username);
                etPassword = (EditText)findViewById(R.id.password);
                btnLogin = (Button)findViewById(R.id.login_button);
                btnSignUp = (Button)findViewById(R.id.signup_button);
                lblResult = (TextView)findViewById(R.id.result);

                    // Check Login
                       String username = etUsername.getText().toString();
                       String password = etPassword.getText().toString();

         if(username.equals("test") && password.equals("test")){

            final Intent i = new Intent(Screen2.this, DrinksTwitter.class);
            btnLogin.setOnClickListener(new OnClickListener() {
                public void onClick(View v) {     
                startActivity(i);              
                }

                  // lblResult.setText("Login successful.");        

                        else { /* ERROR- Syntax error on token "else", { expected */
                                    lblResult.setText("Invalid username or password.");
                                }
                            }
                        });

                        final Intent k = new Intent(Screen2.this, SignUp.class);
                        btnSignUp.setOnClickListener(new OnClickListener() {
                            public void onClick(View v) { 
                                startActivity(k);
                            }
                        }); /* ERROR- Syntax error, insert "}" to complete Statement*/
    }
}


I only briefly looked at your code, but I don't understand what you're doing (in that order).

You're checking the password & username on the OnCreate method, which, barring any autofilling, the textboxes will be blank.

Then you declare your onClickListeners.

Try restructuring it to be something like this (pseudo-code):

OnCreate
    user = usertextbox
    pass = passtextbox
    login.setOnClick
        if user == validUsername && pass == validPass
            Intent i = new Intent(blah.this, blah.class)
            startActivity(i)
        else
            Intent i = new Intent(blah.this, login.class)
            startActivity(i)

This is how I have my app's login screen.

In short, you have to make the check occur in the OnClickListener of the action button, not in the OnCreate method of the activity. The way it is now, the form is created, and the textboxes are blank. Then immediately afterwards, they're compared against some values. That call will always fail to execute, so all of the code in your If statement scope will be ignored indefinitely.

Also, the reason Eclipse is giving you that error is because you're trying to place an else statement inside of a class definition.

When you declare a new OnClickListener() { }, you're essentially declaring a class. So you're in the class scope, not the if scope.

That would be like trying to do:

class test {
    else {
        doStuff();
    }
}


I'm almost positive your problem is in setting an onClick() call instead of just starting the activity. It seems a bit complex of a call, and I think you're already checking the conditions in the appropriate place. Why do you need another onClick() call?


You should indent your code correctly. That syntax error is because you don't have a closing brace around the btnLogin.setOnClickListener. You then miss at least a couple more closing brackets.

Also, as others have posted, you check the edittext value before the onCreate() method even gives up control to the UI.

You set the button's click listener only if the strings match.

To me the code and login makes perfect sense

You should slow down and take a look more carefully. It can't possibly make perfect is sense if there's syntax errors.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜