Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Class not found error, but class definitely exists

Tags:

java

android

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() } 
like image 744
LightMikeE Avatar asked Feb 21 '15 09:02

LightMikeE


People also ask

What causes class not found exception in Java?

When you get a ClassNotFoundException, it means the JVM has traversed the entire classpath and not found the class you've attempted to reference. The solution, as so often in the Java world, is to check your classpath. You define a classpath on the command line by saying java -cp and then your classpath.

What is no class def found error?

Proactively discover and resolve errors. The NoClassDefFoundError is a runtime error in Java that occurs if the Java Virtual Machine (JVM) or a ClassLoader instance attempts to load the definition of a class that could not be found. The class definition exists at compile-time but is not available at runtime.


1 Answers

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

like image 79
Android Boy Avatar answered Sep 27 '22 20:09

Android Boy