我真的被困在这里,对我来说根本没有任何意义。这是怎么回事:我有一个AsyncTask将数据发送到服务器并检索到String响应。字符串响应可以是两件事:它可以是一条消息,告知用户他/她是否输入了正确的凭据,或者可以是“ FALSE”字符串,我用它来向用户显示通知消息,以及将他/她带回主要活动以重试登录。
这是代码片段:
button.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
String cr = "Placeholder";
Credential cred = new Credential();
try { cr = cred.execute().get(); }
catch (InterruptedException e) { e.printStackTrace(); }
catch (ExecutionException e) { e.printStackTrace(); }
//tv.setText(cr);
if (cr == "FALSE") {
Log.d("Checking in false", cr);
Toast.makeText(Credentials.this, "Cannot log in: wrong username or password", Toast.LENGTH_LONG).show();
startActivity(new Intent(Credentials.this, MainActivity.class));
} else if (cr != "FALSE") {
Log.d("Checking in true", cr);
Intent intent = new Intent(Credentials.this, ProfileActivity.class);
intent.putExtra(INTENT_DATA, cr);
startActivity(intent);
}
}
});
通过使用Log.d()
静态方法并查看LogCat,我可以确定返回的值的确FALSE
是凭据是否错误(再次,它不是布尔值,只是大写的String)。有趣的是,执行的代码是我发布的代码段的“ else if”部分,但实际上记录的是返回的字符串值为FALSE
!这意味着(至少对我而言)意味着它应该已经执行了第一个“ if”条件(即if (cr == "FALSE") { }
)。但是不,它会继续执行“ else if”部分。
有任何想法吗?
将equals用于字符串比较。
IE:
错误的:
if (cr == "FALSE") {
正确的:
if (cr.equals("FALSE")) {
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句