android强制关闭:Activity类的ClassNotFoundException

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

android force close: ClassNotFoundException for Activity class

android

提问by Anselm Eickhoff

When I start my (soon-to-be) android game (from eclipse) it opens, but immediately force-closes.

当我开始我的(即将成为)Android 游戏(来自 eclipse)时,它会打开,但会立即强制关闭。

Logcat says:

Logcat 说:

07-09 17:12:35.709: ERROR/AndroidRuntime(3866): Uncaught handler: thread main exiting due to uncaught exception
07-09 17:12:35.719: ERROR/AndroidRuntime(3866): java.lang.RuntimeException: Unable to instantiate activity ComponentInfo{org.anselm.eickhoff.rhythm/org.anselm.eickhoff.rhythm.RhythmGameActivity}: java.lang.ClassNotFoundException: org.anselm.eickhoff.rhythm.RhythmGameActivity in loader dalvik.system.PathClassLoader@4001e740
07-09 17:12:35.719: ERROR/AndroidRuntime(3866):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2497)
07-09 17:12:35.719: ERROR/AndroidRuntime(3866):     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2621)
07-09 17:12:35.719: ERROR/AndroidRuntime(3866):     at android.app.ActivityThread.access00(ActivityThread.java:126)
07-09 17:12:35.719: ERROR/AndroidRuntime(3866):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1932)
07-09 17:12:35.719: ERROR/AndroidRuntime(3866):     at android.os.Handler.dispatchMessage(Handler.java:99)
07-09 17:12:35.719: ERROR/AndroidRuntime(3866):     at android.os.Looper.loop(Looper.java:123)
07-09 17:12:35.719: ERROR/AndroidRuntime(3866):     at android.app.ActivityThread.main(ActivityThread.java:4595)
07-09 17:12:35.719: ERROR/AndroidRuntime(3866):     at java.lang.reflect.Method.invokeNative(Native Method)
07-09 17:12:35.719: ERROR/AndroidRuntime(3866):     at java.lang.reflect.Method.invoke(Method.java:521)
07-09 17:12:35.719: ERROR/AndroidRuntime(3866):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:860)
07-09 17:12:35.719: ERROR/AndroidRuntime(3866):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:618)
07-09 17:12:35.719: ERROR/AndroidRuntime(3866):     at dalvik.system.NativeStart.main(Native Method)
07-09 17:12:35.719: ERROR/AndroidRuntime(3866): Caused by: java.lang.ClassNotFoundException: org.anselm.eickhoff.rhythm.RhythmGameActivity in loader dalvik.system.PathClassLoader@4001e740
07-09 17:12:35.719: ERROR/AndroidRuntime(3866):     at dalvik.system.PathClassLoader.findClass(PathClassLoader.java:243)
07-09 17:12:35.719: ERROR/AndroidRuntime(3866):     at java.lang.ClassLoader.loadClass(ClassLoader.java:573)
07-09 17:12:35.719: ERROR/AndroidRuntime(3866):     at java.lang.ClassLoader.loadClass(ClassLoader.java:532)
07-09 17:12:35.719: ERROR/AndroidRuntime(3866):     at android.app.Instrumentation.newActivity(Instrumentation.java:1021)
07-09 17:12:35.719: ERROR/AndroidRuntime(3866):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2489)
07-09 17:12:35.719: ERROR/AndroidRuntime(3866):     ... 11 more

the interesting line here is (I think):

这里有趣的一行是(我认为):

07-09 17:12:35.719: ERROR/AndroidRuntime(3866): Caused by: java.lang.ClassNotFoundException: org.anselm.eickhoff.rhythm.RhythmGameActivity in loader dalvik.system.PathClassLoader@4001e740

Which surprises me, because I have this class (in the right package)

这让我感到惊讶,因为我有这门课(在正确的包中)

edit:to clarify, added the first line which I had omitted (together with the imports)

编辑:为了澄清,添加了我省略的第一行(连同导入)

package org.anselm.eickhoff.rhythm;
...
public class RhythmGameActivity extends Activity {


    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

    }

    @Override
    public void onPause() {
    }
}

and that's all it does!

这就是它所做的一切!

I also registered it in the manifest:

