当前位置: 动力学知识库 > 问答 > 编程问答 >

android - Invalid login id even if I am inserting the right credentials

问题描述:

I designed a login form in android which takes employee id (an integer value) and a password. But even if I enter the exact credentials it doesn't move to the next activity. It also prints the correct empid in logcat. But always the last Toast message is shown.

protected void onCreate(Bundle savedInstanceState)

{

super.onCreate(savedInstanceState);

setContentView(R.layout.login_activity);

submit = (Button) findViewById(R.id.submit);

empId_edt = (EditText) findViewById(R.id.emp_id);

password_edt = (EditText) findViewById(R.id.password);

submit.setOnClickListener(new OnClickListener()

{

@Override

public void onClick(View v)

{

empid = Integer.parseInt(empId_edt.getText().toString().trim());

System.out.println(empid);

password = password_edt.getText().toString().trim();

if(empid != null && empid>=10000 && empid<=10130)

{

if(password != null && password.length() > 0)

{

if(empid == 10001 && password == "hrms")

{

Intent intent = new Intent(LoginActivity.this, HomeScreenActivity.class);

startActivity(intent);

}

else

{

Toast.makeText(LoginActivity.this, "Please enter valid Employee Id & Password", Toast.LENGTH_SHORT).show();

}

}

else

{

Toast.makeText(LoginActivity.this, "Please enter a valid password", Toast.LENGTH_SHORT).show();

}

}

else

{

Toast.makeText(LoginActivity.this, "Please enter a valid Employee Id", Toast.LENGTH_SHORT).show();

}

}

});

}

网友答案:

Use password.equals("hrms") instead password=="hrms".

A string can not be compared with "=" symbol. You need to use built in methods for equality checks for String

网友答案:

The variable empid should be an integer. You can't check it for null (you can check it for 0). Instead, you can check empId_edt.getText() for null (you should do it before parsing to integer). And yeah, You should put a try-catch around Integer.parseInt(). and compare strings with .equals() rather than ==. These things should fix your code. Thanks to comment and another answer.

网友答案:

This gives answer of your question **The JVM is caching Integer values. == only works for numbers between -128 and 127 **

So use if (empid.intValue() == 10001)

Android two integers with same value not equating properly

分享给朋友:
您可能感兴趣的文章:
随机阅读: