android startActivityForResult给出错误

时间:2011-08-18 01:23:01

标签: android

我是Android新手可以有人帮助我,以下是我试图运行扫描条码的代码,但它给出一个错误,每当我点击按钮它说强制关闭我应该做什么帮助我请。

this.btnCheck = (Button) findViewById(R.id.btnsearch);
this.btnCheck.setOnClickListener(
new OnClickListener() {
public void onClick(View v)
{
Intent intent = new Intent("com.google.zxing.client.android.SCAN");
intent.putExtra("SCAN_MODE", "QR_CODE_MODE");
startActivityForResult(intent, 0);
}
}
);   
}
public void onActivityResult(int requestCode, int resultCode, Intent data) {
if (requestCode == 0) {
if (resultCode == RESULT_OK) {
String contents = data.getStringExtra("SCAN_RESULT");
Toast.makeText(this, "the scaned code is = "+ contents, Toast.LENGTH_SHORT).show(); 
// Handle successful scan
} else if (resultCode == RESULT_CANCELED) {
// Handle cancel
}
}
}

编辑Logcat:

这是日志猫现在开始:

Intent { act=com.google.zxing.client.android.SCAN (has extras) } from pid 359 thread exiting with uncaught exception (group=0x40015560) 

08-18 01:52:00.995: ERROR/AndroidRuntime(359): FATAL EXCEPTION: main 

08-18 01:52:00.995: ERROR/AndroidRuntime(359): android.content.ActivityNotFoundException: No Activity found to handle Intent { act=com.google.zxing.client.android.SCAN (has extras) } 

08-18 01:52:00.995: ERROR/AndroidRuntime(359): at android.app.Instrumentation.checkStartActivityResult(Instrumentation.java:1408)

2 个答案:

答案 0 :(得分:2)

此异常可能意味着您尝试使用的设备未安装BarcodeScanner。您可以在应用中的某个位置使用此代码进行检查,并提示用户将其安装在市场中(如果尚未安装)。

      //Check for Barcode scanner, if not found put up an alert that allows user to install it.
    PackageManager pm = getPackageManager();
    try {
        ApplicationInfo appInfo = pm.getApplicationInfo("com.google.zxing.client.android", 0);
    } catch (NameNotFoundException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
         new AlertDialog.Builder(this)
            .setTitle("WARNING:")
            .setMessage("You don't have Barcode Scanner installed. Please install it.")
             .setCancelable(false)
            .setNeutralButton("Install it now", new DialogInterface.OnClickListener() {
            public void onClick(DialogInterface dialog, int whichButton) {         
                          Uri uri = Uri.parse("market://search?q=pname:com.google.zxing.client.android");
                          startActivity(new Intent(Intent.ACTION_VIEW, uri));
                    }
            })
            .show();
    }

编辑:更改此行:

intent.putExtra("SCAN_MODE", "QR_CODE_MODE");

到此:

intent.putExtra("SCAN_MODE", "PRODUCT_MODE");

我强烈建议你go to this page仔细阅读,然后按照代码示例下面的两个链接查看它显示的所有代码。即使您不理解它,它也会让您了解通过Intents与BarcodeScanner应用程序的功能。

答案 1 :(得分:0)

Intent intent = new Intent("com.google.zxing.client.android.SCAN");
        try {
            startActivityForResult(intent,0);
        } catch (ActivityNotFoundException e) {
            startActivity(new Intent(Intent.ACTION_VIEW, Uri.parse("market://details?id=" + "com.google.zxing.client.android")));
        }