Intent Does Not Start in If-else

i'm a beginner on Android and Java. My problem is:

In my mainActivity class, i have a click event listener. And i'm trying to open an intent if the editText values are right (something like username,password) My code works when there's no if statement and starts the other activity but when i use that username password check, it is not running second activity. It's just waiting...

¿Qué tiene de malo?


import android.os.Bundle;
import android.annotation.SuppressLint;
import android.content.Intent;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;
import android.text.InputFilter.LengthFilter;

public class MainActivity extends Activity {

public EditText username;
public EditText password;   
public TextView tw1;

public void onCreate(Bundle savedInstanceState) {

    Button loginButton=(Button)findViewById(;

    loginButton.setOnClickListener(new OnClickListener() {

        public void onClick(View v) {
            // TODO Auto-generated method stub

            String u_name=username.getText().toString();
            String pass=password.getText().toString();

            if(u_name=="kemal" && pass=="sifre"){

                Intent i=new Intent(MainActivity.this,SecondActivity.class);




public boolean onCreateOptionsMenu(Menu menu) {
    getMenuInflater().inflate(, menu);
    return true;


preguntado el 31 de julio de 12 a las 14:07

4 Respuestas

utilizan el String.equals(String str) en lugar de == for comparing Strings

Respondido 01 ago 12, 18:08

Will you please stop approving bad edits como esta? You're harming the site by letting these edits go through. You're not using the markdown editor correctly yourself, so you shouldn't be reviewing other people's edits at all. - user229044

Strings cannot be compared with = operator like integer values.

They can be comparedwith .equals Método

 if(u_name.equals("kemal") && pass.equals("sifre")){

            Intent i=new Intent(MainActivity.this,SecondActivity.class);

Respondido 31 Jul 12, 14:07

This is right solution, because of operator == compare adressess in memory for strings, not content. - Alex Klimashevski

prueba este

if(u_name.equals("kemal") && pass.equals("sifre"))
    Intent i=new Intent(MainActivity.this,SecondActivity.class);


Respondido 31 Jul 12, 14:07

Yes I agree with the other answers here. You should not use == for comparing strings at any cost.

equals() is the method implemented for comapring strings. And why this is insisted because,

"==" compares the reference value of string object and equals() method compares content of the string object

the “==” operator is used to compare 2 objects, it checks to see if the objects refer to the same place in memory.

But, the equals method is actually meant to compare the contents of 2 objects, and not their location in memory.

So obviously You have to use something like,

if(u_name.equals("kemal") && pass.equals("sifre")){ //your code here }

Respondido el 20 de junio de 20 a las 10:06

Thanks for your brief explanation. So it's something about Java that i have to learn. - soñar

Yes.One could say Android coding depends on Java knowledge also. - andro selva

No es la respuesta que estás buscando? Examinar otras preguntas etiquetadas or haz tu propia pregunta.