0
Button loginbuttonbutton = (Button) findViewById(R.id.btnLogin);
loginbuttonbutton.setOnClickListener(new View.OnClickListener() {
    public void onClick(View view) {
        if(inputEmail.getText().toString() == "EdEffort@ncat.edu" &&
           inputPassword.getText().toString() == "Steelers") {
            Intent myIntent = new Intent(view.getContext(),
                                         Host_Setting_PageActivity.class);
            startActivityForResult(myIntent, 0);
        } else {
            System.out.println("Username or password is incorrect");
        }
    }
});

That is my code and the application actually start but whenever I hit the login button the application closes.

rekire
  • 47,260
  • 30
  • 167
  • 264
user1424296
  • 61
  • 1
  • 2
  • 3
  • 1
    possible duplicate of [How do I compare strings in Java?](http://stackoverflow.com/questions/513832/how-do-i-compare-strings-in-java) ==> Use `.equals` instead of `==` to compare strings. – assylias May 29 '12 at 17:18
  • Everyday there is atleast 1 question on SO on string comparison. – Kazekage Gaara May 29 '12 at 17:22
  • @KazekageGaara - That's not unreasonable - it's an easy mistake to make, and the results don't often look like a string comparison error. For users coming from a language like C++ or C#, recognizing the error isn't in any way intuitive. – derekerdmann May 29 '12 at 17:25
  • @derekerdmann yup. I know. That was just a follow up comment to the possible duplicate comment. I didn't mean to demoralize the OP in any way if that's what you thought it to be. :-) – Kazekage Gaara May 29 '12 at 17:27
  • @KazekageGaara Thats right but sometimes begginer dont know that how to compare string – Samir Mangroliya May 29 '12 at 17:27

4 Answers4

3

first use .equals() to compare strings.

== compares string refrences.Not value.

.equals() = compare strings character equality

if((inputEmail.getText().toString().equals("EdEffort@ncat.edu")&&inputPassword.getText().toString().equals("Steelers"))

And if force close than put logcat here..

Samir Mangroliya
  • 39,918
  • 16
  • 117
  • 134
2

use equals insead of == for String comparison

    loginbuttonbutton.setOnClickListener(new View.OnClickListener() {
        public void onClick(View view) {
            if(inputEmail.getText().toString().equals("EdEffort@ncat.edu") && inputPassword.getText().toString().equals("Steelers") ){
            Intent myIntent = new Intent(YOUR_CURRENT_ACTIVITY.this, Host_Setting_PageActivity.class);
            startActivityForResult(myIntent, 0);
        }
            else{
                System.out.println("Username or password is incorrect");
            }
        }
    });

and make sure you are registering YOUR_CURRENT_ACTIVITY.this and Host_Setting_PageActivity.class both in manifest.xml

ρяσѕρєя K
  • 132,198
  • 53
  • 198
  • 213
0

You should add

Host_Setting_PageActivity.class to your AndroidManifest

And also, you should compare strings always using .equals and not with "==" as "==" will really compare the instance of the string object and .equals will check the value of the string

vanleeuwenbram
  • 1,289
  • 11
  • 19
0

dont use == method in if Condition but used .equals() method like as

if(inputEmail.getText().toString().equals("abc") && inputPassword.getText().toString().equals("abc") ){
----------your code here-----------
}
EzLo
  • 13,780
  • 10
  • 33
  • 38