私は、コードがバーコードをスキャンして内容をチェックするアプリをやっています。どうすればこの問題を解決できますか?ここでelse文がスキップされて何もしない場合にネストされます
コードは次のとおりです。利用可能な限られた情報で
public class barcode extends AppCompatActivity {
Button scan_btn;
EditText Edit_current;
TextView formatTxt, contentTxt,price;
int x;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(activity_promotion_page);
formatTxt = (TextView) findViewById(R.id.scanFormat);
contentTxt = (TextView) findViewById(scanContent);
price = (TextView)findViewById(R.id.Price);
scan_btn = (Button) findViewById(R.id.scan_button);
final Activity activity = this;
scan_btn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
IntentIntegrator integrator = new IntentIntegrator(activity);
integrator.setDesiredBarcodeFormats(IntentIntegrator.PRODUCT_CODE_TYPES);
integrator.setPrompt("Scan");
integrator.setCameraId(0);
integrator.setBeepEnabled(false);
integrator.setBarcodeImageEnabled(false);
integrator.initiateScan();
}
});
}
@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
IntentResult result = IntentIntegrator.parseActivityResult(requestCode, resultCode, data);
if(result.getContents() == null) {
Log.d("MainActivity", "Cancelled scan");
Toast.makeText(this, "Cancelled", Toast.LENGTH_LONG).show();
} else if (result.getContents().equals(12345014)){
price.setText("$999");
}else if (result.getContents().equals(12345021)){
price.setText("$91");
}
}}
あなたは** switch **ステートメントについて読んでみたいです。それらを文字列で使用する方法などです。 – GhostCat
指定した3つの条件のいずれも満たされないため、スキップされます。したがって、result.getContents()はnullではなく、12345014または12345021に等しくない –
getContents()メソッド –