zxing没有正确处理null

时间:2015-04-10 04:49:14

标签: android zxing

如果结果为null,则zxing应显示的错误消息不会出现。我需要改变什么?我的代码在这里:

public void onActivityResult(int requestCode, int resultCode, Intent intent) {
    IntentResult scanningResult = IntentIntegrator.parseActivityResult(requestCode, resultCode, intent);


    if (scanningResult != null) {
                //we have a result
                scanContent = scanningResult.getContents();
                scanFormat = scanningResult.getFormatName();


                formatTxt.setText("FORMAT: " + scanFormat);
                contentTxt.setText("CONTENT: " + scanContent);
                Toast.makeText(this, "Please click 'Update DataBase' after this message ends", Toast.LENGTH_LONG).show();

            }
            else{
                Toast.makeText(getApplicationContext(),"No scan data received. Restart the scanner", Toast.LENGTH_SHORT).show();
            }

        }   

无论结果如何,'Please Click'吐司都会出现。

根据答案,我已将我的代码调整为以下内容:

// retrieve scan result
    public void onActivityResult(int requestCode, int resultCode, Intent intent) {
        //String contents = null;
        //IntentResult scanningResult = IntentIntegrator.parseActivityResult(requestCode, resultCode, intent);
        super.onActivityResult(requestCode, resultCode, intent);
        if (requestCode == 0) {
            //we have a result
            if (resultCode ==RESULT_OK){
                scanContent = intent.getStringExtra("SCAN_RESULT");
                scanFormat = intent.getStringExtra("SCAN_RESULT_FORMAT");
                formatTxt.setText("FORMAT: " + scanFormat);
                contentTxt.setText("CONTENT: " + scanContent);
                Toast.makeText(this, "Please click 'Update DataBase' after this message ends", Toast.LENGTH_LONG).show();
            } else if (resultCode == RESULT_CANCELED) {
            Toast.makeText(getApplicationContext(),"No scan data received. Restart the scanner", Toast.LENGTH_SHORT).show();
            }
        }       
    }

虽然似乎没有任何语法错误,但扫描不显示任何输出。

1 个答案:

答案 0 :(得分:0)

你应该像这样使用onActivityResult

public void onActivityResult(int requestCode, int resultCode, Intent data) {
String contents = null;
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == 0) {
      if (resultCode == RESULT_OK) {
         contents = data.getStringExtra("SCAN_RESULT");
         String format = data.getStringExtra("SCAN_RESULT_FORMAT");
         //moved here
         TextView tv = (TextView) findViewById(R.id.scanResult);
         tv.setText(contents);
         // Handle successful scan
      } else if (resultCode == RESULT_CANCELED) {
         // Handle cancel
      }
}