Java 未找到类错误,但类肯定存在
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/28644256/
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
Class not found error, but class definitely exists
提问by LightMikeE
OK, so I have a project on Android Studio, but when I run the app on the emulator, it crashes. In the stack trace, there is a class not found exception for the main activity class, and by my understanding it points to a line with merely a closing brace for an if statement.
好的,所以我在 Android Studio 上有一个项目,但是当我在模拟器上运行该应用程序时,它崩溃了。在堆栈跟踪中,主活动类有一个未找到类的异常,据我所知,它指向一条只有一个 if 语句的右大括号的行。
Here is the stack trace:
这是堆栈跟踪:
02-21 14:49:44.498 2362-2362/com.example.user.assignment1 E/AndroidRuntime﹕ FATAL EXCEPTION: main
Process: com.example.user.assignment1, PID: 2362
java.lang.RuntimeException: Unable to instantiate activity ComponentInfo{com.example.user.assignment1/com.example.user.assignment1.MainActivity}: java.lang.ClassNotFoundException: Didn't find class "com.example.user.assignment1.MainActivity" on path: DexPathList[[zip file "/data/app/com.example.user.assignment1-2/base.apk"],nativeLibraryDirectories=[/vendor/lib, /system/lib]]
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2209)
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.example.user.assignment1.MainActivity" on path: DexPathList[[zip file "/data/app/com.example.user.assignment1-2/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)
at android.app.Instrumentation.newActivity(Instrumentation.java:1065)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2199)
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)
Suppressed: java.lang.NoClassDefFoundError: com.example.user.assignment1.MainActivity
at dalvik.system.DexFile.defineClassNative(Native Method)
at dalvik.system.DexFile.defineClass(DexFile.java:226)
at dalvik.system.DexFile.loadClassBinaryName(DexFile.java:219)
at dalvik.system.DexPathList.findClass(DexPathList.java:321)
at dalvik.system.BaseDexClassLoader.findClass(BaseDexClassLoader.java:54)
... 14 more
Suppressed: java.lang.ClassNotFoundException: com.example.user.assignment1.MainActivity
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)
... 13 more
Caused by: java.lang.NoClassDefFoundError: Class not found using the boot class loader; no stack available
Since I cannot figure out where the error is, I'm going to post the code here as well:
由于我无法弄清楚错误在哪里,我也将在这里发布代码:
package com.example.user.assignment1;
import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import java.util.Random;
public class MainActivity extends ActionBarActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu_main, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
//noinspection SimplifiableIfStatement
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
public void left_click(View view) {
Button l=(Button) findViewById(R.id.left);
Button r=(Button) findViewById(R.id.right);
int l1 = Integer.parseInt(l.getText().toString());
int r1 = Integer.parseInt(r.getText().toString());
TextView t=(TextView) findViewById(R.id.label);
if(l1>r1)
{
t.setText("Correct! "+l1+" is bigger!");
}
else
{
t.setText("Sorry! " + r1 + " is bigger!");
}
Random ran = new Random();
int random = ran.nextInt(100);
l.setText(random);
r.setText(random);
}
public void right_click(View view) {
Button l=(Button) findViewById(R.id.left);
Button r=(Button) findViewById(R.id.right);
int l1 = Integer.parseInt(l.getText().toString());
int r1 = Integer.parseInt(r.getText().toString());
TextView t=(TextView) findViewById(R.id.label);
if(l1<r1)
{
t.setText("Correct! "+r1+" is bigger!");
}
else
{
t.setText("Sorry! " + l1 + " is bigger!");
}
Random ran = new Random();
int random = ran.nextInt(100);
l.setText(random);
r.setText(random);
}
}
On the ActivityThread.java file, there are many errors which all seem to rise from errors in imports. The imports with errors for that file are:
在 ActivityThread.java 文件中,有许多错误似乎都是由导入错误引起的。该文件有错误的导入是:
import android.content.IIntentReceiver;//IIntentReceiver is in red
import android.content.pm.IPackageManager;//IPackageManager is in red
import android.net.IConnectivityManager;//IConnectivityManager is in red
import com.android.internal.app.IVoiceInteractor;//IVoiceInteractor
import com.android.org.conscrypt.OpenSSLSocketImpl;//conscrypt is in red
import com.android.org.conscrypt.TrustedCertificateStore;//conscrypt is in red
import com.google.android.collect.Lists;//google is in red
import libcore.io.DropBox;//libcore is in red
import libcore.io.EventLogger;//libcore is in red
import libcore.io.IoUtils;//libcore is in red
import libcore.net.event.NetworkEventDispatcher;//libcore is in red
import dalvik.system.CloseGuard;//CloseGuard is in red
import dalvik.system.VMDebug;//VMDebug is in red
import dalvik.system.VMRuntime;//VMRuntime is in red
And here's the manifest file as requested:
这是请求的清单文件:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.user.assignment1" >
<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>
And the build.gradle file:
和 build.gradle 文件:
apply plugin: 'com.android.application'
android {
compileSdkVersion 21
buildToolsVersion "21.1.2"
defaultConfig {
applicationId "com.example.user.assignment1"
minSdkVersion 8
targetSdkVersion 21
versionCode 1
versionName "1.0"
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
}
}
}
dependencies {
compile fileTree(dir: 'libs', include: ['*.jar'])
compile 'com.android.support:appcompat-v7:21.0.3'
}
And the gradle build for the project:
以及项目的 gradle 构建:
// Top-level build file where you can add configuration options common to all sub-projects/modules.
buildscript {
repositories {
jcente
dependencies {
classpath 'com.android.tools.build:gradle:1.0.0'
// NOTE: Do not place your application dependencies here; they belong
// in the individual module build.gradle files
}
}
allprojects {
repositories {
jcenter()
}
}
r()
}
回答by MaKri
This can be because of no .class file. Check that you have defined an output directory of compiled files and try to compile the trouble class again.
这可能是因为没有 .class 文件。检查您是否定义了编译文件的输出目录,然后再次尝试编译故障类。
回答by BrickTop
Your package where you defined your MainActivity is different from the package in your manifest. Your are defining com.example.user.myfirstapp as your package in the manifest and you define your MainActivity with a relative path in the manifest. So the manifest thinks your MainActivity is located at com.example.user.myfirstapp.MainActivty. But your MainActivity actually is in the package com.example.user.assignment1. Either you use an absolute path in your manifest for the MainActivity or you change the package.
您定义 MainActivity 的包与清单中的包不同。您将 com.example.user.myfirstapp 定义为清单中的包,并使用清单中的相对路径定义 MainActivity。因此清单认为您的 MainActivity 位于 com.example.user.myfirstapp.MainActivty。但是您的 MainActivity 实际上在包 com.example.user.assignment1 中。您要么在清单中为 MainActivity 使用绝对路径,要么更改包。
回答by Android Boy
At first "Clean" and "Sync Project with Gradle Files". Then run your code.
首先“清理”和“将项目与 Gradle 文件同步”。然后运行你的代码。
回答by user3865096
I had the following problem:
我遇到了以下问题:
java.lang.NoClassDefFoundError: Class not found using the boot class loader; no stack available
Usually it's AndroidManifest.xml
problem that does not have proper package name or activity with MAIN intent was not properly defined. That's easy fix.
通常它的AndroidManifest.xml
问题是没有正确的包名称或带有 MAIN 意图的活动没有正确定义。这很容易解决。
I had this problem recently and found root cause was on referencing project. This referencing project (BaseGameUtils
) somehow had different version of google-play-service
. The message comes from console
complaining jar mismatch
way before logcat shows above error. Somehow this console message does not stop compiling end up I had runtime error. This costed me like 10 M/Hs.
我最近遇到了这个问题,发现根本原因在于引用项目。这个引用项目 ( BaseGameUtils
) 不知何故有不同版本的google-play-service
. 该消息来自logcat 显示上述错误之前的console
抱怨jar mismatch
方式。不知何故,此控制台消息不会停止编译,最终我遇到了运行时错误。这花费了我 10 M/Hs。
回答by Kalpesh
In Android Studio,
在 Android Studio 中,
Some times due to gradle version, this type of errors are generate.
有时由于 gradle 版本,会生成此类错误。
I faced same and switch from
我面临同样的问题并从
classpath 'com.android.tools.build:gradle:2.0.0-beta2'
类路径 'com.android.tools.build:gradle:2.0.0-beta2'
to
到
classpath 'com.android.tools.build:gradle:1.5.0'
类路径 'com.android.tools.build:gradle:1.5.0'
in Project's build.gradle file. And its worked for me. Actually version gradle:2.0.0-beta2 wasn't stable and there are may be some bug.
在项目的 build.gradle 文件中。它对我有用。实际上版本 gradle:2.0.0-beta2 不稳定,可能存在一些错误。
Note:Always use only stable versions
注意:始终只使用稳定版本
回答by Mohamed
Just delete your project's build folder then clean and run your application .. this did the trick for me.
只需删除您项目的构建文件夹,然后清理并运行您的应用程序..这对我有用。
回答by kalandar
close android studio, delete ".gradle", ".idea", "gradle" these 3 folders. and then open android studio. Now studio will create new config files so app will run correctly.
关闭android studio,删除“.gradle”、“.idea”、“gradle”这3个文件夹。然后打开android studio。现在工作室将创建新的配置文件,因此应用程序将正确运行。
回答by Darush
- Uninstall the app
- restart your phone
- reinstall the app
- 卸载应用
- 重启你的手机
- 重新安装应用程序
回答by Dante
I face on this kind of error, and the solution for me was to add in the gradle file dependencies (app) "compile 'com.android.support:design:25.3.1'" This happened because I started a project without this dependencies and then I added the "card" support, that need this dependencies.
我遇到了这种错误,我的解决方案是在gradle文件依赖项(app)中添加“compile'com.android.support:design:25.3.1'”发生这种情况是因为我开始了一个没有这个依赖项的项目然后我添加了需要此依赖项的“卡”支持。
回答by Zon
This can happen after renaming packages and classes. In one of your xml layouts you may be referencing a custom class inside a tag. This path is not renamed by Android Studio, unfortunately. Check if the path is correct.
这可能发生在重命名包和类之后。在您的 xml 布局之一中,您可能会引用标签内的自定义类。不幸的是,Android Studio 没有重命名此路径。检查路径是否正确。
This also could be a Gradle settings collapse. Try to remove .gradle
and build
foldersfrom your project folder and build
folder from your module folder, rebuild and restart the application. Removed folder will be regenerated with correct settings.
这也可能是 Gradle 设置崩溃。尝试删除.gradle
和build
文件夹,从项目文件夹和build
文件夹从你的模块文件夹,重建并重新启动应用程序。删除的文件夹将使用正确的设置重新生成。