Reputation: 551
I am implementing a bar code scanner in Android using PhoneGap, but when I execute the program it displays a number of runtime errors (shown below).
Does anyone know how to solve this problem?
02-03 18:26:35.351: ERROR/AndroidRuntime(876): FATAL EXCEPTION: main
02-03 18:26:35.351: ERROR/AndroidRuntime(876): java.lang.RuntimeException: Unable to instantiate activity ComponentInfo{com.phonegap.plugins.barcodescanner/com.phonegap.plugins.barcodescanner.BarcodeScanner}: java.lang.ClassCastException: com.phonegap.plugins.barcodescanner.BarcodeScanner
02-03 18:26:35.351: ERROR/AndroidRuntime(876): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2585)
02-03 18:26:35.351: ERROR/AndroidRuntime(876): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2679)
02-03 18:26:35.351: ERROR/AndroidRuntime(876): at android.app.ActivityThread.access$2300(ActivityThread.java:125)
02-03 18:26:35.351: ERROR/AndroidRuntime(876): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2033)
02-03 18:26:35.351: ERROR/AndroidRuntime(876): at android.os.Handler.dispatchMessage(Handler.java:99)
02-03 18:26:35.351: ERROR/AndroidRuntime(876): at android.os.Looper.loop(Looper.java:123)
02-03 18:26:35.351: ERROR/AndroidRuntime(876): at android.app.ActivityThread.main(ActivityThread.java:4627)
hi this is my source code verify and give the solution for it according to errors.
package com.phonegap.plugins.barcodescanner;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import android.app.Activity;
import android.content.Intent;
import android.util.Log;
import com.phonegap.api.Plugin;
import com.phonegap.api.PluginResult;
/**
* This calls out to the ZXing barcode reader and returns the result.
*/
public class BarcodeScanner extends Plugin {
private static final String TEXT_TYPE = "TEXT_TYPE";
private static final String EMAIL_TYPE = "EMAIL_TYPE";
private static final String PHONE_TYPE = "PHONE_TYPE";
private static final String SMS_TYPE = "SMS_TYPE";
public static final int REQUEST_CODE = 0x0ba7c0de;
public String callback;
/**
* Constructor.
*/
public BarcodeScanner() {
}
/**
* Executes the request and returns PluginResult.
*
* @param action The action to execute.
* @param args JSONArray of arguments for the plugin.
* @param callbackId The callback id used when calling back into JavaScript.
* @return A PluginResult object with a status and message.
*/
public PluginResult execute(String action, JSONArray args, String callbackId) {
this.callback = callbackId;
if (action.equals("encode")) {
JSONObject obj = args.optJSONObject(0);
if (obj != null) {
String type = obj.optString("type");
String data = obj.optString("data");
// If the type is null then force the type to text
if (type == null) {
type = TEXT_TYPE;
}
if (data == null) {
return new PluginResult(PluginResult.Status.ERROR, "User did not specify data to encode");
}
encode(type, data);
} else {
return new PluginResult(PluginResult.Status.ERROR, "User did not specify data to encode");
}
}
else if (action.equals("scan")) {
scan();
} else {
return new PluginResult(PluginResult.Status.INVALID_ACTION);
}
PluginResult r = new PluginResult(PluginResult.Status.NO_RESULT);
r.setKeepCallback(true);
return r;
}
/**
* Starts an intent to scan and decode a barcode.
*/
public void scan() {
Intent intentScan = new Intent("com.phonegap.plugins.barcodescanner.SCAN");
intentScan.addCategory(Intent.CATEGORY_DEFAULT);
this.ctx.startActivityForResult((Plugin) this, intentScan, REQUEST_CODE);
}
/**
* Called when the barcode scanner intent completes
*
* @param requestCode The request code originally supplied to startActivityForResult(),
* allowing you to identify who this result came from.
* @param resultCode The integer result code returned by the child activity through its setResult().
* @param intent An Intent, which can return result data to the caller (various data can be attached to Intent "extras").
*/
public void onActivityResult(int requestCode, int resultCode, Intent intent) {
if (requestCode == REQUEST_CODE) {
if (resultCode == Activity.RESULT_OK) {
JSONObject obj = new JSONObject();
try {
obj.put("text", intent.getStringExtra("SCAN_RESULT"));
obj.put("format", intent.getStringExtra("SCAN_RESULT_FORMAT"));
obj.put("cancelled", false);
} catch(JSONException e) {
//Log.d(LOG_TAG, "This should never happen");
}
this.success(new PluginResult(PluginResult.Status.OK, obj), this.callback);
} if (resultCode == Activity.RESULT_CANCELED) {
JSONObject obj = new JSONObject();
try {
obj.put("text", "");
obj.put("format", "");
obj.put("cancelled", true);
} catch(JSONException e) {
//Log.d(LOG_TAG, "This should never happen");
}
this.success(new PluginResult(PluginResult.Status.OK, obj), this.callback);
} else {
this.error(new PluginResult(PluginResult.Status.ERROR), this.callback);
}
}
}
/**
* Initiates a barcode encode.
* @param data The data to encode in the bar code
* @param data2
*/
public void encode(String type, String data) {
Intent intentEncode = new Intent("com.phonegap.plugins.barcodescanner.ENCODE");
intentEncode.putExtra("ENCODE_TYPE", type);
intentEncode.putExtra("ENCODE_DATA", data);
this.ctx.startActivity(intentEncode);
}
}
Upvotes: 0
Views: 1370
Reputation: 149
Have got the some error... Here the solution:
/**
* Starts an intent to scan and decode a barcode.
*/
public void scan() {
/*
Intent intentScan = new Intent("com.phonegap.plugins.barcodescanner.SCAN");
intentScan.addCategory(Intent.CATEGORY_DEFAULT);
this.ctx.startActivityForResult((Plugin) this, intentScan, REQUEST_CODE);
*/
// TODO Auto-generated method stub
Intent intent = new Intent("com.google.zxing.client.android.SCAN");
intent.putExtra("SCAN_MODE", "QR_CODE_MODE");
this.ctx.startActivityForResult((Plugin) this,intent, 0);
}
/**
* Called when the barcode scanner intent completes
*
* @param requestCode The request code originally supplied to startActivityForResult(),
* allowing you to identify who this result came from.
* @param resultCode The integer result code returned by the child activity through its setResult().
* @param intent An Intent, which can return result data to the caller (various data can be attached to Intent "extras").
*/
public void onActivityResult(int requestCode, int resultCode, Intent intent) {
Log.i("App","----> Scanning...." );
if (requestCode == 0) {
if (resultCode == Activity.RESULT_OK) {
JSONObject obj = new JSONObject();
try {
obj.put("text", intent.getStringExtra("SCAN_RESULT"));
obj.put("format", intent.getStringExtra("SCAN_RESULT_FORMAT"));
obj.put("cancelled", false);
} catch(JSONException e) {
//Log.d(LOG_TAG, "This should never happen");
}
this.success(new PluginResult(PluginResult.Status.OK, obj), this.callback);
} if (resultCode == Activity.RESULT_CANCELED) {
JSONObject obj = new JSONObject();
try {
obj.put("text", "");
obj.put("format", "");
obj.put("cancelled", true);
} catch(JSONException e) {
//Log.d(LOG_TAG, "This should never happen");
}
this.success(new PluginResult(PluginResult.Status.OK, obj), this.callback);
} else {
this.error(new PluginResult(PluginResult.Status.ERROR), this.callback);
}
}
}
Upvotes: 1
Reputation: 23273
I think you may have something screwed up in your AndroidManifest.xml file. The class com.phonegap.plugins.barcodescanner.BarcodeScanner should be setup in your plugins.xml file. Did you read my tutorial on setting up the BarcodeScanner?
http://simonmacdonald.blogspot.com/2011/12/installing-barcode-plugin-for-phonegap.html
Upvotes: 2
Reputation: 21066
Upd: The only clue is maybe you're passing wrong class as activity? In your manifest you should have something that extends DroidGap in the activity section. Maybe, you're passing your BarcodeScanner there instead?
Most likely, you're casting something retrieved by findViewById() to a wrong type. Double-check your id's in layout file and what you cast the references to.
You can also run it in debugger and make it break on ClassCastException - it'll show you which line in the source has the error.
Edit But yeah, @thinksteep and @Sephy are mighty right: post your code, without the code your question is ridiculously abstract :)
Upvotes: 1