2017-03-01 14 views
0

qrコードスキャナーをスキャンしたコードの依存関係の異なるアクティビティにリダイレクトする必要がありますが、結果から文字列を取得するのに問題があります。正しい形式。あなたは間違った方法で文字列を比較しているアンドロイドスタジオのif/else節の文字列を取得する

@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)); 
    } 
+0

を使用しています、私はあなたがよく求めているものを得る場合は、あなたの他のクラスにQRコードを渡したいです、サンプル1、サンプル2、サンプル3? – blueware

+0

id_helperが正しい値を得ていますか? – Alpha

+0

qrコードの内容に基づいて特定のクラスにリダイレクトしたい場合は、qrコード自体をアクティビティ –

答えて

5

は、Javaでは、あなたは、equalsメソッドを使用して文字列を比較する必要があります。

if ("1".equals(id_helper)) { 
    // Open activity 
} 

NullPoinerExceptionsを避けるために、常に既知の変数を条件の左側に置きます。

+1

ありがとうたくさんの仲間! –

1
はこのようになり、あなたのif文を変更し

if ("1".equals(id_helper)) { 
    startActivity(new Intent(this, Sample1.class)); 
} else if ("2".equals(id_helper)) { 
    startActivity(new Intent(this, Sample2.class)); 
} else { 
    startActivity(new Intent(this, Sample3.class)); 
} 
0

私の最高の推測では、無効な文字列のチェックを行っているということです。

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)); 
    } 

あなたのコードは、==の代わりに、myString.contentEquals("2")またはmyString.equals("2")またはmyString.equalsIgnoreCase(「2」)

+0

オハイオ州ok、あまりにも遅すぎる:D – Alpha

関連する問題