Android java.lang.RuntimeException错误

时间:2013-12-11 15:36:12

标签: java android android-intent

我正在尝试为Unity构建我的Android插件。但是当我这样做时,我会在Logcat中获得以下错误日志:

12-11 14:45:38.745: E/AndroidRuntime(26806): FATAL EXCEPTION: main
12-11 14:45:38.745: E/AndroidRuntime(26806): java.lang.RuntimeException: Unable to instantiate activity ComponentInfo{com.example.marc/com.example.marc.CompassActivity}: java.lang.ClassNotFoundException: Didn't find class "com.example.marc.CompassActivity" on path: DexPathList[[zip file "/data/app/com.example.marc-1.apk"],nativeLibraryDirectories=[/data/app-lib/com.example.marc-1, /vendor/lib, /system/lib]]
12-11 14:45:38.745: E/AndroidRuntime(26806):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2137)
12-11 14:45:38.745: E/AndroidRuntime(26806):    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2261)
12-11 14:45:38.745: E/AndroidRuntime(26806):    at android.app.ActivityThread.access$600(ActivityThread.java:141)
12-11 14:45:38.745: E/AndroidRuntime(26806):    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1256)
12-11 14:45:38.745: E/AndroidRuntime(26806):    at android.os.Handler.dispatchMessage(Handler.java:99)
12-11 14:45:38.745: E/AndroidRuntime(26806):    at android.os.Looper.loop(Looper.java:137)
12-11 14:45:38.745: E/AndroidRuntime(26806):    at android.app.ActivityThread.main(ActivityThread.java:5103)
12-11 14:45:38.745: E/AndroidRuntime(26806):    at java.lang.reflect.Method.invokeNative(Native Method)
12-11 14:45:38.745: E/AndroidRuntime(26806):    at java.lang.reflect.Method.invoke(Method.java:525)
12-11 14:45:38.745: E/AndroidRuntime(26806):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:737)
12-11 14:45:38.745: E/AndroidRuntime(26806):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
12-11 14:45:38.745: E/AndroidRuntime(26806):    at dalvik.system.NativeStart.main(Native Method)
12-11 14:45:38.745: E/AndroidRuntime(26806): Caused by: java.lang.ClassNotFoundException: Didn't find class "com.example.marc.CompassActivity" on path: DexPathList[[zip file "/data/app/com.example.marc-1.apk"],nativeLibraryDirectories=[/data/app-lib/com.example.marc-1, /vendor/lib, /system/lib]]
12-11 14:45:38.745: E/AndroidRuntime(26806):    at dalvik.system.BaseDexClassLoader.findClass(BaseDexClassLoader.java:53)
12-11 14:45:38.745: E/AndroidRuntime(26806):    at java.lang.ClassLoader.loadClass(ClassLoader.java:501)
12-11 14:45:38.745: E/AndroidRuntime(26806):    at java.lang.ClassLoader.loadClass(ClassLoader.java:461)
12-11 14:45:38.745: E/AndroidRuntime(26806):    at android.app.Instrumentation.newActivity(Instrumentation.java:1061)
12-11 14:45:38.745: E/AndroidRuntime(26806):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2128)
12-11 14:45:38.745: E/AndroidRuntime(26806):    ... 11 more

我搜索了为什么会这样,我发现这是因为我的意图有问题。而且我需要将我的活动添加到我的Manifest文件中。但是,这是我的清单文件:

<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.marc"
android:versionCode="1"
android:versionName="1.0" >

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

<application
    android:allowBackup="true"
    android:icon="@drawable/ic_launcher"
    android:label="@string/app_name"
    android:theme="@style/AppTheme" >

    <activity android:name="com.example.marc.CompassActivity"
        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>

从我所看到的情况来看,我的意图是<activity android:name="com.example.marc.CompassActivity",除非我背后的想法是完全错误的?

为了完成,这是我的java文件:

  package com.example.marc;
import com.unity3d.player.UnityPlayerActivity;

import android.content.Context;
import android.hardware.Sensor;
import android.hardware.SensorEvent;
import android.hardware.SensorEventListener;
import android.hardware.SensorManager;
import android.os.Bundle;
import android.util.Config;
import android.util.Log;
import android.app.Activity;

public class CompassActivity extends UnityPlayerActivity 
{
    private static final String TAG = "Compass";

    private SensorManager mSensorManager;
    private Sensor mSensor;

    static public float xmag;
    static public float ymag;
    static public float zmag;

    private final SensorEventListener mListener = new SensorEventListener() 
    {
        public void onSensorChanged(SensorEvent event)
        {
        if (Config.DEBUG) Log.d(TAG,
        "sensorChanged (" + event.values[0] + ", " + event.values[1] + ", " + event.values[2] + ")");

            xmag = event.values[0];
            ymag = event.values[1];
            zmag = event.values[2];
        }

        public void onAccuracyChanged(Sensor sensor, int accuracy) 
        {
        }
    };

    @Override
    protected void onCreate(Bundle icicle)
    {
        super.onCreate(icicle);
        mSensorManager = (SensorManager)getSystemService(Context.SENSOR_SERVICE);
        mSensor = mSensorManager.getDefaultSensor(Sensor.TYPE_ORIENTATION);
    }

    @Override
    protected void onResume()
    {
        if (Config.DEBUG) Log.d(TAG, "onResume");
        super.onResume();

        mSensorManager.registerListener(mListener, mSensor,
        SensorManager.SENSOR_DELAY_GAME);
    }

