2012-02-03 113 views
0

我正在Android中使用PhoneGap实现条码扫描器,但是当我执行该程序时,它会显示一些运行时错误(如下所示)。解决运行时错误

有谁知道如何解决这个问题?

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

这是ClassCastException异常,你想一个对象类型更改为另一种。没有代码,很难说出什么问题。发布您的代码。 – kosa 2012-02-03 13:08:10

+0

BarecodeScanner类的源代码是什么? – Sephy 2012-02-03 13:09:46

+0

更新了我的答案,检查是否相关) – 2012-02-03 13:57:25

回答

1

UPD: 唯一的线索是,也许你传递错误的类的活动吗?在你的清单中,你应该在活动部分有一些扩展DroidGap的东西。也许,你正在传递你的BarcodeScanner呢?

最有可能的是,您将findViewById()检索到的某些内容转换为错误的类型。在布局文件中仔细检查您的ID,以及您将其引用的内容。

您也可以在调试器中运行它,并使其在ClassCastException上中断 - 它会告诉您源中的哪一行出现错误。

编辑但是,是的,@thinksteep和@Sephy是威武权:发布您的代码,如果没有代码的问题是可笑的抽象:)

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); 
     } 
    } 
} 
相关问题