Android 应用程序主活动出现 ClassNotFoundException

发布于 2024-11-10 04:07:41 字数 1761 浏览 1 评论 0原文

大多数设备都可以运行我的应用程序,但我收到此错误报告:

java.lang.RuntimeException: Unable to instantiate activity ComponentInfo{com.company.app/com.company.app.MainActivity}: java.lang.ClassNotFoundException: com.company.app.MainActivity in loader dalvik.system.PathClassLoader[/mnt/asec/com.company.app-1/pkg.apk]
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2755)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2854)
at android.app.ActivityThread.access$2300(ActivityThread.java:136)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2179)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:143)
at android.app.ActivityThread.main(ActivityThread.java:5068)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:521)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:858)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)
at dalvik.system.NativeStart.main(Native Method)
Caused by: java.lang.ClassNotFoundException: com.company.app.MainActivity in loader dalvik.system.PathClassLoader[/mnt/asec/com.company.app-1/pkg.apk]
at dalvik.system.PathClassLoader.findClass(PathClassLoader.java:243)
at java.lang.ClassLoader.loadClass(ClassLoader.java:573)
at java.lang.ClassLoader.loadClass(ClassLoader.java:532)
at android.app.Instrumentation.newActivity(Instrumentation.java:1034)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2747)
... 11 more

我发现有一个相关的答案: ClassNotFoundException Android

但问题是,我的异常发生在我编写的任何代码行之前,那么我如何确保类加载器加载了吗?

Most of the devices can run my app but I got this error report :

java.lang.RuntimeException: Unable to instantiate activity ComponentInfo{com.company.app/com.company.app.MainActivity}: java.lang.ClassNotFoundException: com.company.app.MainActivity in loader dalvik.system.PathClassLoader[/mnt/asec/com.company.app-1/pkg.apk]
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2755)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2854)
at android.app.ActivityThread.access$2300(ActivityThread.java:136)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2179)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:143)
at android.app.ActivityThread.main(ActivityThread.java:5068)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:521)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:858)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)
at dalvik.system.NativeStart.main(Native Method)
Caused by: java.lang.ClassNotFoundException: com.company.app.MainActivity in loader dalvik.system.PathClassLoader[/mnt/asec/com.company.app-1/pkg.apk]
at dalvik.system.PathClassLoader.findClass(PathClassLoader.java:243)
at java.lang.ClassLoader.loadClass(ClassLoader.java:573)
at java.lang.ClassLoader.loadClass(ClassLoader.java:532)
at android.app.Instrumentation.newActivity(Instrumentation.java:1034)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2747)
... 11 more

I found there is a related answer:
ClassNotFoundException Android

