5

我正在编写一个使用 LruCache 的项目,该项目包含在 android-support-v4.jar 兼容库中。在带有 JB 的设备上运行时,代码运行良好,但是当我在带有 GB 的 Droid X 上运行它时,应用程序因以下错误而死:

I/dalvikvm(2459): Could not find method android.util.LruCache.put, referenced from method blah.blah.Utility.getBitmap
W/dalvikvm(2459): VFY: unable to resolve virtual method 2641: Landroid/util/LruCache;.put (Ljava/lang/Object;Ljava/lang/Object;)Ljava/lang/Object;
D/dalvikvm(2459): VFY: replacing opcode 0x6e at 0x0026
... [repeats similar lines] ...
W/dalvikvm(2459): Unable to resolve superclass of Lblah/blah/Utility$1; (353)
W/dalvikvm(2459): Link of class 'Lblah/blah/Utility$1;' failed
E/dalvikvm(2459): Could not find class 'blah.blah.Utility$1', referenced from method blah.blah.Utility.initCaches
W/dalvikvm(2459): VFY: unable to resolve new-instance 559 (Lblah/blah/Utility$1;) in Lblah/blah/Utility;
D/dalvikvm(2459): VFY: replacing opcode 0x22 at 0x000d
D/dalvikvm(2459): VFY: dead code 0x000f-002c in Lblah/blah/Utility;.initCaches (ILandroid/content/Context;)V
I/dalvikvm(2459): Could not find method android.util.LruCache.get, referenced from method blah.blah.Utility.mCacheGet
W/dalvikvm(2459): VFY: unable to resolve virtual method 2640: Landroid/util/LruCache;.get (Ljava/lang/Object;)Ljava/lang/Object;
D/dalvikvm(2459): VFY: replacing opcode 0x6e at 0x0008
D/dalvikvm(2459): VFY: dead code 0x000b-000e in Lblah/blah/Utility;.mCacheGet (Ljava/lang/String;)Landroid/graphics/Bitmap;
... [repeats similar lines] ...
D/AndroidRuntime(2459): Shutting down VM
W/dalvikvm(2459): threadid=1: thread exiting with uncaught exception (group=0x40018560)
E/AndroidRuntime(2459): FATAL EXCEPTION: main
E/AndroidRuntime(2459): java.lang.NoClassDefFoundError: blah.blah.Utility$1
E/AndroidRuntime(2459):     at blah.blah.Utility.initCaches(Utility.java:49)
E/AndroidRuntime(2459):     at blah.blah.MainActivity.onCreate(MainActivity.java:40)
... [etc] ...
W/ActivityManager(1318):   Force finishing activity blah.blah/.MainActivity

Utility.java:49 看起来像这样:

    mCache = new LruCache<String, Bitmap>( mCacheSize )   // <-- line 49 --
    {
        @Override
        protected int sizeOf( String key, Bitmap bitmap )
        {
            return bitmap.getByteCount();
        }
    };

所以我在任何地方引用 LruCache 或其方法都会导致错误。首先想到的是 android-support-v4.jar 不知何故被排除在 APK 文件之外,但事实并非如此。我将jar文件放在libs/下,运行ant debug后检查classes.dex.d,我看到它添加了以下行:

/home/paul/workspace/blah/libs/android-support-v4.jar \

我的第二个想法是,也许 LruCache 不在 android-support-v4 中,但是查看源代码我看到下面的源文件在那里,并且它具有我正在尝试使用的方法:

/home/paul/bin/android-sdk/extras/android/support/v4/src/java/android/support/v4/util/LruCache.java

那为什么找不到呢?

4

1 回答 1

13

支持库中的类使用不同的命名空间。

您需要导入:

import android.support.v4.util.LruCache;

或者,您可以将 LruCache.java 直接复制并编译到您的应用程序中。

于 2012-08-06T23:52:21.250 回答