Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

Android

ClassCastException

Hello.

I keep getting a ClassCastException and I'm not sure how to fix it:

09-07 15:03:45.727 31235-31235/com.x.x W/Bundle: Key note expected Serializable but value was a com.x.x.items.Note.  The default value <null> was returned.
09-07 15:03:45.737 31235-31235/com.x.x W/Bundle: Attempt to cast generated internal exception:
                                                              java.lang.ClassCastException: com.x.x.items.Note cannot be cast to java.io.Serializable
                                                                  at android.os.BaseBundle.getSerializable(BaseBundle.java:994)
                                                                  at android.os.Bundle.getSerializable(Bundle.java:894)
                                                                  at android.content.Intent.getSerializableExtra(Intent.java:4896)
                                                                  at com.x.x.activities.MainActivity.onCreate(MainActivity.java:46)
                                                                  at android.app.Activity.performCreate(Activity.java:6020)
                                                                  at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1105)
                                                                  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2284)
                                                                  at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2393)
                                                                  at android.app.ActivityThread.access$800(ActivityThread.java:151)
                                                                  at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1309)
                                                                  at android.os.Handler.dispatchMessage(Handler.java:102)
                                                                  at android.os.Looper.loop(Looper.java:135)
                                                                  at android.app.ActivityThread.main(ActivityThread.java:5351)
                                                                  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:908)
                                                                  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:703)

It tells me the error occurs when I cast my note object to a serializable:

mNote = (Note) intent.getSerializableExtra(getString(R.string.note));

Please help!

1 Answer

Brendon Butler
Brendon Butler
4,254 Points

I haven't done this track yet, but my best guess would be that your "Note" class doesn't implement the Serializable interface. If it does, try implementing the Externalizable interface instead.