When I try run my current android project on my phone, after successfully build and deploy it, I am getting this error:
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
My AndroidManifest.xml is that:
<?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>
the main activity is this:
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);
}
}
and the source code for this class and others are all placed in the directory src/main/java/org/hello from my project.
Anyone knows what is causing this error?
UPDATE 1
my 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>
I believe you did not properly set up the java source and resource folder, so maven does not find them in the places where they are supposed to. By default maven assumes the source folder is in
/src
, which would make compile your class tomain.java.org.hello.MainActivity
and this is why it cannot be found.You should add something like this to your maven build file:
see full answer: Changing the Maven structure ( src/java to src/Javasource)