2 yue1liang1chuan yue1liang1chuan 于 2012.11.26 13:42 提问

用Zxing读条形码,但是onActivityResult调用失败

条形码是一维的时候:

public class MainActivity extends Activity {

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);        
    }

    public void test(View view){

        Intent intent = new Intent("com.google.zxing.client.android.SCAN");
        intent.putExtra("SCAN_MODE", "1D_CODE_MODE");
        startActivityForResult(intent, 0);
    }

    public void onActivityResult(int requestCode, int resultCode, Intent intent) {  
          switch (requestCode) {
          case IntentIntegrator.REQUEST_CODE:
             if (resultCode == Activity.RESULT_OK) {

                IntentResult intentResult = 
                   IntentIntegrator.parseActivityResult(requestCode, resultCode, intent);

                if (intentResult != null) {

                   String contents = intentResult.getContents();
                   String format = intentResult.getFormatName();

                   TextView uno = (TextView) findViewById(R.id.textView1);
                   uno.setText(contents);
                   Toast.makeText(this, "Numero: " + contents, Toast.LENGTH_LONG).show();
                   Log.d("SEARCH_EAN", "OK, EAN: " + contents + ", FORMAT: " + format);
                } else {
                   Log.e("SEARCH_EAN", "IntentResult je NULL!");
                }
             } else if (resultCode == Activity.RESULT_CANCELED) {
                Log.e("SEARCH_EAN", "CANCEL");
             }
         }
    }   
}

在工程中已经添加了IntentResult和IntentIntegrator。

点击按钮之后扫描器调用成功了,但是onActivityResult没调用,因为TextView没更新,Toast 也没出现。

不知道哪出错了?请各位高手帮忙

1个回答

Kill_it
Kill_it   2012.11.26 14:11
已采纳

错误一,你没用IntentIntegrator.initiateScan(),而是用手动调用startActivityForResult()

错误二,假设了IntentIntegrator.REQUEST_CODE值为0。它不是零。

所以只要在test()方法用一个调用initiateScan(),应该就能解决了。

yue1liang1chuan
yue1liang1chuan 是这样,多谢多谢
接近 5 年之前 回复
Csdn user default icon
上传中...
上传图片
插入图片