【问题标题】:Android application launch error with Unfortunately has stopped不幸的是,Android应用程序启动错误已停止
【发布时间】:2015-04-12 18:51:32
【问题描述】:

我在 Mac 上的 Eclipse 中创建了一个新的 Android 应用程序项目。我设置了Android 4.4.2 sdk。

当我在物理 Nexus 7 平板电脑中安装此示例项目时,它会引发以下启动错误。我还没有在这个项目中添加任何东西,它只是一个按原样创建的示例项目。

很遗憾已经停止

04-13 00:16:05.395: W/dalvikvm(6761): Unable to resolve superclass of Lcom/example/myfinaltest/MainActivity; (2)
04-13 00:16:05.395: W/dalvikvm(6761): Link of class 'Lcom/example/myfinaltest/MainActivity;' failed
04-13 00:16:05.395: D/AndroidRuntime(6761): Shutting down VM
04-13 00:16:05.395: W/dalvikvm(6761): threadid=1: thread exiting with uncaught exception (group=0x415bcba8)
04-13 00:16:05.395: E/AndroidRuntime(6761): FATAL EXCEPTION: main
04-13 00:16:05.395: E/AndroidRuntime(6761): Process: com.example.myfinaltest, PID: 6761
04-13 00:16:05.395: E/AndroidRuntime(6761): java.lang.RuntimeException: Unable to instantiate activity ComponentInfo{com.example.myfinaltest/com.example.myfinaltest.MainActivity}: java.lang.ClassNotFoundException: Didn't find class "com.example.myfinaltest.MainActivity" on path: DexPathList[[zip file "/data/app/com.example.myfinaltest-2.apk"],nativeLibraryDirectories=[/data/app-lib/com.example.myfinaltest-2, /vendor/lib, /system/lib]]
04-13 00:16:05.395: E/AndroidRuntime(6761):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2121)
04-13 00:16:05.395: E/AndroidRuntime(6761):     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2245)
04-13 00:16:05.395: E/AndroidRuntime(6761):     at android.app.ActivityThread.access$800(ActivityThread.java:135)
04-13 00:16:05.395: E/AndroidRuntime(6761):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1196)
04-13 00:16:05.395: E/AndroidRuntime(6761):     at android.os.Handler.dispatchMessage(Handler.java:102)
04-13 00:16:05.395: E/AndroidRuntime(6761):     at android.os.Looper.loop(Looper.java:136)
04-13 00:16:05.395: E/AndroidRuntime(6761):     at android.app.ActivityThread.main(ActivityThread.java:5017)
04-13 00:16:05.395: E/AndroidRuntime(6761):     at java.lang.reflect.Method.invokeNative(Native Method)
04-13 00:16:05.395: E/AndroidRuntime(6761):     at java.lang.reflect.Method.invoke(Method.java:515)
04-13 00:16:05.395: E/AndroidRuntime(6761):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:779)
04-13 00:16:05.395: E/AndroidRuntime(6761):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595)
04-13 00:16:05.395: E/AndroidRuntime(6761):     at dalvik.system.NativeStart.main(Native Method)
04-13 00:16:05.395: E/AndroidRuntime(6761): Caused by: java.lang.ClassNotFoundException: Didn't find class "com.example.myfinaltest.MainActivity" on path: DexPathList[[zip file "/data/app/com.example.myfinaltest-2.apk"],nativeLibraryDirectories=[/data/app-lib/com.example.myfinaltest-2, /vendor/lib, /system/lib]]
04-13 00:16:05.395: E/AndroidRuntime(6761):     at dalvik.system.BaseDexClassLoader.findClass(BaseDexClassLoader.java:56)
04-13 00:16:05.395: E/AndroidRuntime(6761):     at java.lang.ClassLoader.loadClass(ClassLoader.java:497)
04-13 00:16:05.395: E/AndroidRuntime(6761):     at java.lang.ClassLoader.loadClass(ClassLoader.java:457)
04-13 00:16:05.395: E/AndroidRuntime(6761):     at android.app.Instrumentation.newActivity(Instrumentation.java:1061)
04-13 00:16:05.395: E/AndroidRuntime(6761):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2112)
04-13 00:16:05.395: E/AndroidRuntime(6761):     ... 11 more

