类没有发现错误,但绝对类存在 [英] Class not found error, but class definitely exists

查看:149
本文介绍了类没有发现错误,但绝对类存在的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

好了,我在Android Studio中的一个项目,但是当我运行模拟器上的应用程序,它崩溃。在堆栈跟踪,还有一类未发现异常为主要活动类,并通过我的理解它指向一个符合只是一个右括号的if语句。

下面是堆栈跟踪:

  02-21 14:49:44.498 ​​2362年至2362年/ com.example.user.assignment1 E / AndroidRuntime:致命异常:主要
工艺:com.example.user.assignment1,PID:2362
java.lang.RuntimeException的:无法实例活动ComponentInfo {com.example.user.assignment1 / com.example.user.assignment1.MainActivity}:抛出java.lang.ClassNotFoundException:没有找到类com.example.user.assignment1 .MainActivity的路径:DexPathList [zip文件/data/app/com.example.user.assignment1-2/base.apk"],nativeLibraryDirectories=[/vendor/lib,/系统/ lib目录]
        在android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2209)
        在android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2360)
        在android.app.ActivityThread.access $ 800(ActivityThread.java:144)
        在android.app.ActivityThread $ H.handleMessage(ActivityThread.java:1278)
        在android.os.Handler.dispatchMessage(Handler.java:102)
        在android.os.Looper.loop(Looper.java:135)
        在android.app.ActivityThread.main(ActivityThread.java:5221)
        在java.lang.reflect.Method.invoke(本机方法)
        在java.lang.reflect.Method.invoke(Method.java:372)
        在com.android.internal.os.ZygoteInit $ MethodAndArgsCaller.run(ZygoteInit.java:899)
        在com.android.internal.os.ZygoteInit.main(ZygoteInit.java:694)
 抛出java.lang.ClassNotFoundException:产生的原因没有找到类com.example.user.assignment1.MainActivity的路径:DexPathList [zip文件/data/app/com.example.user.assignment1-2/base apk文件],nativeLibraryDirectories = [/供应商/ lib中/系统/ lib目录]
        在dalvik.system.BaseDexClassLoader.findClass(BaseDexClassLoader.java:56)
        在java.lang.ClassLoader.loadClass(ClassLoader.java:511)
        在java.lang.ClassLoader.loadClass(ClassLoader.java:469)
        在android.app.Instrumentation.newActivity(Instrumentation.java:1065)
        在android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2199)
在android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2360)
在android.app.ActivityThread.access $ 800(ActivityThread.java:144)
在android.app.ActivityThread $ H.handleMessage(ActivityThread.java:1278)
在android.os.Handler.dispatchMessage(Handler.java:102)
在android.os.Looper.loop(Looper.java:135)
在android.app.ActivityThread.main(ActivityThread.java:5221)
在java.lang.reflect.Method.invoke(本机方法)
在java.lang.reflect.Method.invoke(Method.java:372)
在com.android.internal.os.ZygoteInit $ MethodAndArgsCaller.run(ZygoteInit.java:899)
在com.android.internal.os.ZygoteInit.main(ZygoteInit.java:694)
燮pressed:java.lang.NoClassDefFoundError的:com.example.user.assignment1.MainActivity
        在dalvik.system.DexFile.defineClassNative(本机方法)
        在dalvik.system.DexFile.defineClass(DexFile.java:226)
        在dalvik.system.DexFile.loadClassBinaryName(DexFile.java:219)
        在dalvik.system.DexPathList.findClass(DexPathList.java:321)
        在dalvik.system.BaseDexClassLoader.findClass(BaseDexClassLoader.java:54)
        ... 14更多
燮pressed:抛出java.lang.ClassNotFoundException:com.example.user.assignment1.MainActivity
        在java.lang.Class.classForName(本机方法)
        在java.lang.BootClassLoader.findClass(ClassLoader.java:781)
        在java.lang.BootClassLoader.loadClass(ClassLoader.java:841)
        在java.lang.ClassLoader.loadClass(ClassLoader.java:504)
        ... 13更多
 java.lang.NoClassDefFoundError的:产生的原因未找到类使用引导类加载器;没有堆栈可用
 

既然我无法揣摩出了错误,我要去发布code在这里也:

 包com.example.user.assignment1;

进口android.support.v7.app.ActionBarActivity;
进口android.os.Bundle;
进口android.view.Menu;
进口android.view.MenuItem;
进口android.view.View;
进口android.widget.Button;
进口android.widget.TextView;

导入了java.util.Random;


