Android 番石榴:java.lang.NoClassDefFoundError - com.google.common.collect.HashBiMap

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/10804878/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-20 05:02:22  来源:igfitidea点击:

Guava: java.lang.NoClassDefFoundError - com.google.common.collect.HashBiMap

androidhashmapguavabimap

提问by Eric

I currently face the problem of java.lang.NoClassDefFoundError: com.google.common.collect.HashBiMap when using guava libraries downloaded from here: http://code.google.com/p/guava-libraries/

当使用从这里下载的番石榴库时,我目前面临 java.lang.NoClassDefFoundError: com.google.common.collect.HashBiMap 的问题:http: //code.google.com/p/guava-libraries/

I already add guava-12.0.jar into my project as a reference library but I still get the error. May you give some advice on what the problem would be? Thankyou for your help

我已经将 guava-12.0.jar 作为参考库添加到我的项目中,但我仍然收到错误消息。你能就问题出在哪里给出一些建议吗?感谢您的帮助

package my.project;

import android.app.Activity;
import android.os.Bundle;

import com.google.common.collect.BiMap;
import com.google.common.collect.HashBiMap;

public class MainActivity extends Activity{

     private BiMap<String,String>  bidiMap; 

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);     
          setContentView(R.layout.bible_help_cal);

        bidiMap = HashBiMap.create();           
        bidiMap.put("a","100");
        bidiMap.put("b","200");


    }


}

Error Message I get

我收到的错误消息

05-29 18:35:19.737: E/AndroidRuntime(376): FATAL EXCEPTION: main
05-29 18:35:19.737: E/AndroidRuntime(376): java.lang.NoClassDefFoundError: com.google.common.collect.HashBiMap
05-29 18:35:19.737: E/AndroidRuntime(376):  at my.project.MainActivity.onCreate(MainActivity.java:18)
05-29 18:35:19.737: E/AndroidRuntime(376):  at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1048)
05-29 18:35:19.737: E/AndroidRuntime(376):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1712)
05-29 18:35:19.737: E/AndroidRuntime(376):  at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1764)
05-29 18:35:19.737: E/AndroidRuntime(376):  at android.app.ActivityThread.access00(ActivityThread.java:122)
05-29 18:35:19.737: E/AndroidRuntime(376):  at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1002)
05-29 18:35:19.737: E/AndroidRuntime(376):  at android.os.Handler.dispatchMessage(Handler.java:99)
05-29 18:35:19.737: E/AndroidRuntime(376):  at android.os.Looper.loop(Looper.java:132)
05-29 18:35:19.737: E/AndroidRuntime(376):  at android.app.ActivityThread.main(ActivityThread.java:4025)
05-29 18:35:19.737: E/AndroidRuntime(376):  at java.lang.reflect.Method.invokeNative(Native Method)
05-29 18:35:19.737: E/AndroidRuntime(376):  at java.lang.reflect.Method.invoke(Method.java:491)
05-29 18:35:19.737: E/AndroidRuntime(376):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:841)
05-29 18:35:19.737: E/AndroidRuntime(376):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:599)
05-29 18:35:19.737: E/AndroidRuntime(376):  at dalvik.system.NativeStart.main(Native Method)

回答by Paul Sanwald

This error means that the class was available at compile time, but cannot be found during run time. It most commonly happens when your compile time classpath is different from your runtime classpath.

此错误意味着该类在编译时可用,但在运行时找不到。当您的编译时类路径与运行时类路径不同时,最常发生这种情况。

It is very likely that your runtime classpath doesn't contain the guava jar. To verify this, try printing your classpathin your code.

您的运行时类路径很可能不包含番石榴 jar。要验证这一点,请尝试在代码中打印类路径

on the command line, you can use: java -cp "path/to/guava.jar" MyMainClass

在命令行上,您可以使用: java -cp "path/to/guava.jar" MyMainClass

or alternatively, set the CLASSPATH environment variable to include the jar.

或者,设置 CLASSPATH 环境变量以包含 jar。

回答by Jerry H

In my case, I had both guava and google-collection in my library. google-collection library was conflicting with guava library. If you have both guava and google-collection, try to remove google-collection to rebuild.

就我而言,我的图书馆中有番石榴和谷歌收藏。google-collection 库与番石榴库冲突。如果您同时拥有 guava 和 google-collection,请尝试删除 google-collection 以重建。

回答by lubosz

I was trying to use upstream Guave 21 and 22, with sourceCompatibility JavaVersion.VERSION_1_8enabled. This caused this error on devices below SDK version 24. The solution was to use a guave version that still supports Java 7, which was backported for Android.

我试图使用上游 Guave 21 和 22,并sourceCompatibility JavaVersion.VERSION_1_8启用。这在低于 SDK 版本 24 的设备上导致此错误。解决方案是使用仍支持 Java 7 的 guave 版本,该版本已为 Android 反向移植。

dependencies {
  compile 'com.google.guava:guava:23.3-android'
}

回答by ralphgabb

Just download Guava library from here Guava

只需从这里下载番石榴库番石榴

Clean and Build. Works for me. Hope it Helps. Cheers

清洁和建造。为我工作。希望能帮助到你。干杯

回答by Pontios

In my case, I tried to add the guava-17.0.jarwith the option Add External JARs...and this was the problem. When I moved the guava-17.0.jarin my projects folder(under the Libs file) I had no problem.

就我而言,我尝试添加guava-17.0.jarwith 选项Add External JARs...,这就是问题所在。当我移动guava-17.0.jar我的项目文件夹(在 Libs 文件下)时,我没有问题。

Of course I, removed the External JAR and add it again using the Add JARs...option

当然,我删除了外部 JAR 并使用Add JARs...选项再次添加它