我正在使用以下行来检测 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;
}
Logcat错误:
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
请回复。
答案 0 :(得分:0)
在调用之前检查意图是否可用
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;
}
也可以尝试使用
try {
....
} catch ( ClassNotFoundException e ) {
// TODO handle me
}
答案 1 :(得分:0)
需要询问是否有这个应用程序。并且不是要捕捉异常
Intent intent = new Intent("com.google.zxing.client.android.SCAN");
List<ResolveInfo> list = getPackageManager().queryIntentActivities( intent, 0 );
list.size()&gt; 0,表示存在目标应用程序。
答案 2 :(得分:0)
您正在Intent
进行整合。但我猜你还试图在我们的项目中包含我们的代码和清单的一部分。不要这样做。这没有必要,并导致这样的错误,因为您也告诉Android代码在您的应用程序中。它不是,也不应该是。
答案 3 :(得分:0)