【发布时间】:2014-11-13 22:26:28
【问题描述】:
当我尝试在我的手机上运行我当前的 android 项目时,在成功构建和部署它后,我收到了这个错误:
E/AndroidRuntime(15869): FATAL EXCEPTION: main
E/AndroidRuntime(15869): Process: org.hello, PID: 15869
E/AndroidRuntime(15869): java.lang.RuntimeException: Unable to instantiate activity ComponentInfo{org.hello/org.hello.MainActivity}: java.lang.ClassNotFoundException: Didn't find class "org.hello.MainActivity" on path: DexPathList[[zip file "/data/app/org.hello-1.apk"],nativeLibraryDirectories=[/data/app-lib/org.hello-1, /vendor/lib, /system/lib]]
E/AndroidRuntime(15869): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2124)
E/AndroidRuntime(15869): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2257)
E/AndroidRuntime(15869): at android.app.ActivityThread.access$800(ActivityThread.java:139)
E/AndroidRuntime(15869): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1210)
E/AndroidRuntime(15869): at android.os.Handler.dispatchMessage(Handler.java:102)
E/AndroidRuntime(15869): at android.os.Looper.loop(Looper.java:136)
E/AndroidRuntime(15869): at android.app.ActivityThread.main(ActivityThread.java:5086)
E/AndroidRuntime(15869): at java.lang.reflect.Method.invokeNative(Native Method)
E/AndroidRuntime(15869): at java.lang.reflect.Method.invoke(Method.java:515)
E/AndroidRuntime(15869): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:785)
E/AndroidRuntime(15869): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:601)
E/AndroidRuntime(15869): at dalvik.system.NativeStart.main(Native Method)
E/AndroidRuntime(15869): Caused by: java.lang.ClassNotFoundException: Didn't find class "org.hello.MainActivity" on path: DexPathList[[zip file "/data/app/org.hello-1.apk"],nativeLibraryDirectories=[/data/app-lib/org.hello-1, /vendor/lib, /system/lib]]
E/AndroidRuntime(15869): at dalvik.system.BaseDexClassLoader.findClass(BaseDexClassLoader.java:56)
E/AndroidRuntime(15869): at java.lang.ClassLoader.loadClass(ClassLoader.java:497)
E/AndroidRuntime(15869): at java.lang.ClassLoader.loadClass(ClassLoader.java:457)
E/AndroidRuntime(15869): at android.app.Instrumentation.newActivity(Instrumentation.java:1084)
E/AndroidRuntime(15869): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2115)
E/AndroidRuntime(15869): ... 11 more
W/ActivityManager( 916): Force finishing activity org.hello/.MainActivity
我的 AndroidManifest.xml 是这样的:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="org.hello"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="11"
android:targetSdkVersion="21" />
<application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity
android:name=".MainActivity"
android:label="@string/title_activity_main" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
主要活动是这样的:
public class MainActivity extends FragmentActivity {
// Fragment TabHost as mTabHost
private FragmentTabHost mTabHost;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mTabHost = (FragmentTabHost)findViewById(android.R.id.tabhost);
mTabHost.setup(this, getSupportFragmentManager(), R.id.realtabcontent);
mTabHost.addTab(mTabHost.newTabSpec("tab1").setIndicator("Tab1"), Tab1Fragment.class, null);
mTabHost.addTab(mTabHost.newTabSpec("tab2").setIndicator("Tab2"), Tab2Fragment.class, null);
mTabHost.addTab(mTabHost.newTabSpec("tab3").setIndicator("Tab3"), Tab3Fragment.class, null);
}
}
这个类和其他类的源代码都放在我项目的src/main/java/org/hello目录下。
有谁知道是什么导致了这个错误?
更新 1
我的 pom.xml:
<?xml version="1.0" encoding="UTF-8"?>
<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/maven-v4_0_0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>org.hello</groupId>
<artifactId>basic-tabs</artifactId>
<version>0.1.0</version>
<packaging>apk</packaging>
<properties>
<!-- use UTF-8 for everything -->
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
<android.sdk.path>/home/kleber/android-sdk-linux/</android.sdk.path>
</properties>
<dependencies>
<dependency>
<groupId>com.google.android</groupId>
<artifactId>android</artifactId>
<version>4.1.1.4</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>com.google.android</groupId>
<artifactId>support-v4</artifactId>
<version>20.0.0</version>
<scope>system</scope>
<systemPath>/home/kleber/android-sdk-linux/extras/android/support/v4/android-support-v4.jar</systemPath>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>com.jayway.maven.plugins.android.generation2</groupId>
<artifactId>android-maven-plugin</artifactId>
<version>3.9.0-rc.1</version>
<configuration>
<sdk>
<platform>19</platform>
</sdk>
<deleteConflictingFiles>true</deleteConflictingFiles>
<undeployBeforeDeploy>true</undeployBeforeDeploy>
</configuration>
<extensions>true</extensions>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.1</version>
<configuration>
<source>1.7</source>
<target>1.7</target>
<showDeprecation>true</showDeprecation>
<showWarnings>true</showWarnings>
<executable>/home/kleber/jdk1.7.0_55/bin/javac</executable>
<fork>true</fork>
</configuration>
</plugin>
</plugins>
</build>
</project>
【问题讨论】:
-
尝试清理项目。
-
@Hegazy 我试试这个,但没有解决问题。
-
在您的
AndroidManifest中,尝试将活动从android:name=".MainActivity"替换为android:name="org.hello.MainActivity" -
如果存储在
src/main/java/org/hello,包应该是main.java.org.hello而不是org.hello? -
@ShobhitPuri 这是一个 Maven 项目。在类中,包被声明为 org.hello。 src/main/java 是项目结构的一部分。
标签: android xml android-activity launcher