我正在尝试让二维码扫描仪根据扫描的代码重定向到不同的 Activity ,但我无法以正确的形式从结果中获取字符串。
@Override
public void handleResult(Result result) {
Log.w("handleResult", result.getText());
AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setTitle("Scan results");
builder.setMessage(result.getText());
AlertDialog alertDialog = builder.create();
alertDialog.show();
String id_helper = result.getText().toString();
if (id_helper == "1") {
startActivity(new Intent(this, Sample1.class));
} else if (id_helper == "2") {
startActivity(new Intent(this, Sample2.class));
} else {
startActivity(new Intent(this, Sample3.class));
}
最佳答案
您比较字符串的方式是错误的,在 Java 中,您必须使用 equals
方法来比较字符串。
if ("1".equals(id_helper)) {
// Open activity
}
始终将已知变量放在条件的左侧,以避免 NullPoinerExceptions
。
关于java - 在android studio中检索if/else子句的字符串,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42526495/