运行 示例代码:致命异常:main...无法实例化 activity

Running sample code: FATAL EXCEPTION: main...Unable to instantiate activity

我正在尝试 运行 AndEngine 提供的示例代码(最简单的示例代码如下所示,来自 here). I added Andengine.jar file library received from here

一切都很好,但不幸的是,当我想要 运行 样本时,它抛出 FATAL EXCEPTION: main Unable to instantiate activity 错误。我在 AndroidManifest.xml 文件中注册了活动,但仍然出现错误。任何提示如何解决它?

06-02 15:44:48.699: E/AndroidRuntime(19437): FATAL EXCEPTION: main
06-02 15:44:48.699: E/AndroidRuntime(19437): Process: com.example.t, PID: 19437
06-02 15:44:48.699: E/AndroidRuntime(19437): java.lang.RuntimeException: Unable to instantiate activity ComponentInfo{com.example.t/com.example.t.MainActivity}: java.lang.ClassNotFoundException: Didn't find class "com.example.t.MainActivity" on path: DexPathList[[zip file "/data/app/com.example.t-1/base.apk"],nativeLibraryDirectories=[/vendor/lib, /system/lib]]
06-02 15:44:48.699: E/AndroidRuntime(19437):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2236)
06-02 15:44:48.699: E/AndroidRuntime(19437):    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2387)
06-02 15:44:48.699: E/AndroidRuntime(19437):    at android.app.ActivityThread.access0(ActivityThread.java:151)
06-02 15:44:48.699: E/AndroidRuntime(19437):    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1303)
06-02 15:44:48.699: E/AndroidRuntime(19437):    at android.os.Handler.dispatchMessage(Handler.java:102)
06-02 15:44:48.699: E/AndroidRuntime(19437):    at android.os.Looper.loop(Looper.java:135)
06-02 15:44:48.699: E/AndroidRuntime(19437):    at android.app.ActivityThread.main(ActivityThread.java:5254)
06-02 15:44:48.699: E/AndroidRuntime(19437):    at java.lang.reflect.Method.invoke(Native Method)
06-02 15:44:48.699: E/AndroidRuntime(19437):    at java.lang.reflect.Method.invoke(Method.java:372)
06-02 15:44:48.699: E/AndroidRuntime(19437):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:903)
06-02 15:44:48.699: E/AndroidRuntime(19437):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:698)
06-02 15:44:48.699: E/AndroidRuntime(19437): Caused by: java.lang.ClassNotFoundException: Didn't find class "com.example.t.MainActivity" on path: DexPathList[[zip file "/data/app/com.example.t-1/base.apk"],nativeLibraryDirectories=[/vendor/lib, /system/lib]]
06-02 15:44:48.699: E/AndroidRuntime(19437):    at dalvik.system.BaseDexClassLoader.findClass(BaseDexClassLoader.java:56)
06-02 15:44:48.699: E/AndroidRuntime(19437):    at java.lang.ClassLoader.loadClass(ClassLoader.java:511)
06-02 15:44:48.699: E/AndroidRuntime(19437):    at java.lang.ClassLoader.loadClass(ClassLoader.java:469)
06-02 15:44:48.699: E/AndroidRuntime(19437):    at android.app.Instrumentation.newActivity(Instrumentation.java:1066)
06-02 15:44:48.699: E/AndroidRuntime(19437):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2226)
06-02 15:44:48.699: E/AndroidRuntime(19437):    ... 10 more
06-02 15:44:48.699: E/AndroidRuntime(19437):    Suppressed: java.lang.NoClassDefFoundError: com.example.t.MainActivity
06-02 15:44:48.699: E/AndroidRuntime(19437):        at dalvik.system.DexFile.defineClassNative(Native Method)
06-02 15:44:48.699: E/AndroidRuntime(19437):        at dalvik.system.DexFile.defineClass(DexFile.java:226)
06-02 15:44:48.699: E/AndroidRuntime(19437):        at dalvik.system.DexFile.loadClassBinaryName(DexFile.java:219)
06-02 15:44:48.699: E/AndroidRuntime(19437):        at dalvik.system.DexPathList.findClass(DexPathList.java:321)
06-02 15:44:48.699: E/AndroidRuntime(19437):        at dalvik.system.BaseDexClassLoader.findClass(BaseDexClassLoader.java:54)
06-02 15:44:48.699: E/AndroidRuntime(19437):        ... 14 more
06-02 15:44:48.699: E/AndroidRuntime(19437):    Suppressed: java.lang.ClassNotFoundException: com.example.t.MainActivity
06-02 15:44:48.699: E/AndroidRuntime(19437):        at java.lang.Class.classForName(Native Method)
06-02 15:44:48.699: E/AndroidRuntime(19437):        at java.lang.BootClassLoader.findClass(ClassLoader.java:781)
06-02 15:44:48.699: E/AndroidRuntime(19437):        at java.lang.BootClassLoader.loadClass(ClassLoader.java:841)
06-02 15:44:48.699: E/AndroidRuntime(19437):        at java.lang.ClassLoader.loadClass(ClassLoader.java:504)
06-02 15:44:48.699: E/AndroidRuntime(19437):        ... 13 more
06-02 15:44:48.699: E/AndroidRuntime(19437):    Caused by: java.lang.NoClassDefFoundError: Class not found using the boot class loader; no stack available

