我正在使用以下几行来检测ZXing 条码扫描仪,但它正在引发异常调用ClassNotFoundException
。
try{
Intent intent = new Intent("com.google.zxing.client.android.SCAN");
startActivityForResult(intent, 0);
}
catch(Exception e)
{
//calling Alert message which says to download the application
}
ClassNotFoundException
说 com.google.zxing.client.android.CaptureActivity
没有找到。但我不明白为什么这个异常没有被处理,即使我把它放在 try,catch Blocks 中。
完整代码:
我也尝试使用 isIntentAvailable(Context context, Intent intent) 方法,但我仍然遇到同样的错误。
try{
Intent intent = new Intent("com.google.zxing.client.android.SCAN");
if(isIntentAvailable(this, intent)){
runOnUiThread(new Runnable() {
public void run() {
pd.dismiss();
}
});
startActivityForResult(intent, 0);
}
else{
AlertDialog diaBox = installDialogBox();
diaBox.show();
}
}
catch (Exception e) {
pd.dismiss();
Log.e("in Exception","Exception");
e.printStackTrace();
}
方法:检查应用程序是否安装。
public static boolean isIntentAvailable(Context context, Intent intent) {
final PackageManager packageManager = context.getPackageManager();
List<ResolveInfo> list =packageManager.queryIntentActivities(intent,PackageManager.MATCH_DEFAULT_ONLY);
return list.size() > 0;
}
日志错误:
05-24 10:24:36.507: E/AndroidRuntime(9159): FATAL EXCEPTION: main
05-24 10:24:36.507: E/AndroidRuntime(9159): java.lang.RuntimeException: Unable to instantiate activity ComponentInfo{com.zzzzz.xxxxx.activies/com.google.zxing.client.android.CaptureActivity}: java.lang.ClassNotFoundException: com.google.zxing.client.android.CaptureActivity in loader dalvik.system.PathClassLoader[/data/app/com.sparx.maternity.activies-2.apk]
05-24 10:24:36.507: E/AndroidRuntime(9159): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1618)
05-24 10:24:36.507: E/AndroidRuntime(9159): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1716)
05-24 10:24:36.507: E/AndroidRuntime(9159): at android.app.ActivityThread.access$1500(ActivityThread.java:124)
05-24 10:24:36.507: E/AndroidRuntime(9159): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:968)
05-24 10:24:36.507: E/AndroidRuntime(9159): at android.os.Handler.dispatchMessage(Handler.java:99)
05-24 10:24:36.507: E/AndroidRuntime(9159): at android.os.Looper.loop(Looper.java:123)
05-24 10:24:36.507: E/AndroidRuntime(9159): at android.app.ActivityThread.main(ActivityThread.java:3806)
05-24 10:24:36.507: E/AndroidRuntime(9159): at java.lang.reflect.Method.invokeNative(Native Method)
05-24 10:24:36.507: E/AndroidRuntime(9159): at java.lang.reflect.Method.invoke(Method.java:507)
05-24 10:24:36.507: E/AndroidRuntime(9159): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)
05-24 10:24:36.507: E/AndroidRuntime(9159): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
05-24 10:24:36.507: E/AndroidRuntime(9159): at dalvik.system.NativeStart.main(Native Method)
05-24 10:24:36.507: E/AndroidRuntime(9159): Caused by: java.lang.ClassNotFoundException: com.google.zxing.client.android.CaptureActivity in loader dalvik.system.PathClassLoader[/data/app/com.sparx.maternity.activies-2.apk]
05-24 10:24:36.507: E/AndroidRuntime(9159): at dalvik.system.PathClassLoader.findClass(PathClassLoader.java:240)
05-24 10:24:36.507: E/AndroidRuntime(9159): at java.lang.ClassLoader.loadClass(ClassLoader.java:551)
05-24 10:24:36.507: E/AndroidRuntime(9159): at java.lang.ClassLoader.loadClass(ClassLoader.java:511)
05-24 10:24:36.507: E/AndroidRuntime(9159): at android.app.Instrumentation.newActivity(Instrumentation.java:1021)
05-24 10:24:36.507: E/AndroidRuntime(9159): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1610)
05-24 10:24:36.507: E/AndroidRuntime(9159): ... 11 more
请回复。