代码:

package com.example.myfinaltest;

import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;

public class MainActivity extends ActionBarActivity {

    @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;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        int id = item.getItemId();
        if (id == R.id.action_settings) {
            return true;
        }
        return super.onOptionsItemSelected(item);
    }
}

AndroidManifest.xml:

    <?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.example.myfinaltest"
    android:versionCode="1"
    android:versionName="1.0" >

    <uses-sdk
        android:minSdkVersion="8"
        android:targetSdkVersion="19" />

    <application
        android:allowBackup="true"
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        >
        <activity
            android:name=".MainActivity"
            android:label="@string/app_name" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
    </application>

</manifest>

对于,values/styles.xml 和 values-v11/styles.xml 和 values-v14/styles.xml

所有 3 个 xml 的值相同,但是我评论了 App 主题,因为我看到使用 Appcompat 库构建时出现错误。

    <resources>

    <!--
        Base application theme, dependent on API level. This theme is replaced
        by AppBaseTheme from res/values-vXX/styles.xml on newer devices.

    <style name="AppBaseTheme" parent="Theme.AppCompat.Light">
    </style>
-->

    <!-- Application theme. 
    <style name="AppTheme" parent="AppBaseTheme">
    </style>
-->
</resources>

【问题讨论】:

  • 发布AndroidManifest.xml。你的values/styles.xml 是什么样的?
  • 我更新了 AndroidManifest 和样式数据
  • 我知道为什么会这样。请看我的回答。

标签: android


【解决方案1】:

检查您的 AndroidManifest.xml 以查看您的活动是否定义如下。

<activity
    android:name="com.example.myfinaltest.MainActivity"
    android:label="@string/app_name" >
</activity>

在您的情况下,包名称可能不是“com.example.myfinaltest”。

【讨论】:

  • 不,它不工作。我已将数据粘贴到 AndroidManifest 中。
【解决方案2】:

由于您使用AppCompat 并扩展ActionBarActivity,您需要指定正确的主题。

AndroidManifest.xml:

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.example.myfinaltest"
    android:versionCode="1"
    android:versionName="1.0" >

    <uses-sdk
        android:minSdkVersion="8"
        android:targetSdkVersion="19" />

    <application
        android:allowBackup="true"
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme"> <!-- You need a theme -->
        <activity
            android:name=".MainActivity"
            android:label="@string/app_name" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
    </application>

</manifest>

values/styles.xml:

你可以全部删除除了values/styles.xml

<resources>

    <style name="AppBaseTheme" parent="Theme.AppCompat.Light">
    </style>

    <style name="AppTheme" parent="AppBaseTheme">
    </style>

</resources>

【讨论】:

  • 我在使用 Appcompat 时出现构建错误:[2015-04-13 01:48:04 - MyFinalTest] Workspace/MyFinalTest/res/values/styles.xml:3: error: Error retrieving parent for项目:找不到与给定名称“Theme.AppCompat.Light”匹配的资源。
  • 我看到您正在使用Eclipse,右键单击您的项目并确保将AppCompat 添加为构建资源。
  • 我这里有没有设​​置,右键属性->Java构建路径->库?我可以从哪里选择 appcompat?
  • AppCompat 在您机器上的“Android SDK”文件夹中。您为什么没有切换到 Android Studio?这一切都在 Android Studio 中为您完成。
  • 我使用的是 Mac 10.10 Yosemite,不支持 Android Studio。
猜你喜欢
  • 1970-01-01
  • 2017-01-12
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-11-28
  • 1970-01-01
相关资源
最近更新 更多