Java 由于 ClassNotFoundException 无法启动活动
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/26702179/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Failing to launch activity due to ClassNotFoundException
提问by Eric Siegal
I am trying to launch a new activity using the code:
我正在尝试使用以下代码启动一个新活动:
Intent i = new Intent(this, TempLobbyActivity.class);
startActivity(i);
finish();
When I launch this code I get the following exceptions when executing the Intent constructor.
当我启动此代码时,在执行 Intent 构造函数时出现以下异常。
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.access0(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.access0(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
I have made sure that the class TempLobbyActivity does exist and is a proper activity. I get no other errors.
我已经确保类 TempLobbyActivity 确实存在并且是一个适当的活动。我没有其他错误。
Here is the begining of 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 {
And here is my manifest file:
这是我的清单文件:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.edr.iveflownthat"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="14"
android:targetSdkVersion="19" />
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.GET_ACCOUNTS" />
<uses-permission android:name="android.permission.USE_CREDENTIALS" />
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
<application
android:allowBackup="true"
android:icon="@drawable/app_logo"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<meta-data
android:name="com.google.android.gms.version"
android:value="@integer/google_play_services_version" />
<activity
android:name=".pages.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>
<activity
android:name=".StartActivity"
android:label="@string/title_activity_start" >
</activity>
<activity
android:name=".pages.AccountLoginActivity"
android:label="@string/title_activity_login" >
</activity>
<activity
android:name=".pages.MyPlanesActivity"
android:label="@string/title_activity_my_planes" >
</activity>
<activity
android:name="com.edr.icom.edr.iveflownthat.activities.AccountNewActivity"
android:label="@string/title_activity_new_account" >
</activity>
<activity
android:name=".pages.SearchPlanesActivity"
android:label="@string/title_activity_search"
android:parentActivityName=".pages.LobbyActivity" >
<meta-data
android:name="android.support.PARENT_ACTIVITY"
android:value="com.edr.iveflownthat.pages.LobbyActivity" />
</activity>
<activity
android:name=".pages.LeaderBoardActivity"
android:label="@string/title_activity_leader_board" >
</activity>
<activity
android:name=".pages.PlaneBioActivity"
android:label="@string/title_activity_plane_info" >
</activity>
<activity
android:name=".pages.AccountLoginFacebookActivity"
android:label="@string/title_activity_account_login_facebook" >
</activity>
<activity
android:name=".pages.AccountLoginGoogleActivity"
android:label="@string/title_activity_account_login_google" >
</activity>
<activity
android:name=".pages.AccountLoginTwitterActivity"
android:label="@string/title_activity_account_login_twitter" >
</activity>
<activity
android:name=".pages.SettingsActivity"
android:label="@string/title_activity_settings" >
</activity>
<activity
android:name=".pages.MainTabActivity"
android:label="@string/title_activity_main_tab" >
</activity>
<activity
android:name=".pages.LobbyActivity"
android:label="@string/title_activity_lobby" >
</activity>
<activity
android:name=".pages.MessageStreamActivity"
android:label="@string/title_activity_message_stream"
android:parentActivityName=".pages.LobbyActivity" >
<meta-data
android:name="android.support.PARENT_ACTIVITY"
android:value="com.edr.iveflownthat.pages.LobbyActivity" />
</activity>
<activity
android:name=".pages.LeaderboardActivity"
android:label="@string/title_activity_leaderboard"
android:parentActivityName=".pages.LobbyActivity" >
<meta-data
android:name="android.support.PARENT_ACTIVITY"
android:value="com.edr.iveflownthat.pages.LobbyActivity" />
</activity>
<service
android:name=".services.MainService"
android:exported="false" />
<activity
android:name=".pages.NewMessageActivity"
android:label="@string/title_activity_new_message" >
</activity>
<activity
android:name=".pages.PlaneActivity"
android:label="@string/title_activity_plane" >
</activity>
<!--
<meta-data
android:name="com.facebook.sdk.ApplicationId"
android:value="@string/app_id" />
<activity
android:name="com.facebook.LoginActivity"
android:label="@string/app_name" >
</activity>
-->
<activity
android:name=".TempLobbyActivity"
android:label="@string/title_activity_temp_lobby" >
</activity>
</application>
</manifest>
采纳答案by kinjal patel
I was getting the same error even though class was defined in AndroidManifest.xml.
即使在 AndroidManifest.xml 中定义了类,我也遇到了同样的错误。
Cause: Sometimes this happens as you add more than one version of appcompat library.
原因:有时当您添加多个版本的 appcompat 库时会发生这种情况。
Solution: Remove any previous version of appcompat lib and add appcompat_v7.jar to your /lib folder.
解决方案:删除任何以前版本的 appcompat lib 并将 appcompat_v7.jar 添加到您的 /lib 文件夹。
Clean your project and build again. You might not get error again.
清理您的项目并重新构建。您可能不会再次出错。
回答by Reinaldo
At your AndroidManifest.xml register the TempLobbyActivity
在您的 AndroidManifest.xml 中注册 TempLobbyActivity
<activity android:name="TempLobbyActivity"></activity>
So your manifest file should be like this
所以你的清单文件应该是这样的
<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>
<activity android:name="TempLobbyActivity"></activity>
回答by Majkel
There is a bug on API level 10 (Android 2.3.3 - 2.3.7) since November 21 (Possibly in the Play Services library):
自 11 月 21 日(可能在 Play 服务库中)以来,API 级别 10(Android 2.3.3 - 2.3.7)有一个错误:
See this bug report:https://code.google.com/p/android/issues/detail?id=81083
请参阅此错误报告:https : //code.google.com/p/android/issues/detail?id=81083
回答by Masoud Dadashi
For the ones who may still face this problem:
对于那些可能仍然面临这个问题的人:
Add proper dependencies to your build.gradle, Such error happens when dependencies aren't properly added and gradle doesn't compile libraries added to the project, So you don't access those library classes in runtime.
向 build.gradle 添加适当的依赖项,当依赖项未正确添加且 gradle 未编译添加到项目中的库时会发生此类错误,因此您不会在运行时访问这些库类。
回答by barkside
I found that I had an error in the console that I had ignored:
我发现我忽略了控制台中的错误:
[ExoPlayerLib] Could not find ExoPlayerLib.apk.
[ExoPlayerLib] 找不到 ExoPlayerLib.apk。
Solving this with SO answer, the secondary error of NoClassDefFoundError was also solved.
用SO answer解决这个问题,NoClassDefFoundError 的次要错误也解决了。
Looks like, any issue with building and including libraries in a project can cause this problem.
看起来,在项目中构建和包含库的任何问题都可能导致此问题。