gpt4 book ai didi

java - 找不到类错误,但类肯定存在

转载 作者:IT老高 更新时间:2023-10-28 21:15:35 27 4
gpt4 key购买 nike

好的,我在 Android Studio 上有一个项目,但是当我在模拟器上运行该应用程序时,它崩溃了。在堆栈跟踪中,主 Activity 类有一个类未找到异常,据我了解,它指向仅带有一个 if 语句的右大括号的行。

这是堆栈跟踪:

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

由于我无法弄清楚错误在哪里,我也将在这里发布代码:

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);
}
}

在 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

这里是要求的 list 文件:

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

还有 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'
}

以及项目的 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()
}

最佳答案

首先是“清理”和“使用 Gradle 文件同步项目”。然后运行你的代码。

关于java - 找不到类错误,但类肯定存在,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28644256/

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