LightMikeE
LightMikeE

Reputation: 729

Class not found error, but class definitely exists

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

Upvotes: 30

Views: 52528

Answers (11)

Ybhaw
Ybhaw

Reputation: 145

Had the same issue, was not able to get the file even after trying all answers. The issue was my project was present in a directory that had space in their path.

C:\Users\SomeUser\SOMENAME WITHSPACE\project

After moving the project to an other folder without space, build was successful and was able to run the project.

Gradle versionw as 5.5.1

Upvotes: 0

Zon
Zon

Reputation: 19880

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.

This also could be a Gradle settings collapse. Try to remove .gradle and build folders from your project folder and build folder from your module folder, rebuild and restart the application. Removed folder will be regenerated with correct settings.

Upvotes: 0

Dante
Dante

Reputation: 99

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.

Upvotes: 0

Darush
Darush

Reputation: 12021

  1. Uninstall the app
  2. restart your phone
  3. reinstall the app

Upvotes: -2

kalandar
kalandar

Reputation: 823

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.

Upvotes: 0

Kalpesh
Kalpesh

Reputation: 1807

In Android Studio,

Some times due to gradle version, this type of errors are generate.

I faced same and switch from

classpath 'com.android.tools.build:gradle:2.0.0-beta2'

to

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

Note: Always use only stable versions

Upvotes: 2

Mohamed
Mohamed

Reputation: 791

Just delete your project's build folder then clean and run your application .. this did the trick for me.

Upvotes: 12

user3865096
user3865096

Reputation: 29

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.

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.

Upvotes: 1

Android Boy
Android Boy

Reputation: 4345

At first "Clean" and "Sync Project with Gradle Files". Then run your code.

Upvotes: 13

BrickTop
BrickTop

Reputation: 230

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.

Upvotes: 6

MaKri
MaKri

Reputation: 200

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.

Upvotes: -1

Related Questions