yue1liang1chuan
2012-11-26 05:42
采纳率: 69.8%
浏览 4.8k

用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 2012-11-26 06:11
    已采纳

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

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

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

    打赏 评论

相关推荐 更多相似问题