我也在清单中注册了它:

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
      package="org.anselm.eickhoff.rhythm"
      android:versionCode="1" android:versionName="pre-alpha">

    <uses-permission android:name="android.permission.INTERNET" />
    <application android:icon="@drawable/icon" android:label="@string/app_name" android:debuggable="true" android:hasCode="false">

    <activity android:name=".RhythmGameActivity">
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />
            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>
    </application>

</manifest> 

edit:this all has started since I renamed the activity from RhythmGame to RhythmGameActivity, but I am pretty sure I replaced all the references so maybe it is still wrongly chached somewhere? (I tried refreshing and cleaning the project)

编辑:自从我将活动从 RhythmGame 重命名为 RhythmGameActivity 以来,这一切都已经开始,但我很确定我替换了所有引用,所以也许它仍然在某处被错误地查了一下?(我尝试刷新和清理项目)

Your help is really appreciated - im stuck!

非常感谢您的帮助 - 我被卡住了!

采纳答案by jchristof

I use Perforce with Eclipse and have discovered that Perforce's default behavior of making all files under source control read-only causes build problems that aren't clearly specified in Eclipse's build window. Also, there seems to be a quirk for me when adding a library project where even if the .classpath and .project file are writable the library may not properly associate itself with the project. I generally follow these steps for resolving errors of this nature (this assumes that the project's manifest is correct):

我将 Perforce 与 Eclipse 一起使用,并发现 Perforce 将源代码控制下的所有文件设为只读的默认行为会导致在 Eclipse 的构建窗口中未明确指定的构建问题。此外,在添加库项目时,我似乎有一个怪癖,即使 .classpath 和 .project 文件是可写的,库也可能无法将自身与项目正确关联。我通常按​​照以下步骤来解决这种性质的错误(假设项目的清单是正确的):

1) Close Eclipse. Eclipse may cache some info about a file's R/W status.

1) 关闭 Eclipse。Eclipse 可能会缓存有关文件 R/W 状态的一些信息。

2) (Broad stroke) Make all of the files in the project writable either by checking them out from source control, or through the OS. .classpath and .project at the bare minimum should be writable.

2)(广义)通过从源代码管理或通过操作系统检出项目中的所有文件,使它们可写。.classpath 和 .project 至少应该是可写的。

2.5) If you placed files under source control that are generated during the build process and are a normal part of the Android build process then they should be removed from source control and made writable. Including but not limited to .class and files in the bin and gen directories.

2.5) 如果您将在构建过程中生成的文件置于源代码管理之下,并且是 Android 构建过程的正常部分,那么它们应该从源代码管理中删除并使其可写。包括但不限于bin和gen目录下的.class和文件。

3) Open the project in Eclipse. If there are no errors the problem may be fixed.

3) 在 Eclipse 中打开项目。如果没有错误,问题可能会得到解决。

4) Examine the project explorer and look specifically for library project dependencies that may be missing. In my experience it's important to see all lib projects' directories and their library status icon in your project's explorer hierarchy. If a lib project folder and icon is missing, go to Properties->Android and select and add the missing lib(s). After adding a lib select "Apply" an watch the project explorer to make sure that the lib icon appears in the project directory. This step explanation may seem overly detailed, but I've been bitten several times before forcing myself to be extra observant here. If a lib won't add, try adding other dummy lib project and removing them in combination with the lib you want. No joke - this is sometimes necessary for me.

4) 检查项目资源管理器并专门查找可能丢失的库项目依赖项。根据我的经验,在项目的资源管理器层次结构中查看所有 lib 项目的目录及其库状态图标很重要。如果缺少 lib 项目文件夹和图标,请转到 Properties->Android 并选择并添加缺少的 lib(s)。添加 lib 后,选择“应用”并观察项目资源管理器以确保 lib 图标出现在项目目录中。这一步的解释可能看起来过于详细,但我已经被咬过好几次了,然后才强迫自己在这里加倍观察。如果不会添加 lib,请尝试添加其他虚拟 lib 项目并将它们与您想要的 lib 结合删除。不是开玩笑 - 这对我来说有时是必要的。

5) Clean all of the projects.

5) 清理所有项目。

6) Read the error list and resolve any other errors.

6) 阅读错误列表并解决任何其他错误。

7) Fix all of the warnings you've been putting off fixing.

7) 修复您一直推迟修复的所有警告。

