android startActivityForResult giving error
I am new to android can someone help me please, following is the code i tried to run for scanning the barcode but it gives an error whenever i click the button it says force to close what should i do help me please.
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
}
}
}
Edit Logcat:
this is the log cat now Starting:
Intent { act=com.googl开发者_开发技巧e.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)
This exception probably means that the device you are trying to use doesn't have BarcodeScanner installed. You can use this code somewhere in your app to check, and prompt the user to install it form the market if they do not already have.
//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();
}
Edit: Change this line:
intent.putExtra("SCAN_MODE", "QR_CODE_MODE");
to this:
intent.putExtra("SCAN_MODE", "PRODUCT_MODE");
I highly suggest you go to this page Read it thoroughly, and then follow the two links that are underneath the code example and look at all of the code that it shows. Even if you do not understand it, it will give you some idea what is capable via Intents with the BarcodeScanner app.
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")));
}
精彩评论