Android设备无法实例化启动器活动

时间:2014-11-13 22:26:28

标签: android xml android-activity launcher

当我尝试在手机上运行当前的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>

1 个答案:

答案 0 :(得分:0)

我相信你没有正确设置java源代码和资源文件夹,因此maven在它们应该的位置找不到它们。默认情况下,maven假定源文件夹位于/src,这会使您的类编译为main.java.org.hello.MainActivity,这就是无法找到它的原因。

你应该在你的maven构建文件中添加这样的东西:

<build>
    <sourceDirectory>src/main/java</sourceDirectory>
    <resources>
      <resource>
        <directory>src/main/res</directory>
      </resource>
    </resources>
</build>

请参阅完整答案:Changing the Maven structure ( src/java to src/Javasource)