有人可以帮我解决下面写的错误“IF CODE”每次都不会进入块里面,即使是之前的值if也会记录在控制台中。
if(userNameString == userInstance.getUsername() && passwordString == userInstance.getPassword())
以下是完整的代码:
if(userInstance == null)
{
Log.i("no record found againse User Instance",""+userInstance);
Toast.makeText(getApplicationContext(),
"User Id and Password is wrong", Toast.LENGTH_SHORT).show();
getLoaderManager().destroyLoader(arg0.getId());
}
else
{
Log.i("Record found againse User Instance 1st",""+userInstance + "userName:" + userNameString +":pass:::"+passwordString);
if(userNameString == userInstance.getUsername() && passwordString == userInstance.getPassword())
{
Log.i("Record found againse User Instance 2nd",""+userInstance);
//Log.i("UserInstance else","" + userInstance);
SessionManager.saveLoginCredentials(context,userInstance.getUsername().toString(),userInstance.getPassword().toString());
Intent intent = new Intent(LoginActivityService.this,
ProposalListActivity.class);
startActivity(intent);
getLoaderManager().destroyLoader(arg0.getId());
}
}
这是LogCat:
02-04 22:36:39.150:I / on LoadFinished of Loader(14572):姓名:kong,密码:f10343d1dc8d44c8935b356aa3f8aae2,名:Kong
02-04 22:36:39.150:我/记录再次发现User Instance 1st(14572):姓名:kong,密码:f10343d1dc8d44c8935b356aa3f8aae2,名字:KonguserName:kong:pass ::: kongkong
修改 有一些逻辑问题,我从webservice获得的密码是编码而不是文本,我正在将文本与编码值进行比较,这就是if语句块未被执行的原因。 *
答案 0 :(得分:2)
在字符串中,您必须使用String#equals()
方法进行检查,而不是==
。
使用以下代码:
if(userNameString.equals(userInstance.getUsername()) &&
passwordString.equals(userInstance.getPassword()))
答案 1 :(得分:1)
使用equals
方法代替==
来比较字符串
if(userNameString.equals(userInstance.getUsername()) && passwordString.equals(userInstance.getPassword()))