由于ClassNotFoundException而无法启动活动

我正在尝试使用代码启动一个新活动:

Intent i = new Intent(this, TempLobbyActivity.class); startActivity(i); finish(); 

当我启动此代码时,我在执行Intent构造函数时遇到以下exception。

 FATAL EXCEPTION: main Process: com.edr.iveflownthat, PID: 1457 java.lang.NoClassDefFoundError: Failed resolution of: Lcom/edr/iveflownthat/TempLobbyActivity; at com.edr.iveflownthat.pages.MainActivity.tryAutoLogin(MainActivity.java:46) at com.edr.iveflownthat.pages.MainActivity.onCreate(MainActivity.java:22) at android.app.Activity.performCreate(Activity.java:5933) at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1105) at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2251) at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2360) at android.app.ActivityThread.access$800(ActivityThread.java:144) at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1278) at android.os.Handler.dispatchMessage(Handler.java:102) at android.os.Looper.loop(Looper.java:135) at android.app.ActivityThread.main(ActivityThread.java:5221) at java.lang.reflect.Method.invoke(Native Method) at java.lang.reflect.Method.invoke(Method.java:372) at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:899) at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:694) Caused by: java.lang.ClassNotFoundException: Didn't find class "com.edr.iveflownthat.TempLobbyActivity" on path: DexPathList[[zip file "/data/app/com.edr.iveflownthat-1/base.apk"],nativeLibraryDirectories=[/vendor/lib, /system/lib]] FATAL EXCEPTION: main Process: com.edr.iveflownthat, PID: 1457 java.lang.NoClassDefFoundError: Failed resolution of: Lcom/edr/iveflownthat/TempLobbyActivity; at com.edr.iveflownthat.pages.MainActivity.tryAutoLogin(MainActivity.java:46) at com.edr.iveflownthat.pages.MainActivity.onCreate(MainActivity.java:22) at android.app.Activity.performCreate(Activity.java:5933) at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1105) at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2251) at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2360) at android.app.ActivityThread.access$800(ActivityThread.java:144) at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1278) at android.os.Handler.dispatchMessage(Handler.java:102) at android.os.Looper.loop(Looper.java:135) at android.app.ActivityThread.main(ActivityThread.java:5221) at java.lang.reflect.Method.invoke(Native Method) at java.lang.reflect.Method.invoke(Method.java:372) at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:899) at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:694) Caused by: java.lang.ClassNotFoundException: Didn't find class "com.edr.iveflownthat.TempLobbyActivity" on path: DexPathList[[zip file "/data/app/com.edr.iveflownthat-1/base.apk"],nativeLibraryDirectories=[/vendor/lib, /system/lib]] at dalvik.system.BaseDexClassLoader.findClass(BaseDexClassLoader.java:56) at java.lang.ClassLoader.loadClass(ClassLoader.java:511) at java.lang.ClassLoader.loadClass(ClassLoader.java:469) ... 15 more Suppressed: java.lang.NoClassDefFoundError: com.edr.iveflownthat.TempLobbyActivity at dalvik.system.DexFile.defineClassNative(Native Method) at dalvik.system.DexFile.defineClass(DexFile.java:222) at dalvik.system.DexFile.loadClassBinaryName(DexFile.java:215) at dalvik.system.DexPathList.findClass(DexPathList.java:321) at dalvik.system.BaseDexClassLoader.findClass(BaseDexClassLoader.java:54) ... 17 more Suppressed: java.lang.ClassNotFoundException: com.edr.iveflownthat.TempLobbyActivity at java.lang.Class.classForName(Native Method) at java.lang.BootClassLoader.findClass(ClassLoader.java:781) at java.lang.BootClassLoader.loadClass(ClassLoader.java:841) at java.lang.ClassLoader.loadClass(ClassLoader.java:504) ... 16 more Caused by: java.lang.NoClassDefFoundError: Class not found using the boot class loader; no stack available at dalvik.system.BaseDexClassLoader.findClass(BaseDexClassLoader.java:56) at java.lang.ClassLoader.loadClass(ClassLoader.java:511) at java.lang.ClassLoader.loadClass(ClassLoader.java:469) ... 15 more Suppressed: java.lang.NoClassDefFoundError: com.edr.iveflownthat.TempLobbyActivity at dalvik.system.DexFile.defineClassNative(Native Method) at dalvik.system.DexFile.defineClass(DexFile.java:222) at dalvik.system.DexFile.loadClassBinaryName(DexFile.java:215) at dalvik.system.DexPathList.findClass(DexPathList.java:321) at dalvik.system.BaseDexClassLoader.findClass(BaseDexClassLoader.java:54) ... 17 more Suppressed: java.lang.ClassNotFoundException: com.edr.iveflownthat.TempLobbyActivity at java.lang.Class.classForName(Native Method) at java.lang.BootClassLoader.findClass(ClassLoader.java:781) at java.lang.BootClassLoader.loadClass(ClassLoader.java:841) at java.lang.ClassLoader.loadClass(ClassLoader.java:504) ... 16 more Caused by: java.lang.NoClassDefFoundError: Class not found using the boot class loader; no stack available 

我确保TempLobbyActivity类确实存在并且是一个适当的活动。 我没有其他错误。

这是TempLobbyActivity的开始:

 package com.edr.iveflownthat; import android.app.Activity; import android.os.Bundle; import android.support.v4.app.Fragment; import android.support.v4.app.FragmentActivity; import android.support.v4.app.FragmentManager; import android.support.v4.widget.DrawerLayout; import android.support.v7.app.ActionBarDrawerToggle; import android.view.View; import android.widget.AdapterView; import android.widget.AdapterView.OnItemClickListener; import android.widget.ArrayAdapter; import android.widget.ListView; import com.edr.iveflownthat.pages.fragments.LobbyLeaderboardFragment; import com.edr.iveflownthat.pages.fragments.LobbyMessagerFragment; import com.edr.iveflownthat.pages.fragments.LobbyMyPlanesFragment; public class TempLobbyActivity extends FragmentActivity { 

这是我的清单文件:

                                                        <!--    -->     

即使在AndroidManifest.xml中定义了类,我也得到了同样的错误。

原因 :有时会发生这种情况,因为您添加了多个版本的appcompat库。

解决方案 :删除任何以前版本的appcompat lib并将appcompat_v7.jar添加到/ lib文件夹。

清理您的项目并重新构建。 您可能不会再次收到错误。

在AndroidManifest.xml中注册TempLobbyActivity

  

所以你的清单文件应该是这样的

        

自11月21日以来,API级别10(Android 2.3.3 – 2.3.7)存在一个错误(可能在Play服务库中):

请参阅此错误报告: https //code.google.com/p/android/issues/detail?id = 81083

对于那些可能仍然面临这个问题的人:

向build.gradle添加适当的依赖项,如果没有正确添加依赖项并且gradle没有编译添加到项目中的库,则会发生此类错误,因此您不会在运行时访问这些库类。

我发现我在控制台中出现了一个错误:

[ExoPlayerLib]找不到ExoPlayerLib.apk。

用SO答案解决这个问题 ,NoClassDefFoundError的二次错误也得到了解决。

看起来,在项目中构建和包含库的任何问题都可能导致此问题。