gpt4 book ai didi

java - Android java.lang.RuntimeException 错误

转载 作者:行者123 更新时间:2023-12-01 13:43:18 25 4
gpt4 key购买 nike

我正在尝试为 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

我搜索了为什么会发生这种情况,发现这是因为我的 Intent 有问题。我需要将我的 Activity 添加到我的 list 文件中。但是,这是我的 list 文件:

<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>

据我所知,我的 Intent 就在 <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;
}
}

同样,根据我的想法,这就是我应该传递给我的 list 文件的内容,例如,它被称为“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

最佳答案

我认为

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

应该是

   <activity android:name="CompassActivity"

(您已经在包属性中提到了“com.example.marc”android:name 的值只能是 Activity 类的名称。由于您的 list 文件显示主 Activity 类是“com.example.marc.CompassActivity”,运行时环境会查找它,但找不到它,因为您的主 Activity 类文件被命名为“CompassAcitvity”)

[编辑]

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

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

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

我认为主要 Activity 类在构建过程中被排除,或者它具有与 list 中提到的不同的名称。

关于java - Android java.lang.RuntimeException 错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20523306/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com