问题
I trying to use AndroidAnnotaions, but getting ClassNotFoundException
error in my project.
For testing purposes, I created a new project with one Activity
and I still getting the error.
My files:
AndroidManifest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.rodrigora.testannotations"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="16" />
<application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity
android:name="com.rodrigora.testannotations.MainActivity_"
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>
MainActivity.java
package com.rodrigora.testannotations;
import android.app.Activity;
import android.view.Menu;
import com.googlecode.androidannotations.annotations.EActivity;
@EActivity(R.layout.activity_main)
public class MainActivity extends Activity {
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.activity_main, menu);
return true;
}
}
These are the two files I changed from sample project created by Eclipse and followed these steps to add AndroidAnnotations to project:
- Added
androidannotations-api-2.7.jar
tolibs
folder and tobuild path
- Added
androidannotations-2.7.jar
tocompiled-libs
folder and toFactory Path
- Marked
androidannotations-2.7.jar
inOrder and Export
tab.
Here the stack trace when I launch the app:
12-28 20:29:21.296: E/AndroidRuntime(30600): FATAL EXCEPTION: main
12-28 20:29:21.296: E/AndroidRuntime(30600): java.lang.RuntimeException: Unable to instantiate activity ComponentInfo{com.rodrigora.testannotations/com.rodrigora.testannotations.MainActivity_}: java.lang.ClassNotFoundException: com.rodrigora.testannotations.MainActivity_ in loader dalvik.system.PathClassLoader[/data/app/com.rodrigora.testannotations-2.apk]
12-28 20:29:21.296: E/AndroidRuntime(30600): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1573)
12-28 20:29:21.296: E/AndroidRuntime(30600): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1667)
12-28 20:29:21.296: E/AndroidRuntime(30600): at android.app.ActivityThread.access$1500(ActivityThread.java:117)
12-28 20:29:21.296: E/AndroidRuntime(30600): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:935)
12-28 20:29:21.296: E/AndroidRuntime(30600): at android.os.Handler.dispatchMessage(Handler.java:99)
12-28 20:29:21.296: E/AndroidRuntime(30600): at android.os.Looper.loop(Looper.java:130)
12-28 20:29:21.296: E/AndroidRuntime(30600): at android.app.ActivityThread.main(ActivityThread.java:3687)
12-28 20:29:21.296: E/AndroidRuntime(30600): at java.lang.reflect.Method.invokeNative(Native Method)
12-28 20:29:21.296: E/AndroidRuntime(30600): at java.lang.reflect.Method.invoke(Method.java:507)
12-28 20:29:21.296: E/AndroidRuntime(30600): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:867)
12-28 20:29:21.296: E/AndroidRuntime(30600): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:625)
12-28 20:29:21.296: E/AndroidRuntime(30600): at dalvik.system.NativeStart.main(Native Method)
12-28 20:29:21.296: E/AndroidRuntime(30600): Caused by: java.lang.ClassNotFoundException: com.rodrigora.testannotations.MainActivity_ in loader dalvik.system.PathClassLoader[/data/app/com.rodrigora.testannotations-2.apk]
12-28 20:29:21.296: E/AndroidRuntime(30600): at dalvik.system.PathClassLoader.findClass(PathClassLoader.java:240)
12-28 20:29:21.296: E/AndroidRuntime(30600): at java.lang.ClassLoader.loadClass(ClassLoader.java:551)
12-28 20:29:21.296: E/AndroidRuntime(30600): at java.lang.ClassLoader.loadClass(ClassLoader.java:511)
12-28 20:29:21.296: E/AndroidRuntime(30600): at android.app.Instrumentation.newActivity(Instrumentation.java:1021)
12-28 20:29:21.296: E/AndroidRuntime(30600): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1565)
12-28 20:29:21.296: E/AndroidRuntime(30600): ... 11 more
Anyone knows what I forgot?
Eclipse should show generated classes by AndroidAnnotations
?
Thank you!
EDIT
Full code - bitbucket
回答1:
It looks like your AndroindAnnotation generated classes are not getting included in your build. Here are the lines your .classpath seems to be missing --
<classpathentry kind="src" path=".apt_generated">
<attributes>
<attribute name="optional" value="true"/>
</attributes>
</classpathentry>
These should have been generated for you as part of following the AndroidAnnotations install. Things to check --
Your ADT tools are up to date.
You ran
Project->Clean
after enabling annotations
I was able to clone your repo, import into eclipse, and get a clean build and run the app without a crash using the following steps --
- Refactor/rename project to TestAnnotations (eclipse imported as "MainActivity_")
- Enable annotation processing (needed to turn on
Enable project specific settings
andEnable annonation processing
in `Java Compiler->Annontation Processing) - Project->Clean
- Run it
Note: If I did an import and just refactored to get a clean build, but did not enable annotation processing, I got the exact same behavior you describe. Check to make sure you have project specific settings on, annotations on, and then do a clean build.
回答2:
With maven and eclipse updating project configuration causes android annotation output folder get changed. To fix it go to java build path>Source and set the android annotation folder's output to default output folder.
来源:https://stackoverflow.com/questions/14076694/classnotfoundexception-using-androidannotations