    @Override
    protected void onStop()
    {
        if (Config.DEBUG) Log.d(TAG, "onStop");
        mSensorManager.unregisterListener(mListener);
        super.onStop();
    }

    public static float getX()
    {
        return xmag;
    }

    public static float getY() 
    {
        return ymag;
    }

    public static float getZ()
    {
        return zmag;
    }
}

同样,根据我的想法,这是我应该传递给我的Manifest文件,例如,它被称为“CompassActivity”。很明显,虽然我一定做错了,否则我不会得到错误。有人可以告诉我它是做错了什么/错过了吗?

修改

最新的LogCat文件:

   12-11 16:32:16.957: E/AndroidRuntime(29055): FATAL EXCEPTION: main
12-11 16:32:16.957: E/AndroidRuntime(29055): java.lang.RuntimeException: Unable to instantiate activity ComponentInfo{com.example.marc/com.example.marc.CompassActivity}: java.lang.ClassNotFoundException: Didn't find class "com.example.marc.CompassActivity" on path: DexPathList[[zip file "/data/app/com.example.marc-2.apk"],nativeLibraryDirectories=[/data/app-lib/com.example.marc-2, /vendor/lib, /system/lib]]
12-11 16:32:16.957: E/AndroidRuntime(29055):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2137)
12-11 16:32:16.957: E/AndroidRuntime(29055):    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2261)
12-11 16:32:16.957: E/AndroidRuntime(29055):    at android.app.ActivityThread.access$600(ActivityThread.java:141)
12-11 16:32:16.957: E/AndroidRuntime(29055):    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1256)
12-11 16:32:16.957: E/AndroidRuntime(29055):    at android.os.Handler.dispatchMessage(Handler.java:99)
12-11 16:32:16.957: E/AndroidRuntime(29055):    at android.os.Looper.loop(Looper.java:137)
12-11 16:32:16.957: E/AndroidRuntime(29055):    at android.app.ActivityThread.main(ActivityThread.java:5103)
12-11 16:32:16.957: E/AndroidRuntime(29055):    at java.lang.reflect.Method.invokeNative(Native Method)
12-11 16:32:16.957: E/AndroidRuntime(29055):    at java.lang.reflect.Method.invoke(Method.java:525)
12-11 16:32:16.957: E/AndroidRuntime(29055):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:737)
12-11 16:32:16.957: E/AndroidRuntime(29055):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
12-11 16:32:16.957: E/AndroidRuntime(29055):    at dalvik.system.NativeStart.main(Native Method)
12-11 16:32:16.957: E/AndroidRuntime(29055): Caused by: java.lang.ClassNotFoundException: Didn't find class "com.example.marc.CompassActivity" on path: DexPathList[[zip file "/data/app/com.example.marc-2.apk"],nativeLibraryDirectories=[/data/app-lib/com.example.marc-2, /vendor/lib, /system/lib]]
12-11 16:32:16.957: E/AndroidRuntime(29055):    at dalvik.system.BaseDexClassLoader.findClass(BaseDexClassLoader.java:53)
12-11 16:32:16.957: E/AndroidRuntime(29055):    at java.lang.ClassLoader.loadClass(ClassLoader.java:501)
12-11 16:32:16.957: E/AndroidRuntime(29055):    at java.lang.ClassLoader.loadClass(ClassLoader.java:461)
12-11 16:32:16.957: E/AndroidRuntime(29055):    at android.app.Instrumentation.newActivity(Instrumentation.java:1061)
12-11 16:32:16.957: E/AndroidRuntime(29055):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2128)
12-11 16:32:16.957: E/AndroidRuntime(29055):    ... 11 more

3 个答案:

答案 0 :(得分:1)

我认为

 <activity android:name="com.example.marc.CompassActivity"

应该

   <activity android:name="CompassActivity"

(你已经在包属性中提到了“com.example.marc” android:name的值应该只是activity类的名称。 由于您的清单文件说主要活动类是“com.example.marc.CompassActivity”,运行时环境会查找它,但它找不到它,因为您的主要活动类文件被命名为“CompassAcitvity”)

[编辑]

主要问题是运行时无法实例化MainActivity,因为它无法在DexPathList中找到“com.example.marc.CompassActivity”类

参见方法"findClass()" line 310-323

如果成功找到一个类,则返回另一个类对象 null 它将返回到BaseDexClassLoader(see line 58)如果收到null,则会抛出ClassNotFoundException,在这种情况下会发生。

我认为主要活动类在构建过程中被排除,或者它有一些不同的名称,与清单中提到的不同。

答案 1 :(得分:0)

实际上找不到您的UnityPlayerActivity课程,但未找到编译器显示CompassActivity

如果你在图书馆中有UnityPlayerActivity并且链接到你的应用程序,那么请仔细检查你的项目 - >右键单击 - &gt;属性 - &gt; Java构建路径---&gt;订单和导出有你的项目检查。

答案 2 :(得分:0)

关注@ TNR的答案 - 实际上找不到您的UnityPlayerActivity课程,但LogCat显示未找到CompassActivity

您可以尝试做的是:

  1. 使用清单中的原始UnityPlayerActivity重建项目(仅用于测试)。它有效吗? (如果没有,那么先解决这个问题)

  2. 使用JD对CompassActivity所在的JAR进行反编译。它是否继承自UnityPlayerActivity?在我的情况下,编译器出现问题,它将继承更改为未找到的NPUnityPlayerActivity。如果出现此问题,您需要重新编译代码并确保继承是正确的。

  3. 祝你好运!