我正在使用PhoneGap在Android中实现条形码扫描程序,但是当我执行程序时,它会显示许多运行时错误(如下所示)。
有谁知道如何解决这个问题?
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)
这是我的源代码验证,并根据错误为其提供解决方案。
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);
}
}
答案 0 :(得分:2)
我想你的AndroidManifest.xml文件可能会搞砸了。应该在plugins.xml文件中设置com.phonegap.plugins.barcodescanner.BarcodeScanner类。您是否阅读了有关设置BarcodeScanner的教程?
http://simonmacdonald.blogspot.com/2011/12/installing-barcode-plugin-for-phonegap.html
答案 1 :(得分:1)
<强> UPD:强> 唯一的线索可能是你将错误的课程作为活动传递?在你的清单中,你应该有一些在活动部分扩展DroidGap的东西。也许,你在那里传递你的BarcodeScanner?
最有可能的是,你将findViewById()检索的内容转换为错误的类型。仔细检查布局文件中的id以及您对引用的内容。
您也可以在调试器中运行它并使其在ClassCastException上中断 - 它将显示源中的哪一行有错误。
编辑但是,@ thinksteep和@Sephy是强大的:发布你的代码,没有代码你的问题是荒谬的抽象:)
答案 2 :(得分:0)
有一些错误......解决方案:
/**
* 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);
}
}
}