我在Eclipse Juno中使用了m2eclipse插件,JDK 1.7(基於Stack Overflow上的一些搜索從JRE 1.6切換而來)。在Eclipse中轉換爲Maven項目的結果ClassNotFoundException
新創建時,裸機Android應用程序運行正常。但是,當我將其轉換爲Maven項目時,每當我嘗試運行該應用程序時,我都會收到一個ClassNotFoundException異常。我嘗試過更新項目,重新啓動Eclipse,檢查構建路徑中的所有庫,清理並重建項目,重新啓動模擬器,並將我的pom.xml降至最低。
這個錯誤的原因是什麼?這是Maven的一些簡單的配置錯誤嗎?
我的logcat:
07-09 23:07:18.027: D/AndroidRuntime(958): Shutting down VM
07-09 23:07:18.027: W/dalvikvm(958): threadid=1: thread exiting with uncaught exception (group=0x409c01f8)
07-09 23:07:18.137: E/AndroidRuntime(958): FATAL EXCEPTION: main
07-09 23:07:18.137: E/AndroidRuntime(958): java.lang.RuntimeException: Unable to instantiate activity ComponentInfo{com.example.myproject/com.example.myproject.MainActivity}: java.lang.ClassNotFoundException: com.example.myproject.MainActivity
07-09 23:07:18.137: E/AndroidRuntime(958): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1880)
07-09 23:07:18.137: E/AndroidRuntime(958): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1981)
07-09 23:07:18.137: E/AndroidRuntime(958): at android.app.ActivityThread.access$600(ActivityThread.java:123)
07-09 23:07:18.137: E/AndroidRuntime(958): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1147)
07-09 23:07:18.137: E/AndroidRuntime(958): at android.os.Handler.dispatchMessage(Handler.java:99)
07-09 23:07:18.137: E/AndroidRuntime(958): at android.os.Looper.loop(Looper.java:137)
07-09 23:07:18.137: E/AndroidRuntime(958): at android.app.ActivityThread.main(ActivityThread.java:4424)
07-09 23:07:18.137: E/AndroidRuntime(958): at java.lang.reflect.Method.invokeNative(Native Method)
07-09 23:07:18.137: E/AndroidRuntime(958): at java.lang.reflect.Method.invoke(Method.java:511)
07-09 23:07:18.137: E/AndroidRuntime(958): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:784)
07-09 23:07:18.137: E/AndroidRuntime(958): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551)
07-09 23:07:18.137: E/AndroidRuntime(958): at dalvik.system.NativeStart.main(Native Method)
07-09 23:07:18.137: E/AndroidRuntime(958): Caused by: java.lang.ClassNotFoundException: com.example.myactivity.MainActivity
07-09 23:07:18.137: E/AndroidRuntime(958): at dalvik.system.BaseDexClassLoader.findClass(BaseDexClassLoader.java:61)
07-09 23:07:18.137: E/AndroidRuntime(958): at java.lang.ClassLoader.loadClass(ClassLoader.java:501)
07-09 23:07:18.137: E/AndroidRuntime(958): at java.lang.ClassLoader.loadClass(ClassLoader.java:461)
07-09 23:07:18.137: E/AndroidRuntime(958): at android.app.Instrumentation.newActivity(Instrumentation.java:1023)
07-09 23:07:18.137: E/AndroidRuntime(958): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1871)
07-09 23:07:18.137: E/AndroidRuntime(958): ... 11 more
我的pom.xml:
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.example</groupId>
<artifactId>myproject</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>jar</packaging>
<dependencies>
<dependency>
<groupId>com.google.android</groupId>
<artifactId>android</artifactId>
<version>4.1.1.4</version>
<scope>provided</scope>
</dependency>
</dependencies>
<build>
<sourceDirectory>src</sourceDirectory>
<plugins>
<plugin>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.0</version>
<configuration>
<source>1.7</source>
<target>1.7</target>
</configuration>
</plugin>
</plugins>
</build>
</project>
我MainActivity.java:
package com.example.myproject;
import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
public class MainActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
}
更新:我發現試圖在pom.xml上執行Maven Clean和Maven generate-sources成功。然而,Maven的安裝給我一個錯誤:
[ERROR] COMPILATION ERROR :
[INFO] -------------------------------------------------------------
[ERROR] MainActivity.java:[12,33] package R does not exist
[ERROR] MainActivity.java:[18,44] package R does not exist
我還添加了一些東西(依賴性和包裝)以及修改(1.6-> 1.7,3.1-> 3.0)在pom.xml文件這是最新的編輯。
JRE 1.6不足,但是,Oracle JDK 1.6是Android開發文檔正式推薦的版本,而不是JDK 1.7。無論如何,這可能不是導致這個特定錯誤的原因,儘管最終你最好切換回Oracle JDK 1.6。 –
這是有點尷尬的情況,因爲Oracle建議使用1.7,因爲他們不更新它們的1.6。 Kind of catch 22. – BLaZuRE
也許[本文](http://www.vogella.com/articles/AndroidBuildMaven/article.html)可以提供幫助。 –