0

i am beginner , so do not understand how i fix this problem. My XML code is ok. but when run emulator then click on app its say " unfortunately.... has stopped "

Error msg :

    java.lang.RuntimeException: Unable to instantiate activity
 ComponentInfo{com.example.imran.justjava/com.example.imran.justjava.MainActivity}: java.lang.ClassNotFoundException: Didn't find class "com.example.imran.justjava.MainActivity" on path: DexPathList[[zip file "/data/app/com.example.imran.justjava-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.imran.justjava.MainActivity" on path: DexPathList[[zip file "/data/app/com.example.imran.justjava-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.ClassNotFoundException: com.example.imran.justjava.MainActivity

    Caused by: java.lang.NoClassDefFoundError: Class not found using the boot class loader; no stack available

Here is my java code

import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.TextView;

import com.example.imran.justjava.R;

/**
* This app displays an order form to order coffee.
*/
public class MainActivity extends AppCompatActivity {

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
}

/**
 * This method is called when the order button is clicked.
 */
public void submitOrder(View view) {
    display(1);
}

/**
 * This method displays the given quantity value on the screen.
 */
private void display(int number) {
    TextView quantityTextView = (TextView) findViewById(
            R.id.quantity_text_view);
    quantityTextView.setText("" + number);
}
}

Here is Xml Code

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginBottom="16dp"
        android:text="quantity"
        android:textAllCaps="true"
        android:padding="16dp"
        android:textSize="16sp"
        />
    <TextView
        android:id="@+id/quantity_text_view"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="0"
        android:padding="16dp"
        android:textSize="16sp"
        android:textColor="@android:color/black"

        />
    <Button
        android:id="@+id/order_button"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginTop="16dp"
        android:text="order"
        android:onClick="submitOrder"


        />

</LinearLayout>

AndroidManifast.xml code

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.example.imran.justjava" >

    <application
        android:allowBackup="true"
        android:icon="@mipmap/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>

</manifest>
Imran Khan
  • 52
  • 2
  • 10

4 Answers4

1

I had the same issue in latest AndroidStudio. Everything was compiling and running fine in later versions of Android.

However, as soon as I ran the application on an emulator with Android 4.x, it would crash with the same ClassNotFoundException as mentioned in this post.

After trying a lot of solutions suggested online, it turns out the issue was related to multiDexEnabled being set to true. I could have sworn this was working fine before. Disabling the multidex solved the issue.

Following the guide at https://developer.android.com/studio/build/multidex.html#mdex-gradle should probably fix it as well, although I have not tried that out myself.

  • Setting "multiDexEnabled false" in the build.gradle -> android -> defaultConfig solved the issue, thanks. – Nicholas Obert Nov 15 '21 at 19:32
  • try this answer if you are facing same issue while developing flutter app. https://stackoverflow.com/a/54680454/6303688 – Nicks Jan 05 '22 at 07:42
0

What is your logcat throws

Caused by: java.lang.ClassNotFoundException: Didn't find class "com.example.imran.justjava.MainActivity"

ClassNotFoundException : ClassNotFoundException occurs when class loader could not find the required class in class path . So , basically you should check your class path and add the class in the classpath.

I guess your activity in manifest is not correct.

Post your manifest.xml .

Have a look here https://developer.android.com/intl/es/samples/BasicContactables/AndroidManifest.html

Example

<activity
      android:name=".MainActivity"
       >
   
      <intent-filter>
         <action android:name="android.intent.action.MAIN" />
         <category android:name="android.intent.category.LAUNCHER"/>
      </intent-filter>
   
   </activity>
Community
  • 1
  • 1
IntelliJ Amiya
  • 74,896
  • 15
  • 165
  • 198
0

Error:

java.lang.RuntimeException: Unable to instantiate activity ComponentInfo{com.example.imran.justjava/com.example.imran.justjava.MainActivity}: java.lang.ClassNotFoundException: Didn't find class

I have also faced this issue.

Your MainActivity.java is excluded from compile, so this class isn't included in .apk

Remove following line from excludeFromCompile section of the .idea/compiler.xml file

<file url="file://$PROJECT_DIR$/src/com/example/imran/justjava/MainActivity.java" />
Rajesh Jadav
  • 12,801
  • 5
  • 53
  • 78
0

I think error is in your Manifest file. first ensure that you've declared the MainActivity correctly in AndroidManifest.xml

<activity android:name=".MainActivity" >
    <intent-filter>
            <action android:name="android.intent.action.MAIN" />
            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
</activity>
Rajesh Jadav
  • 12,801
  • 5
  • 53
  • 78
Amit Kumar
  • 1,428
  • 2
  • 12
  • 20