Android: Unable to instantiate activity / ClassNotFoundException

I recently published an app to the market and I'm now getting an error by some user, the app presumably crashes right when it starts. Unfortunately I can't contact him directly and the app works fine in the emulator as well as on my phone (and some friends' phones).
EDIT: I guess that this happens to more than one user as I received comments in the market like "crashes on start" or "doesn't work". I only received this one stacktrace but there's no info about the configuration, device, Android version, etc.

The app is a simple soundboard, so there's really no magic involved, but I can't get why it fails on some phones. Here's the stack trace I'm getting, I hope anybody can help me out:

java.lang.RuntimeException: Unable to instantiate activity ComponentInfo{com.my.app/com.my.app.SoundMachine}: java.lang.ClassNotFoundException: com.my.app.SoundMachine in loader dalvik.system.PathClassLoader[/mnt/asec/com.my.app-1/pkg.apk]
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2585)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2679)
at android.app.ActivityThread.access$2300(ActivityThread.java:125)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2033)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:123)
at android.app.ActivityThread.main(ActivityThread.java:4627)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:521)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:876)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:634)
at dalvik.system.NativeStart.main(Native Method)
Caused by: java.lang.ClassNotFoundException: com.my.app.SoundMachine in loader dalvik.system.PathClassLoader[/mnt/asec/com.my.app-1/pkg.apk]
at dalvik.system.PathClassLoader.findClass(PathClassLoader.java:243)
at java.lang.ClassLoader.loadClass(ClassLoader.java:573)
at java.lang.ClassLoader.loadClass(ClassLoader.java:532)
at android.app.Instrumentation.newActivity(Instrumentation.java:1021)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2577)
... 11 more

These are the first couple of lines from my activity:

public class SoundMachine extends Activity {
  private SoundManager mSoundManager;

  @Override
  public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);

EDIT: This is the (almost) complete onCreate:

  @Override
  public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);

    mSoundManager = new SoundManager();
    mSoundManager.initSounds(getBaseContext());

    int counter = 0;
    for (Integer soundFile : soundFiles) {
      counter++;
      mSoundManager.addSound(counter, soundFile);
    }

    ImageButton SoundButton1 = (ImageButton) findViewById(R.id.sound1);
    SoundButton1.setOnClickListener(new OnClickListener() {
      public void onClick(View v) {
        mSoundManager.playSound(1);
      }
    });
    SoundButton1.setOnLongClickListener(new OnLongClickListener() {
      public boolean onLongClick(View v) {
        saveSoundChoice(soundFiles[0], soundNames[0]);
        return true;
      }
    });

(...more of this...)

    Button StopButton = (Button) findViewById(R.id.stopbutton);
    StopButton.setOnClickListener(new OnClickListener() {
      public void onClick(View v) {
        mSoundManager.stopAll();
      }
    });
  }

And here's my manifest:

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.my.app" android:installLocation="preferExternal"
    android:versionCode="9" android:versionName="1.2">
    <application android:icon="@drawable/icon" android:label="@string/app_name">
        <activity android:name=".SoundMachine" android:label="@string/app_name"
            android:screenOrientation="portrait">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
    </application>
    <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
    <uses-sdk android:minSdkVersion="3" android:targetSdkVersion="8" />
</manifest>

So all possible errors I already read about here and in some forums don't apply to my app.

  • The activity is present in the manifest.
  • The super-method is called in the overriden method.
  • The ContentView is set before accessing elements in the view.

I know it's hard to pinpoint the source of an error without being able to reproduce it, but maybe somebody has a bright idea and can help me out.

Some questions:

  • Do I need the "intent"-part in the manifest? Eclipse created it when I created the project. (Yes, according to Mayra)
  • Should the super-method be called where it is? (Yes, according to Mayra)

EDIT: The main question that remains now is: How come the path in PathClassLoader is different from my package-name? The page John J Smith posted seems to deal with the same problem, but I don't understand the fix that was applied there.

Thanks, Select0r


Solution 1:

I solved this problems by selecting : Project->properties->order and export

select the all external jar files. clean and build it solved the issue

Solution 2:

Since ADT update to revision 22 (May 2013) you have to check "Android Private Libraries" check box in Project -> Properties -> Java Build Path -> Order and Export in Eclipse for your older projects to get rid of this exception ...

Solution 3:

I just got the same error (Unable to instantiate activity...) with Opera Mini. Opera Mini was on SD card (moved to SD card in the app setting). The error seems to be related to the fact that I swapped the SD card yesterday. The device was shutdown, I copied all data from the old card over to the new card (with cp -a) and then inserted the new card and started the device again. Everything seems to work as expected, but I see now that all apps on the SD card crashes with the same error.

  • Device: HTC Desire HD (Android 2.2)
  • Old SDHC card: SanDisk 8GB class 4
  • New SDHC card: Kingston 16GB class 4

So I would say that this is a an Android bug and not something that can be fixed by app developers.

Also see: http://android-developers.blogspot.com/2010/07/apps-on-sd-card-details.html

It has always been the case that when you swap SD cards on an Android device, if you physically copy the contents of the old card to the new one, the system will use the data on the new card as if nothing had changed. This is also true of apps which have been installed on the SD card."

This seems to be incorrect.

Solution 4:

This happened to me when I refactored one of my package names and the refactor tool removed the leading "." from all of my activity names. so in the AndroidManifest.xml, I had to change android:name="MyActivity" to android:name=".MyActivity"

hope this helps someone, I just wasted a bunch of time o

Solution 5:

This doesn't apply to the OP's question, but it did to mine and it took me quite some time to figure it out: when using the Google Maps API, make sure you've included the library declaration in the application manifest.

For instance:

public class MyActivity extends MapActivity {
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
    }

    @Override
    protected boolean isRouteDisplayed() {
        return false;
    }

}

In AndroidManifest.xml:

<application>
    <uses-library android:name="com.google.android.maps" />
    <activity android:name="MyActivity" 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>