我在 Eclipse Juno 中使用 m2eclipse 插件和 JDK 1.7(根据 Stack Overflow 上的一些搜索从 JRE 1.6 切换)。
新创建的准系统 Android 应用程序运行良好。但是,当我将其转换为 Maven 项目时,每当我尝试运行应用程序时,我都会开始收到 ClassNotFoundException。我尝试过更新项目,重新启动 Eclipse,检查构建路径中的所有库,清理和重建项目,重新启动模拟器,并将我的 pom.xml 减少到最低限度。
这个错误的原因是什么?是 Maven 的一些简单的错误配置吗?
我的日志猫:
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 install 给了我一个错误:
[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 文件。