源代码:

package com.example.t;

import org.anddev.andengine.ui.activity.BaseGameActivity;

import android.view.Menu;
import android.view.MenuItem;

/**
 * (c) 2010 Nicolas Gramlich 
 * (c) 2011 Zynga Inc.
 * 
 * @author Nicolas Gramlich
 * @since 22:10:28 - 11.04.2010
 */
public abstract class MainActivity extends BaseGameActivity {
    // ===========================================================
    // Constants
    // ===========================================================

    private static final int MENU_TRACE = Menu.FIRST;

    // ===========================================================
    // Fields
    // ===========================================================

    // ===========================================================
    // Constructors
    // ===========================================================

    // ===========================================================
    // Getter & Setter
    // ===========================================================

    // ===========================================================
    // Methods for/from SuperClass/Interfaces
    // ===========================================================

    @Override
    public boolean onCreateOptionsMenu(final Menu pMenu) {
        pMenu.add(Menu.NONE, MENU_TRACE, Menu.NONE, "Start Method Tracing");
        return super.onCreateOptionsMenu(pMenu);
    }

    @Override
    public boolean onPrepareOptionsMenu(final Menu pMenu) {
        pMenu.findItem(MENU_TRACE).setTitle(this.mEngine.isMethodTracing() ? "Stop Method Tracing" : "Start Method Tracing");
        return super.onPrepareOptionsMenu(pMenu);
    }

    @Override
    public boolean onMenuItemSelected(final int pFeatureId, final MenuItem pItem) {
        switch(pItem.getItemId()) {
            case MENU_TRACE:
                if(this.mEngine.isMethodTracing()) {
                    this.mEngine.stopMethodTracing();
                } else {
                    this.mEngine.startMethodTracing("AndEngine_" + System.currentTimeMillis() + ".trace");
                }
                return true;
            default:
                return super.onMenuItemSelected(pFeatureId, pItem);
        }
    }

    // ===========================================================
    // Methods
    // ===========================================================

    // ===========================================================
    // Inner and Anonymous Classes
    // ===========================================================
}

和清单文件:

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

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

    <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/app_name" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

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

</manifest>

您的 activity 不能是摘要 class。尝试

public class MainActivity extends BaseGameActivity {
    ...
}

我有同样的问题,因为你添加了一个新库Andengine.jar试试这个:

在您的项目中转到 Properties -> Java Build Path -> Order and Export,取消选中选项 Android Dependencies,执行 clean & 构建项目。

这在以下内容中有描述: Android ClassNotFoundException: Didn't find class on path