Android Activity ClassNotFoundException - 尝试了一切 [英] Android Activity ClassNotFoundException - tried everything
问题描述
我刚刚将一个应用程序重构为一个框架库和一个应用程序,但是现在当我尝试在模拟器中启动该应用程序时,我收到以下错误堆栈跟踪:
I've just refactored an app into a framework library and an application, but now when I try and start the app in the emulator I get the following error stack trace:
06-02 18:22:35.529: E/AndroidRuntime(586): FATAL EXCEPTION: main
06-02 18:22:35.529: E/AndroidRuntime(586): java.lang.RuntimeException: Unable to instantiate activity ComponentInfo{com.matthewrathbone.eastersays/com.matthewrathbone.eastersays.EasterSimonSaysActivity}: java.lang.ClassNotFoundException: com.matthewrathbone.eastersays.EasterSimonSaysActivity in loader dalvik.system.PathClassLoader[/data/app/com.matthewrathbone.eastersays-1.apk]
06-02 18:22:35.529: E/AndroidRuntime(586): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2585)
06-02 18:22:35.529: E/AndroidRuntime(586): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2679)
06-02 18:22:35.529: E/AndroidRuntime(586): at android.app.ActivityThread.access$2300(ActivityThread.java:125)
06-02 18:22:35.529: E/AndroidRuntime(586): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2033)
06-02 18:22:35.529: E/AndroidRuntime(586): at android.os.Handler.dispatchMessage(Handler.java:99)
06-02 18:22:35.529: E/AndroidRuntime(586): at android.os.Looper.loop(Looper.java:123)
06-02 18:22:35.529: E/AndroidRuntime(586): at android.app.ActivityThread.main(ActivityThread.java:4627)
06-02 18:22:35.529: E/AndroidRuntime(586): at java.lang.reflect.Method.invokeNative(Native Method)
06-02 18:22:35.529: E/AndroidRuntime(586): at java.lang.reflect.Method.invoke(Method.java:521)
06-02 18:22:35.529: E/AndroidRuntime(586): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868)
06-02 18:22:35.529: E/AndroidRuntime(586): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)
06-02 18:22:35.529: E/AndroidRuntime(586): at dalvik.system.NativeStart.main(Native Method)
06-02 18:22:35.529: E/AndroidRuntime(586): Caused by: java.lang.ClassNotFoundException: com.matthewrathbone.eastersays.EasterSimonSaysActivity in loader dalvik.system.PathClassLoader[/data/app/com.matthewrathbone.eastersays-1.apk]
06-02 18:22:35.529: E/AndroidRuntime(586): at dalvik.system.PathClassLoader.findClass(PathClassLoader.java:243)
06-02 18:22:35.529: E/AndroidRuntime(586): at java.lang.ClassLoader.loadClass(ClassLoader.java:573)
06-02 18:22:35.529: E/AndroidRuntime(586): at java.lang.ClassLoader.loadClass(ClassLoader.java:532)
06-02 18:22:35.529: E/AndroidRuntime(586): at android.app.Instrumentation.newActivity(Instrumentation.java:1021)
06-02 18:22:35.529: E/AndroidRuntime(586): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2577)
06-02 18:22:35.529: E/AndroidRuntime(586): ... 11 more
通常这意味着清单文件在某种程度上是错误的,但我已经仔细检查了我能想到的所有内容.
Usually this means that the manifest file is wrong in some way, but I've double checked everything I can think of.
这是我的活动课:
package com.matthewrathbone.eastersays;
import android.os.Bundle;
import com.rathboma.simonsays.Assets.Season;
import com.rathboma.simonsays.SeasonPicker;
import com.rathboma.simonsays.SimonSaysActivity;
public class EasterSimonSaysActivity extends SimonSaysActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
}
@Override
protected void onDestroy() {
// TODO Auto-generated method stub
super.onDestroy();
}
@Override
public SeasonPicker getSeasonPicker() {
return new SeasonPicker(){
@Override
public Season getSeason() {
// TODO Auto-generated method stub
return Season.EASTER;
}
};
}
}
如您所见,它已正确列在清单中:
As you can see, it's listed correctly in the manifest:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.matthewrathbone.eastersays"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk android:minSdkVersion="7" android:targetSdkVersion="15" />
<application
android:icon="@drawable/ic_launcher"
android:label="@string/app_name" >
<activity
android:name=".EasterSimonSaysActivity"
android:label="@string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
我不知道如何解决这个问题,希望得到任何帮助.我在 SO 上扫描了许多类似的问题,但没有看到这种特殊行为.
I have no idea how to fix this, and would appreciate any help. I've scanned many similar questions on SO without seeing this particular behavior.
更多信息:
- 我检查了生成的 APK 内部,该类在classes.dex 文件
- 我已经尝试在日食
- 我尝试使用一个全新的设备映像,该映像没有已经有一份 APK 副本
- 我已将库项目更改为一个普通的java,然后改回一个android项目,没有区别
- 将抽象的 SimonSaysActivity 添加到清单中没有任何区别.
- 我尝试将每个依赖项都设为 android 库项目,并同步他们需要的 android 版本,但没有帮助
找到解决方案(见下文).致所有发布答案/评论的人:你们都很摇滚,感谢帮助我解决问题!
Found the solution (see below). To everyone that posted an answer / comment: You all rock, thanks for helping me work through the problems!
看起来这是由 SDK 工具升级引入的.感谢@Nick 在此链接的评论中:http://iqadd.com/item/noclassdeffounderror-adt-fix
Looks like this is introduced by an SDK tools upgrade. Thanks to @Nick below in the comments for this link: http://iqadd.com/item/noclassdeffounderror-adt-fix
推荐答案
我花了一些时间玩我自己的项目,我能够复制您的问题并在尝试运行我的主项目时获得完全相同的异常堆栈跟踪,所以我认为这可能是原因:
I spent some time play with my own project, and I am able to replicate your problem and get exactly the same exception stack trace when trying to run my main project, so I think this could be the cause:
就像我想的那样,这完全是关于你如何在 Android 主项目中引用你的 Android 库项目,一个简单的 Eclipse 配置设置.
Just like what I thought, it is all about how you reference your Android library project in the Android main project, a simple Eclipse configuration settings.
错误的方式:
右键单击主项目,选择Properties ->Java 构建路径 ->项目 ->添加...
,这个在Android主项目的构建路径中添加Android库项目作为依赖项目,这不起作用.如果在主项目中定义了所有需要的Android相关资源,编译时不会出错,但是运行应用程序时,会出现问题中描述的异常.
The Wrong Way:
Right click main project, choose Properties -> Java Build Path -> Projects -> Add...
, this add the Android library Project as a dependency project in Android main project's build path, this does not work. If all required Android-related resources are defined in main project, you will not get any error at compile time, but when run the application, you get the exception described in the question.
正确的方法:
右键单击主项目,选择Properties ->Android
,在库部分,在此处添加您的 Android 库项目.查看官方开发指南引用库项目.这应该可以解决您的所有问题.另请注意,您必须使用相对路径引用实际的 Android 库项目,如 图书馆项目 - 开发注意事项.
The Correct Way:
Right click main project, choose Properties -> Android
, in the Library section, add your Android library project here. Check out official dev guide Referencing a library project. This should fix all your problem. Also note that you have to use relative path reference the actual Android library project, as stated in the Library Project - Development considerations.
希望这会有所帮助.
这篇关于Android Activity ClassNotFoundException - 尝试了一切的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!