8) (Not essential but important) Revert all unchanged files and observe what's left in you changelist - these are the files that will need your attention in the future to prevent build problems.

8)(不是必需但很重要)恢复所有未更改的文件并观察更改列表中剩余的内容 - 这些是将来需要您注意以防止构建问题的文件。

回答by Yar

Just had the same problem. After updating ADT to the newest version, my

刚刚有同样的问题。将 ADT 更新到最新版本后,我的

lib
文件夹不再被识别。不得不将其重命名为
libs
现在:他们真的必须每 5 分钟发布一次新的 SDK 吗?他们不能创造一个更长时间的稳定环境吗?

回答by Martin

Try to fix .classpath.

尝试修复 .classpath。

Had the same problem caused by corrupt .classpath file. After fixing it the ClassNotFoundException was gone.

有由损坏的 .classpath 文件引起的相同问题。修复它后 ClassNotFoundException 消失了。

The background is that we are using SVN, an update caused a conflict in .classpath. Eclipse, however, did not show any error message or hint about that. I only found out when I tried to submit the current changes.

背景是我们使用的是SVN,一次更新导致.classpath冲突。但是,Eclipse 没有显示任何错误消息或提示。我是在尝试提交当前更改时才发现的。

The solution here was to do a fresh SVN check out of the project, so a working version of .classpath was restored.

此处的解决方案是对项目进行新的 SVN 检查,因此恢复了 .classpath 的工作版本。

回答by Kamran

I had this problem once when my working application suddenly started throwing ClassNotFoundException. The problem was that the classes were not being compiled and translated to the "dex" file, this is obvious to find because the APK just shrinks.

当我的工作应用程序突然开始抛出 ClassNotFoundException 时,我遇到过这个问题。问题是这些类没有被编译并翻译成“dex”文件,这很明显,因为 APK 只是缩小了。

So, in eclipse, in order to fix it just add the javabuilder to your .project file.

因此,在 eclipse 中,为了修复它,只需将 javabuilder 添加到您的 .project 文件中。

<buildCommand>
  <name>org.eclipse.jdt.core.javabuilder</name>
  <arguments>
  </arguments>
</buildCommand>

Hope it helps.

希望能帮助到你。

回答by yydl

Just had this same problem. Was going nuts. Fully restarted emulator/adb/and eclipse and it fixed it. Strange stuff...

刚刚有同样的问题。快疯了。完全重新启动 emulator/adb/and eclipse 并修复它。奇怪的东西...

回答by Edwin Evans

Did you rename in Eclipse. Double check that it renamed with sub-package correctly. In my case it removed the dot:

你在 Eclipse 中重命名了吗?仔细检查它是否正确使用子包重命名。在我的情况下,它删除了点:

Wrong: ui.MyActivity

错误:ui.MyActivity

Fix: .ui.MyActivity

修复:.ui.MyActivity

回答by Jean-Philippe Jodoin

I had the same problem and in my case, I had forgot to put a library in the Android Manifest. I had a MapView Activity and I had forgot to put :

我遇到了同样的问题,就我而言,我忘记在 Android 清单中放置一个库。我有一个 MapView 活动,但我忘了放:

<uses-library android:name="com.google.android.maps"/>.

So, you should make sure you have all your library included in your manifest.

因此,您应该确保清单中包含所有库。

回答by Anselm Eickhoff

Not really a solution but this fixed it:

不是真正的解决方案,但这修复了它:

creating a new project from scratch and migrating the code

从头开始创建一个新项目并迁移代码

Please close this if something like it is possible here.

如果这里有可能,请关闭它。

Thanks for all your ideas and thoughts!

感谢您的所有想法和想法!

回答by iMx

I got this error in a FragmentActivity after updating the SDK to Android 4. The reason was, that you no longer need to include the android-support-v4.jar. Just add the support tools by clicking right mouse button on your Project, than Android Tools -> Add support library.

将 SDK 更新到 Android 4 后,我在 FragmentActivity 中收到此错误。原因是,您不再需要包含 android-support-v4.jar。只需在您的项目上单击鼠标右键添加支持工具,而不是 Android 工具 -> 添加支持库。

回答by SocialError

I know it's not your problem , but 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"?>

<?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.

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

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.

希望这有助于某人节省几个小时。