Android Studio: ClassNotFoundException

patrick picture patrick · Nov 12, 2013 · Viewed 30.5k times · Source

i was busy with my app for over a week, when suddenly:

11-12 07:59:17.860    1653-1653/nl.test.myapp E/AndroidRuntime﹕ FATAL EXCEPTION: main
java.lang.RuntimeException: Unable to instantiate activity ComponentInfo{nl.test.MyApp/nl.test.MyApp.MainActivity}: java.lang.ClassNotFoundException: Didn't find class "nl.test.myapp.MainActivity" on path: DexPathList[[zip file "/data/app/nl.test.myapp-2.apk"],nativeLibraryDirectories=[/data/app-lib/nl.test.myapp-2, /system/lib]]
        at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2137)
        at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2261)
        at android.app.ActivityThread.access$600(ActivityThread.java:141)
        at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1256)
        at android.os.Handler.dispatchMessage(Handler.java:99)
        at android.os.Looper.loop(Looper.java:137)
        at android.app.ActivityThread.main(ActivityThread.java:5103)
        at java.lang.reflect.Method.invokeNative(Native Method)
        at java.lang.reflect.Method.invoke(Method.java:525)
        at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:737)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
        at dalvik.system.NativeStart.main(Native Method)
 Caused by: java.lang.ClassNotFoundException: Didn't find class "nl.test.myapp.MainActivity" on path: DexPathList[[zip file "/data/app/nl.test.MyApp-2.apk"],nativeLibraryDirectories=[/data/app-lib/nl.test.MyApp-2, /system/lib]]
        at dalvik.system.BaseDexClassLoader.findClass(BaseDexClassLoader.java:53)
        at java.lang.ClassLoader.loadClass(ClassLoader.java:501)
        at java.lang.ClassLoader.loadClass(ClassLoader.java:461)
        at android.app.Instrumentation.newActivity(Instrumentation.java:1061)
        at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2128)
        at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2261)
        at android.app.ActivityThread.access$600(ActivityThread.java:141)
        at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1256)
        at android.os.Handler.dispatchMessage(Handler.java:99)
        at android.os.Looper.loop(Looper.java:137)
        at android.app.ActivityThread.main(ActivityThread.java:5103)
        at java.lang.reflect.Method.invokeNative(Native Method)
        at java.lang.reflect.Method.invoke(Method.java:525)
        at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:737)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
        at dalvik.system.NativeStart.main(Native Method)

here is my AndroidManifest.xml:

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="nl.test.myapp"
    android:versionCode="1"
    android:versionName="1.0" >

    <uses-sdk
        android:minSdkVersion="7"
        android:targetSdkVersion="19" />

    <uses-permission android:name="android.permission.INTERNET" />

    <application
        android:allowBackup="true"
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >
        <activity
            android:name="nl.test.myapp.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>

        <activity android:name=".welcomewizard.WelcomeWizardIntroPage"
            android:label="@string/app_name" />

        <activity android:name=".welcomewizard.WelcomeWizardPage1"
            android:label="@string/app_name" />

        <activity android:name=".welcomewizard.WelcomeWizardPage2"
            android:label="@string/app_name" />

        <activity android:name=".welcomewizard.WelcomeWizardPage3"
            android:label="@string/app_name" />

        <activity android:name=".welcomewizard.WelcomeWizardPage4"
            android:label="@string/app_name" />

        <activity android:name=".welcomewizard.WelcomeWizardFinishPage"
            android:label="@string/app_name" />

        <activity android:name=".CompanySelectorActivity"
            android:label="@string/compsel_actionbarlabel" />
    </application>

</manifest>

this was happening after some problems with my R.java (the id's for the views where incorrect), after rebuilding the project this problem happend

i've already tried the Invalidate Cashes/restart in the IDE File-menu

Update 14:51: this is my compiler.xml file:

<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
  <component name="CompilerConfiguration">
    <option name="DEFAULT_COMPILER" value="Javac" />
    <resourceExtensions />
    <wildcardResourcePatterns>
      <entry name="!?*.java" />
      <entry name="!?*.form" />
      <entry name="!?*.class" />
      <entry name="!?*.groovy" />
      <entry name="!?*.scala" />
      <entry name="!?*.flex" />
      <entry name="!?*.kt" />
      <entry name="!?*.clj" />
    </wildcardResourcePatterns>
    <annotationProcessing>
      <profile default="true" name="Default" enabled="false">
        <processorPath useClasspath="true" />
      </profile>
    </annotationProcessing>
  </component>
</project>

My MainActivity.java file (\src\main\java\nl\test\myapp\MainActivity.java) :

package nl.test.myapp;

import android.annotation.TargetApi;
import android.content.Intent;
import android.os.Build;
import android.os.Bundle;
import android.support.v7.app.ActionBarActivity;
import android.view.Menu;
import android.widget.RelativeLayout;

import nl.test.myapp.welcomewizard.WelcomeWizardIntroPage;

public class MainActivity extends ActionBarActivity {

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

        setBackground(R.drawable.background_grey);

        Intent intent = new Intent(this, WelcomeWizardIntroPage.class);
        startActivity(intent);
        finish();
    }


    @Override
    public boolean onCreateOptionsMenu(Menu menu) {

        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.main, menu);
        return true;
    }

    @TargetApi(Build.VERSION_CODES.JELLY_BEAN)
    private void setBackground(int drawable){
        int currentApiVersion = android.os.Build.VERSION.SDK_INT;
        if(currentApiVersion >= android.os.Build.VERSION_CODES.JELLY_BEAN){
            RelativeLayout layout = (RelativeLayout) findViewById(R.id.layout_main);
            layout.setBackground(getResources().getDrawable(drawable));
        }
    }
}

Answer

user2415486 picture user2415486 · Apr 23, 2015

Close android studio. Navigate to the project folder. Delete all files relating to intelliJ (Android Studio) i.e

  • .idea (folder)
  • *.imi (files)

Also make sure that package name matches the package in the android manifest.xml.

Then reimport the project.

This happened to me because I renamed the root package and IntelliJ (android studio) didn't track this.