公共类MainActivity扩展ActionBarActivity {

@覆盖
保护无效的onCreate(包savedInstanceState){
    super.onCreate(savedInstanceState);
    的setContentView(R.layout.activity_main);
}


@覆盖
公共布尔onCreateOptionsMenu(功能菜单){
    //充气菜单;这增加了项目操作栏,如果它是present。
    。getMenuInflater()膨胀(R.menu.menu_main,菜单);
    返回true;
}

@覆盖
公共布尔onOptionsItemSelected(菜单项项){
    //处理动作栏项目点击这里。将操作栏
    //自动在主/向上按钮操作的点击,只要
    //你在AndroidManifest.xml中指定一个父活动。
    INT的id = item.getItemId();

    // noinspection SimplifiableIfStatement
    如果(ID == R.id.action_settings){
        返回true;
    }

    返回super.onOptionsItemSelected(项目);
}

公共无效left_click(查看视图){
    按钮L =(按钮)findViewById(R.id.left);
    按钮R =(按钮)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);




    如果(L1> R1)
    {
        t.se​​tText(正确!+ L1 +更大!);
    }
    其他
    {
        t.se​​tText(!对不起+ R1 +更大!);
    }

    随机RAN =新的随机();
    INT无规= ran.nextInt(100);

    l.setText(随机);
    r.setText(随机);

}

公共无效right_click(查看视图){
    按钮L =(按钮)findViewById(R.id.left);
    按钮R =(按钮)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);




    如果(L1< R1)
    {
        t.se​​tText(!正确的+ R1 +更大!);
    }
    其他
    {
        t.se​​tText(!对不起+ L1 +更大!);
    }
    随机RAN =新的随机();
    INT无规= ran.nextInt(100);

    l.setText(随机);
    r.setText(随机);
}
}
 

在ActivityThread.java文件,有很多的错误,是似乎都从错误中上升的进口。与该文件错误的进口是:

 进口android.content.IIntentReceiver; // IIntentReceiver是红色的
进口android.content.pm.IPackageManager; // IPackageManager是红色的
进口android.net.IConnectivityManager; // IConnectivityManager是红色的
进口com.android.internal.app.IVoiceInteractor; // IVoiceInteractor
进口com.android.org.conscrypt.OpenSSLSocketImpl; // conscrypt是红色的
进口com.android.org.conscrypt.TrustedCertificateStore; // conscrypt是红色的
进口com.google.android.collect.Lists; //谷歌是红色的
进口libcore.io.DropBox; // libcore是红色的
进口libcore.io.EventLogger; // libcore是红色的
进口libcore.io.IoUtils; // libcore是红色的
进口libcore.net.event.NetworkEventDispatcher; // libcore是红色的
进口dalvik.system.CloseGuard; // CloseGuard是红色的
进口dalvik.system.VMDebug; // VMDebug是红色的
进口dalvik.system.VMRuntime; // VMRuntime是红色的
 

这是因为要求的清单文件:

 < XML版本=1.0编码=UTF-8&GT?;
<舱单的xmlns:机器人=htt​​p://schemas.android.com/apk/res/android
包=com.example.user.assignment1>

<应用
    机器人:allowBackup =真
    机器人:图标=@可绘制/ ic_launcher
    机器人:标签=@字符串/ APP_NAME
    机器人:主题=@风格/ AppTheme>
    <活动
        机器人:名称=。MainActivity
        机器人:标签=@字符串/ APP_NAME>
        <意向滤光器>
            <作用机器人:名称=android.intent.action.MAIN/>

            <类机器人:名称=android.intent.category.LAUNCHER/>
        &所述; /意图滤光器>
    < /活性GT;
< /用途>
 

而build.gradle文件:

 应用插件:com.android.application

安卓{
compileSdkVersion 21
buildToolsVersion21.1.2

defaultConfig {
    的applicationIDcom.example.user.assignment1
    的minSdkVersion 8
    targetSdkVersion 21
    版本code 1
    VERSIONNAME1.0
}
buildTypes {
    推出 {
        minifyEnabled假
        proguardFiles getDefaultProguardFile('ProGuard的-android.txt'),'proguard-rules.pro
    }
}
}

依赖{
编译文件树(导演:库,包括:['的* .jar'])
编译com.android.support:appcompat-v7:21.0.3
}
 

和的摇篮建设项目:

  //顶级构建文件,你可以到各子项目/模块添加常用的配置选项。

buildscript {
库{
    jcente
    依赖{
        类路径com.android.tools.build:gradle:1.0.0

        //注意:不要在这里放置应用程序依赖;他们属于
        //在单个模块build.gradle文件
    }
}

allprojects {
    库{
        jcenter()
    }
}
R()
}
 

解决方案

在这里你定义了MainActivity你的包是从包在你的清单不同。你要定义com.example.user.myfirstapp作为你的包中的清单和你在清单中的相对路径定​​义MainActivity。因此,清单认为你MainActivity位于com.example.user.myfirstapp.MainActivty。但是你MainActivity实际上是在包com.example.user.assignment1。要么你使用绝对路径在你的清单为MainActivity或更改套餐。

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.

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

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:

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:

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:

// 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()
}

解决方案

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.

这篇关于类没有发现错误,但绝对类存在的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