运行示例代码:FATAL EXCEPTION:main ...无法实例化活动

时间:2015-06-02 23:04:53

标签: android andengine

我正在尝试运行AndEngine提供的示例代码(如下所示,最简单的示例代码,来自here)。我添加了从here收到的Andengine.jar文件库。

一切都很好,但不幸的是,当我想运行示例时,它会抛出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.access$800(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>

2 个答案:

答案 0 :(得分:2)

您的活动不能成为抽象类。尝试

public class MainActivity extends BaseGameActivity {
    ...
}

答案 1 :(得分:1)

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

在您的项目中

转到Properties - &gt; Java Build Path - &gt; Order and Export,取消选中Android Dependencies选项,执行clean&amp;建立项目。

这描述如下: Android ClassNotFoundException: Didn't find class on path