eclipse Android 应用程序未处理的异常:找不到类“X”,从方法“Y”引用

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/13882567/
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-09-19 19:37:07  来源:igfitidea点击:

Android application unhandled exception: Could not find class 'X', referenced from method 'Y'

javaandroideclipse

提问by Patrick Bateman

I am a bit new to Android development so bear with me.

我对 Android 开发有点陌生,所以请耐心等待。

I've already reviewed this MapView: Could not find class A referenced from method Band this Could not find method XXX, referenced from method YYY

我已经查看了这个MapView:找不到从方法 B 引用的类 A和这个找不到方法 XXX,从方法 YYY 引用

but they do not help solve my issue.

但它们无助于解决我的问题。

I am getting the error reported in my question title when trying to run my app. My app requires a java project that I pulled from Github, which itself required a handful of jar files to compile on its own. I have the java project compiled, and Eclipse makes my android project appear to be compiled, but it isn't working on my phone.

尝试运行我的应用程序时,我收到问题标题中报告的错误。我的应用程序需要一个我从 Github 中提取的 Java 项目,它本身需要一些 jar 文件才能自行编译。我编译了 java 项目,Eclipse 使我的 android 项目看起来已经编译,但它在我的手机上不起作用。

I tried the following, all leading to the same result:

我尝试了以下,都导致相同的结果:

  • referenced the java library directly using the java build path
  • built an android project library which references the java library directly using the java build path, then adding the android project library to my projects android library path
  • built an android project library which references a jar I built from the java library, then adding the android project library to my projects android library path
  • built a jar from the java library, then adding the jar to the build path of my android project
  • 使用java构建路径直接引用java库
  • 使用java构建路径构建了一个直接引用java库的android项目库,然后将android项目库添加​​到我的项目android库路径中
  • 构建了一个 android 项目库,它引用了我从 java 库构建的 jar,然后将 android 项目库添加​​到我的项目 android 库路径中
  • 从java库构建了一个jar,然后将jar添加到我的android项目的构建路径中

the end result is always the same, my program compiles but when I try to run it crashes saying "could not find class "class from the GitHub project" referenced from "my main activity method"

最终结果总是相同的,我的程序编译但当我尝试运行它时崩溃说“找不到类”来自 GitHub 项目的类”引用自“我的主要活动方法”

Could anyone throw a bone?

有人可以扔骨头吗?

Logcat

逻辑猫

    12-14 11:08:49.792: I/Process(5538): Sending signal. PID: 5538 SIG: 9
12-14 11:09:56.722: D/szipinf(5626): Initializing inflate state
12-14 11:09:56.732: E/dalvikvm(5626): Could not find class 'gmusic.api.impl.GoogleSkyJamAPI', referenced from method com.example.gpmx.MainActivity.runGoogleAPI
12-14 11:09:56.732: W/dalvikvm(5626): VFY: unable to resolve new-instance 445 (Lgmusic/api/impl/GoogleSkyJamAPI;) in Lcom/example/gpmx/MainActivity;
12-14 11:09:56.732: D/dalvikvm(5626): VFY: replacing opcode 0x22 at 0x0000
12-14 11:09:56.732: D/dalvikvm(5626): VFY: dead code 0x0002-0036 in Lcom/example/gpmx/MainActivity;.runGoogleAPI (Ljava/lang/String;Ljava/lang/String;)V
12-14 11:09:56.812: D/AndroidRuntime(5626): Shutting down VM
12-14 11:09:56.812: W/dalvikvm(5626): threadid=1: thread exiting with uncaught exception (group=0x40018560)
12-14 11:09:56.812: E/AndroidRuntime(5626): FATAL EXCEPTION: main
12-14 11:09:56.812: E/AndroidRuntime(5626): java.lang.NoClassDefFoundError: gmusic.api.impl.GoogleSkyJamAPI
12-14 11:09:56.812: E/AndroidRuntime(5626):     at com.example.gpmx.MainActivity.runGoogleAPI(MainActivity.java:63)
12-14 11:09:56.812: E/AndroidRuntime(5626):     at com.example.gpmx.MainActivity.onResume(MainActivity.java:36)
12-14 11:09:56.812: E/AndroidRuntime(5626):     at android.app.Instrumentation.callActivityOnResume(Instrumentation.java:1150)
12-14 11:09:56.812: E/AndroidRuntime(5626):     at android.app.Activity.performResume(Activity.java:3832)
12-14 11:09:56.812: E/AndroidRuntime(5626):     at android.app.ActivityThread.performResumeActivity(ActivityThread.java:2231)
12-14 11:09:56.812: E/AndroidRuntime(5626):     at android.app.ActivityThread.handleResumeActivity(ActivityThread.java:2256)
12-14 11:09:56.812: E/AndroidRuntime(5626):     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1789)
12-14 11:09:56.812: E/AndroidRuntime(5626):     at android.app.ActivityThread.access00(ActivityThread.java:123)
12-14 11:09:56.812: E/AndroidRuntime(5626):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:939)
12-14 11:09:56.812: E/AndroidRuntime(5626):     at android.os.Handler.dispatchMessage(Handler.java:99)
12-14 11:09:56.812: E/AndroidRuntime(5626):     at android.os.Looper.loop(Looper.java:130)
12-14 11:09:56.812: E/AndroidRuntime(5626):     at android.app.ActivityThread.main(ActivityThread.java:3835)
12-14 11:09:56.812: E/AndroidRuntime(5626):     at java.lang.reflect.Method.invokeNative(Native Method)
12-14 11:09:56.812: E/AndroidRuntime(5626):     at java.lang.reflect.Method.invoke(Method.java:507)
12-14 11:09:56.812: E/AndroidRuntime(5626):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:864)
12-14 11:09:56.812: E/AndroidRuntime(5626):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:622)
12-14 11:09:56.812: E/AndroidRuntime(5626):     at dalvik.system.NativeStart.main(Native Method)

