0

几个星期以来,我一直在尝试使用 ZXING 库来创建一个应用程序,该应用程序可以生成并显示一个包含文本的简单二维码。我已经阅读了关于这个主题的所有其他问题,但无法让它发挥作用。

我想我已经接近了,目前我已经将 core.jar 作为我的项目的参考,并且我还有 zxing 文件夹中的 android 库作为参考库。

这是我的代码:

String uniqueID="hi";
    BarcodeFormat barcodeFormat = BarcodeFormat.QR_CODE;
    int width0 = 500;
    int height0 = 500;
    QRCodeWriter writer = new QRCodeWriter();
    try
    {
        EnumMap<EncodeHintType, Object> hint = new EnumMap<EncodeHintType, Object>(EncodeHintType.class);
        hint.put(EncodeHintType.CHARACTER_SET, "UTF-8");
        BitMatrix bitMatrix = writer.encode(uniqueID, barcodeFormat, width0, height0, hint);
        int width = bitMatrix.getWidth();
        int height = bitMatrix.getHeight();
        int[] pixels = new int[width * height];
        for (int y = 0; y < height; y++)
        {
            int offset = y * width;
            for (int x = 0; x < width; x++)
            {

                pixels[offset + x] = bitMatrix.get(x, y) ? android.graphics.Color.BLACK : android.graphics.Color.WHITE;
            }
        }

        Bitmap bitmap = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888);
        bitmap.setPixels(pixels, 0, width, 0, 0, width, height);
        ImageView imageview = (ImageView)findViewById(R.id.qrCode);
        imageview.setImageBitmap(bitmap);
    } catch (WriterException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }

此代码没有任何错误。当我运行应用程序并单击运行此代码的按钮时,我得到以下 logcat 输出:

06-19 15:33:34.493: W/dalvikvm(1340): VFY: unable to resolve static field 1076 (QR_CODE) in Lcom/google/zxing/BarcodeFormat;
06-19 15:33:34.493: D/dalvikvm(1340): VFY: replacing opcode 0x62 at 0x0002
06-19 15:33:34.493: D/dalvikvm(1340): DexOpt: unable to opt direct call 0x114f at 0x0a in Lcom/example/qrgen/MainActivity;.genQR
06-19 15:33:34.493: I/dalvikvm(1340): DexOpt: unable to optimize static field ref 0x043c at 0x14 in Lcom/example/qrgen/MainActivity;.genQR
06-19 15:33:34.963: D/libEGL(1340): loaded /system/lib/egl/libEGL_mali.so
06-19 15:33:34.993: D/libEGL(1340): loaded /system/lib/egl/libGLESv1_CM_mali.so
06-19 15:33:34.998: D/libEGL(1340): loaded /system/lib/egl/libGLESv2_mali.so
06-19 15:33:35.003: D/(1340): Device driver API match
06-19 15:33:35.003: D/(1340): Device driver API version: 10
06-19 15:33:35.003: D/(1340): User space API version: 10 
06-19 15:33:35.003: D/(1340): mali: REVISION=Linux-r2p4-02rel0 BUILD_DATE=Tue Oct 16 15:37:13 KST 2012 
06-19 15:33:35.073: D/OpenGLRenderer(1340): Enabling debug mode 0
06-19 15:33:38.883: D/AndroidRuntime(1340): Shutting down VM
06-19 15:33:38.883: W/dalvikvm(1340): threadid=1: thread exiting with uncaught exception (group=0x420312a0)
06-19 15:33:38.913: E/AndroidRuntime(1340): FATAL EXCEPTION: main
06-19 15:33:38.913: E/AndroidRuntime(1340): java.lang.IllegalStateException: Could not execute method of the activity
06-19 15:33:38.913: E/AndroidRuntime(1340):     at android.view.View$1.onClick(View.java:3691)
06-19 15:33:38.913: E/AndroidRuntime(1340):     at android.view.View.performClick(View.java:4211)
06-19 15:33:38.913: E/AndroidRuntime(1340):     at android.view.View$PerformClick.run(View.java:17267)
06-19 15:33:38.913: E/AndroidRuntime(1340):     at android.os.Handler.handleCallback(Handler.java:615)
06-19 15:33:38.913: E/AndroidRuntime(1340):     at android.os.Handler.dispatchMessage(Handler.java:92)
06-19 15:33:38.913: E/AndroidRuntime(1340):     at android.os.Looper.loop(Looper.java:137)
06-19 15:33:38.913: E/AndroidRuntime(1340):     at android.app.ActivityThread.main(ActivityThread.java:4898)
06-19 15:33:38.913: E/AndroidRuntime(1340):     at java.lang.reflect.Method.invokeNative(Native Method)
06-19 15:33:38.913: E/AndroidRuntime(1340):     at java.lang.reflect.Method.invoke(Method.java:511)
06-19 15:33:38.913: E/AndroidRuntime(1340):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1006)
06-19 15:33:38.913: E/AndroidRuntime(1340):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:773)
06-19 15:33:38.913: E/AndroidRuntime(1340):     at dalvik.system.NativeStart.main(Native Method)
06-19 15:33:38.913: E/AndroidRuntime(1340): Caused by: java.lang.reflect.InvocationTargetException
06-19 15:33:38.913: E/AndroidRuntime(1340):     at java.lang.reflect.Method.invokeNative(Native Method)
06-19 15:33:38.913: E/AndroidRuntime(1340):     at java.lang.reflect.Method.invoke(Method.java:511)
06-19 15:33:38.913: E/AndroidRuntime(1340):     at android.view.View$1.onClick(View.java:3686)
06-19 15:33:38.913: E/AndroidRuntime(1340):     ... 11 more
06-19 15:33:38.913: E/AndroidRuntime(1340): Caused by: java.lang.NoClassDefFoundError: com.google.zxing.BarcodeFormat
06-19 15:33:38.913: E/AndroidRuntime(1340):     at com.example.qrgen.MainActivity.genQR(MainActivity.java:36)
06-19 15:33:38.913: E/AndroidRuntime(1340):     ... 14 more

看来问题出在 com.google.zxing.BarcodeFormat 上,但我检查了我引用的库,并且该类确实存在。关于什么可能是错误的任何想法?

4

1 回答 1

2

您最近更新了 ADT 版本吗?因为最新版本(ADT 22)会导致依赖项(例如您的 ZXing)与NoClassDefFoundError... 崩溃,在这种情况下,这与您的代码绝对没有关系。

最常见的解决方案已经在 stackoverflow 上,检查它是否有效!

于 2013-06-19T14:42:16.077 回答