简体   繁体   中英

Scan Barcode in android

When I run the app , It is force closed.How can QR code is scanning?I need to implement the bar-code scanner in my android application.Here I got the following error on my console.How can clear this error?...Here the zxing(code.jar) jar file is added also. I have used below code:

 public class Main extends Activity {
  @Override
  public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);
    HandleClick hc = new HandleClick();
    findViewById(R.id.butQR).setOnClickListener(hc);
    findViewById(R.id.butProd).setOnClickListener(hc);
    findViewById(R.id.butOther).setOnClickListener(hc);
  }
  private class HandleClick implements OnClickListener{
    public void onClick(View arg0) {
      Intent intent = new Intent("com.google.zxing.client.android.SCAN");
      switch(arg0.getId()){
        case R.id.butQR:
          intent.putExtra("SCAN_MODE", "QR_CODE_MODE");
        break;
        case R.id.butProd:
          intent.putExtra("SCAN_MODE", "PRODUCT_MODE");
        break;
        case R.id.butOther:
          intent.putExtra("SCAN_FORMATS", "CODE_39,CODE_93,CODE_128,DATA_MATRIX,ITF,CODABAR");
        break;
      }
      startActivityForResult(intent, 0);    //Barcode Scanner to scan for us
    }
  }
  public void onActivityResult(int requestCode, int resultCode, Intent intent) {
    if (requestCode == 0) {
      TextView tvStatus=(TextView)findViewById(R.id.tvStatus);
      TextView tvResult=(TextView)findViewById(R.id.tvResult);
      if (resultCode == RESULT_OK) {
        tvStatus.setText(intent.getStringExtra("SCAN_RESULT_FORMAT"));
        tvResult.setText(intent.getStringExtra("SCAN_RESULT"));
      } else if (resultCode == RESULT_CANCELED) {
        tvStatus.setText("Press a button to start a scan.");
        tvResult.setText("Scan cancelled.");
      }
    }
  }
}

my console shows following error:

      09-06 00:56:36.198: D/AndroidRuntime(5417): Shutting down VM
      09-06 00:56:36.198: W/dalvikvm(5417): threadid=1: thread exiting with uncaught exception (group=0x4001d800)
      09-06 00:56:36.208: E/AndroidRuntime(5417): FATAL EXCEPTION: main
      09-06 00:56:36.208: E/AndroidRuntime(5417): android.content.ActivityNotFoundException: No Activity found to handle Intent { act=com.google.zxing.client.android.SCAN (has extras) }
      09-06 00:56:36.208: E/AndroidRuntime(5417):   at android.app.Instrumentation.checkStartActivityResult(Instrumentation.java:1408)
      09-06 00:56:36.208: E/AndroidRuntime(5417):   at android.app.Instrumentation.execStartActivity(Instrumentation.java:1378)
      09-06 00:56:36.208: E/AndroidRuntime(5417):   at android.app.Activity.startActivityForResult(Activity.java:2817)
      09-06 00:56:36.208: E/AndroidRuntime(5417):   at biz.tekeye.scanbarcode.Main$HandleClick.onClick(Main.java:33)
      09-06 00:56:36.208: E/AndroidRuntime(5417):   at android.view.View.performClick(View.java:2408)
      09-06 00:56:36.208: E/AndroidRuntime(5417):   at android.view.View$PerformClick.run(View.java:8816)
      09-06 00:56:36.208: E/AndroidRuntime(5417):   at android.os.Handler.handleCallback(Handler.java:587)
      09-06 00:56:36.208: E/AndroidRuntime(5417):   at android.os.Handler.dispatchMessage(Handler.java:92)
      09-06 00:56:36.208: E/AndroidRuntime(5417):   at android.os.Looper.loop(Looper.java:123)
      09-06 00:56:36.208: E/AndroidRuntime(5417):   at android.app.ActivityThread.main(ActivityThread.java:4627)
      09-06 00:56:36.208: E/AndroidRuntime(5417):   at java.lang.reflect.Method.invokeNative(Native Method)
      09-06 00:56:36.208: E/AndroidRuntime(5417):   at java.lang.reflect.Method.invoke(Method.java:521)
      09-06 00:56:36.208: E/AndroidRuntime(5417):   at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868)
      09-06 00:56:36.208: E/AndroidRuntime(5417):   at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)
      09-06 00:56:36.208: E/AndroidRuntime(5417):   at dalvik.system.NativeStart.main(Native Method)
      09-06 00:56:38.359: I/Process(5417): Sending signal. PID: 5417 SIG: 9

Just install an app that can handle that intent (Zxing).

Also, catch that exception and prompt the user to install said application.

At first glance, it looks like you haven't installed the ZXing Barcode Scanner app on your phone.

You can direct the user to the app in the market using a market link, like this: Install Application programmatically on Android

Best solution by far is to use the IntentIntegrator provided by the project. It not only handles installing the app but other small gotchas.

http://code.google.com/p/zxing/source/browse/trunk#trunk%2Fandroid-integration

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM