30

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.access800ドル(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.access800ドル(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()
}
asked Feb 21, 2015 at 9:35
6
  • 1
    Check the activity declared in the manifest. Commented Feb 21, 2015 at 9:39
  • 1
    Could you also post your Manifest file? Commented Feb 21, 2015 at 9:41
  • 1
    i tried rebuliding, cleaning, nothing helped Commented Feb 21, 2015 at 12:56
  • 1
    can you post your module's build.gradle? Commented Feb 21, 2015 at 20:22
  • 1
    i added the manifest and the build.gradle files Commented Feb 22, 2015 at 5:59

11 Answers 11

13

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

runDOSrun
11.1k7 gold badges50 silver badges60 bronze badges
answered Jul 1, 2015 at 12:34
Sign up to request clarification or add additional context in comments.

3 Comments

Worked for me! I just updated to Android Studio 2. Beta 2 from 1.5.1. The project requested me to upgrade the gradle
Tools -> Android -> Sync Project with Gradle Files
Android Studio - Build - Clean Project
12

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

answered Aug 3, 2016 at 11:19

1 Comment

Android Studio - Build - Clean Project
6

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.

answered Feb 21, 2015 at 19:57

Comments

2

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

answered Feb 16, 2016 at 7:31

Comments

1

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.

Giulio Franco
3,25017 silver badges18 bronze badges
answered Aug 4, 2015 at 13:29

Comments

0

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.

answered Mar 8, 2017 at 19:42

Comments

0

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.

answered Apr 21, 2017 at 10:27

Comments

0

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.

answered May 24, 2017 at 6:59

Comments

0

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

answered Sep 23, 2020 at 5:00

Comments

-1

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.

answered Feb 21, 2015 at 9:46

2 Comments

How would I do such a thing?
@LightMikeE I don't know Android studio, but try to click on something like "compile file" in your java class' context menu
-2
  1. Uninstall the app
  2. restart your phone
  3. reinstall the app
answered Mar 17, 2017 at 20:06

Comments

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.