but the question is, my exception occurs before any line of code i write, so how can I ensure the class loader is loaded?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(13

风铃鹿 2024-11-17 04:07:41

我有完全相同的问题。突然我的应用程序停止工作了。我没有对清单进行任何更改,但由于一些意外,缺少第一行:

这导致了 ClassNotFoundException 用于启动器活动。我为此苦苦挣扎了几个小时,每次检查舱单是否可能出现错误时,我都忽略了这一点。

我再次删除这一行后发生了奇怪的事情。惊喜,惊喜应用程序仍然有效。 (而且我没有忘记重建应用程序)

希望这可以帮助某人节省几个小时。

I had exactly the same problem. Suddenly my app stopped working. I didint make any change to manifest, but with some accident there was missing first row:

<?xml version="1.0" encoding="utf-8"?>

This caused ClassNotFoundException for launcher activity. I was struggling with that for few hours and I overlooked this every time I was checking manifest for possible mistake.

Strange thing happened after I have deleted this row again. Suprise, suprise app was still working. (and I didnt forget to rebuild app)

Hope this help someone to save few hours.

风柔一江水 2024-11-17 04:07:41

当我的类在清单中未正确定义时,我会收到此错误,通常是因为我没有输入正确的名称空间,如下所示:

    <activity android:name="com.company.project.main.MyMainActivity" ...>

而不是

    <activity android:name="MyMainActivity" ...>

不确定为什么它在某些设备上适用,但在其他设备上不适用。

I get this error when my class isn't correctly defined in the manifest, typically because I haven't put in the correct name space as in:

    <activity android:name="com.company.project.main.MyMainActivity" ...>

as opposed to

    <activity android:name="MyMainActivity" ...>

Not sure why it would work for you on some devices and not others though.

素罗衫 2024-11-17 04:07:41

请关闭您的日食并打开。清理您的项目并进行刷新。然后跑过去看看。

Please close your eclipse and open. Clean your project and do a refresh. Then run and see.

绳情 2024-11-17 04:07:41

我遇到了同样的问题,问题是使用 Android 支持库的第三方库。

myproject/libs/android-support-v4.jar 
3rdpartylib/libs/android-support-v4.jar

当我用新版本更新库时,支持库也被更新 - 但不在我的项目中。之后我无法启动该应用程序并出现此错误。使两个库相同(复制)后,就可以了。

I had the same issue and the problem was a third party library that is using Android Support Library.

myproject/libs/android-support-v4.jar 
3rdpartylib/libs/android-support-v4.jar

When I updated the library with newer version, the support library was also updated - but not in my project. After that I could not launch the application with this error. After making both libraries to be the same (copy), it was ok.

脸赞 2024-11-17 04:07:41

在 Eclipse 中,检查“属性”>“Java 构建路径”>“源”并查看输出文件夹是否始终是默认文件夹(/bin/classes)。

对我来说,这是一个由于 Maven 插件而导致的问题:每当我右键单击>Maven>更新项目...>确定时,它会将 src 文件夹的输出文件夹更改为 target/classes,从而导致 ClassNotFoundException。

In eclipse, check Properties>Java Build Path>Source and see if the output folder is always the default (/bin/classes).

For me, this was an issue because of the maven plugin: whenever I right click>Maven>Update Project...>Ok, it changes the output folder of the src folder to target/classes, causing the ClassNotFoundException.

末が日狂欢 2024-11-17 04:07:41

我刚刚遇到这个问题。事实证明,这是因为我已将 MainActivity 移至新包中。

我所做的是将其从

com.example.MainActivity

移入,

Activity

因此在我的清单中它看起来像这样:

 android:name="Activity.MainActivity"`

通过将其移回原始包来解决它。它也可以通过正确重命名你的包来解决:

例如:com.example.Activity.MainActivity

希望这对某人有帮助。

I just ran into this problem. Turns out it was because I had moved my MainActivity into a new package.

What I did was move it from

com.example.MainActivity

into

Activity

so in my manifest it looked like this:

 android:name="Activity.MainActivity"`

Solved it by moving it back into the original package. It can also be solved by renaming your packages correctly:

Eg: com.example.Activity.MainActivity

Hope this helps someone.

记忆里有你的影子 2024-11-17 04:07:41

最近几天我遇到了这个问题。我可以在每个测试环境上运行我的应用程序,我可以导出一个发行版 apk,然后在每个测试环境上安装并运行它,但是当我将 apk 上传到 Play 商店并从 Play 商店安装时,应用程序将关闭并抛出 ClassNotFoundException 。事实证明,在我的项目中,我将多部分表单数据发送到 Web API,为此需要一些外部库。起初我只是包含了所有的 mim4jhttpclienthttpcore .jars (结果是很多罐子)。

在一一删除几乎所有这些并查看我的项目是否仍然可以构建之后,我只剩下一个实际上需要的罐子。要点是删除不需要构建的所有 jar。然后我测试了该应用程序,结果发现我需要添加其中一个 jar,因为它的资源被构建项目所需的资源所引用。确保在删除 jar 后测试应用程序的每个功能。

然后我浏览了我包含的每个库项目,并右键单击 project ->;属性-> java构建路径->订购和导出并选中所有复选框对于每个图书馆我自己的包

然后我做了一个项目 ->对包含的每个项目和主项目进行清理。

然后我导出了 apk 并将该包进行 beta 测试,它终于在用户设备上运行了。

I had this problem for the last couple days. I could run my app on every test environment, I could export a release apk, and install and run it on every test environment, but when I uploaded the apk to the play store and installed from the play store the app would fource close throwing ClassNotFoundException. It turned out that in my project I was sending multipart form data to a web api, and to do this requires some external libraries. At first I just included all the mim4j, httpclient, and httpcore .jars (turns out to be a lot of jars).

After deleting almost all of them one by one and seeing if my project would still build I was left with only one jar THAT WAS ACTUALLY REQUIRED. The take away is delete any jars that are not required to build. Then I tested the app and turns out I needed to add one of the jars back because it's resources were referenced by the one that was required to build the project. Make sure to test every function of your app after deleting jars.

Then I went through each library project I was including and went to right-click on project -> properties -> java build path -> order and export and checked all the boxes FOR EVERY LIBRARY and MY OWN PACKAGE.

Then I did a project -> clean on every project that is included and the main project.

Then I exported the apk and put the package up for beta testing and it finally worked on users devices.

铁轨上的流浪者 2024-11-17 04:07:41

我遇到了这个问题,并使用 这个

我没有在“订购和导出”下检查任何外部库,并且收到错误。

I ran into this problem and it was fixed using this.

I had not checked one external library under "Order and export" and i was getting the error.

空城之時有危險 2024-11-17 04:07:41

上面的答案都不适合我,如果将来能为任何人节省一些时间,请贡献我的解决方案。

显然对我有用的是删除我的项目中额外的支持 lib v4 jar。

在我通过 android 工具更新支持库后,我必须修复我使用的许多库中的 jar 不匹配问题,并将它们全部指向 android-sdks 文件夹中的同一个 jar。

我也不小心将它添加到我的项目中,由于某种原因,它使我的 MainActivity 类在运行时找不到,因为该 jar 也在我正在使用的 android-support-v7-appcompat 库中。

eclipse 上完全没有错误。

我从项目中删除了该 jar,并在导出选项卡中检查了 v7 appcompat 库中的 jar,以便它也可用于我的项目,瞧,应用程序正在运行。

None of the answers above worked for me, contributing my solution if it's going to spare some hours to anyone in the future.

What apparently worked for me was removing an extra support lib v4 jar in my project.

After I updated the support lib via android tools, I had to fix jar mismatches in many libraries I use and point them all to the same jar in the android-sdks folder.

I accidently added it to my project as well, and for some reason it made my MainActivity class not found in runtime, because the jar was also in the android-support-v7-appcompat library I was using.

No errors on eclipse at all.

I removed the jar from my project, and checked the jar in the v7 appcompat library in the export tab so it would be available to my project as well, and voila, app is working.

荒路情人 2024-11-17 04:07:41

感谢版本控制,我注意到我的 .classpath xml 文件中没有这些行:

<classpathentry kind="src" path="src"/>
<classpathentry kind="src" path="gen"/>

这些导致主活动找不到类。

Thanks to version control, I noticed that I didn't have these lines in my .classpath xml file:

<classpathentry kind="src" path="src"/>
<classpathentry kind="src" path="gen"/>

These caused Class-not-found for main activity.

爱的故事 2024-11-17 04:07:41

运行 Robolectric 测试时我也得到了同样的结果。实际上,测试类路径包含带有 android.com.Activity 的 jar,但尚未创建。
我的解决方案是从 Maven 项目中使用的第 10 个 JDK 版本降级 JAVA_HOME。

I've got the same when run Robolectric tests. Actually, test classpath includes jar with android.com.Activity, which has not been founded.
A solution in my case is downgrade JAVA_HOME from 10th JDK version, which is used in Maven project.

回忆凄美了谁 2024-11-17 04:07:41

我将以下文件夹复制/粘贴到新项目中,保留相同的包名称,并解决了它:

  • ../src
  • /debug ../src/main
  • ../src/release

最后我添加了文件依赖项build.gradle

GL

I made a copy/paste of the following folders to a new project, keeping the same package name, and solved it:

  • ../src/debug
  • ../src/main
  • ../src/release

And finally I added the file dependencies build.gradle.

GL

ヅ她的身影、若隐若现 2024-11-17 04:07:41

如果您使用某些开发依赖包更改了 flutter 中的包名称,则必须交叉检查

  1. androidmanifest.xml => 中的 两个区域"

If you changed your package name in flutter using some dev dependency package you have to cross check the both area

  1. In androidmanifest.xml => "<activity android:name="your new package name.MainActivity"
    2.In mainActivity.java
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文