One more piece of information, if this helps. The java project I am using depends on various apache and google jars, which are in the java projects "lib" (not "libs") folder. Is this a problem when creating a jar?

如果有帮助,再提供一条信息。我正在使用的 java 项目取决于各种 apache 和 google jar,它们位于 java 项目“lib”(不是“libs”)文件夹中。这是创建 jar 时的问题吗?

Final update:It turned out that there was a lesson learned. At least with my configuration, all of the jars I needed in the android library had to be at the same directory level, that is, just under "libs". I have a couple of required jars, like apaches common.io jar within it's own directory under "libs", this was causing a problem.

最后更新:原来是吸取了教训。至少在我的配置中,我在 android 库中需要的所有 jar 都必须在同一目录级别,即在“libs”下。我有几个必需的 jar,比如 apaches common.io jar,位于“libs”下它自己的目录中,这导致了一个问题。

回答by jamis0n

See this SO postfor correctly importing an existing project into Eclipse...

请参阅此 SO 帖子以将现有项目正确导入 Eclipse ...

The most useful post seems to be:

最有用的帖子似乎是:

  1. File->Import->General->Existing Projects into Workspace, Next
  2. Select root directory: /path/to/project
  3. Projects->Select All
  4. UNCHECK both "Copy projects into workspace" and "Add project to working sets"
  5. Finish
  1. File->Import->General->Existing Projects into Workspace,Next
  2. 选择根目录:/path/to/project
  3. 项目->全选
  4. 取消选中“将项目复制到工作区”和“将项目添加到工作集”
  5. 结束

To add it as a library for another project, see this linkfrom the Android dev site.

要将其添加为另一个项目的库,请参阅Android 开发站点中的此链接

  1. In the Package Explorer, right-click the library project and select Properties.
  2. In the Properties window, select the "Android" properties group at left and locate the Library properties at right.
  3. Select the "is Library" checkbox and click Apply.
  1. 在包资源管理器中,右键单击库项目并选择属性。
  2. 在“属性”窗口中,选择左侧的“Android”属性组并找到右侧的库属性。
  3. 选中“是库”复选框并单击“应用”。

回答by leifeng

I had the same issue. And finally found the solution.

我遇到过同样的问题。并最终找到了解决方案。

The java project you imported should be built when you build your android project.

您导入的java项目应该在您构建android项目时构建。

Java build path -> Order and Export, check the java project and then rebuild your android project.

Java build path -> Order and Export,检查java项目,然后重建你的android项目。

回答by Peter Wiles

I had the same problem after importing OSMDroid into my app. I checking the LogCat output, which showed that the Dex loader couldn't find a particular Class (GeoPoint) in OSMDroid. I realised that there were no class files in the bin folder, i.e. it hadn't been compiled. The reason was that my import had put the source into a library called "org", rather than into "src". Moving it into "src" and rebuilding OSMDroid generated the proper Class files in "bin" and all now works.

将 OSMDroid 导入我的应用程序后,我遇到了同样的问题。我检查了 LogCat 输出,结果表明 Dex 加载器在 OSMDroid 中找不到特定的类 (GeoPoint)。我意识到 bin 文件夹中没有类文件,即它没有被编译。原因是我的导入已将源代码放入名为“org”的库中,而不是放入“src”中。将其移动到“src”并重建 OSMDroid 在“bin”中生成正确的类文件,现在一切正常。