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 Build a Simple Android App (retired 2014) Pretty Little Things Positioning Text Using a Linear Layout

I have no errors in my code but still crystalball app crashes in the emulator. Please help

folks, i was following instructions and have no errors in my code. But when i run the crystalball app it crashes. Please see below for logcat. Also can you teach me on how to read the error messages? Thank you!

07-16 00:43:25.475: D/dalvikvm(1054): GC_FOR_ALLOC freed 70K, 5% free 2812K/2960K, paused 105ms, total 105ms 07-16 00:43:25.475: I/dalvikvm-heap(1054): Grow heap (frag case) to 3.254MB for 457216-byte allocation 07-16 00:43:25.525: D/dalvikvm(1054): GC_FOR_ALLOC freed <1K, 5% free 3258K/3408K, paused 30ms, total 30ms 07-16 00:43:25.555: D/AndroidRuntime(1054): Shutting down VM 07-16 00:43:25.555: W/dalvikvm(1054): threadid=1: thread exiting with uncaught exception (group=0xb3ae3ba8) 07-16 00:43:25.565: E/AndroidRuntime(1054): FATAL EXCEPTION: main 07-16 00:43:25.565: E/AndroidRuntime(1054): Process: com.example.crystalball, PID: 1054 07-16 00:43:25.565: E/AndroidRuntime(1054): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.crystalball/com.example.crystalball.MainActivity}: java.lang.ClassCastException: android.widget.TextView cannot be cast to android.widget.Button 07-16 00:43:25.565: E/AndroidRuntime(1054): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2195) 07-16 00:43:25.565: E/AndroidRuntime(1054): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2245) 07-16 00:43:25.565: E/AndroidRuntime(1054): at android.app.ActivityThread.access$800(ActivityThread.java:135) 07-16 00:43:25.565: E/AndroidRuntime(1054): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1196) 07-16 00:43:25.565: E/AndroidRuntime(1054): at android.os.Handler.dispatchMessage(Handler.java:102) 07-16 00:43:25.565: E/AndroidRuntime(1054): at android.os.Looper.loop(Looper.java:136) 07-16 00:43:25.565: E/AndroidRuntime(1054): at android.app.ActivityThread.main(ActivityThread.java:5017) 07-16 00:43:25.565: E/AndroidRuntime(1054): at java.lang.reflect.Method.invokeNative(Native Method) 07-16 00:43:25.565: E/AndroidRuntime(1054): at java.lang.reflect.Method.invoke(Method.java:515) 07-16 00:43:25.565: E/AndroidRuntime(1054): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:779) 07-16 00:43:25.565: E/AndroidRuntime(1054): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595) 07-16 00:43:25.565: E/AndroidRuntime(1054): at dalvik.system.NativeStart.main(Native Method) 07-16 00:43:25.565: E/AndroidRuntime(1054): Caused by: java.lang.ClassCastException: android.widget.TextView cannot be cast to android.widget.Button 07-16 00:43:25.565: E/AndroidRuntime(1054): at com.example.crystalball.MainActivity.onCreate(MainActivity.java:23) 07-16 00:43:25.565: E/AndroidRuntime(1054): at android.app.Activity.performCreate(Activity.java:5231) 07-16 00:43:25.565: E/AndroidRuntime(1054): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087) 07-16 00:43:25.565: E/AndroidRuntime(1054): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2159) 07-16 00:43:25.565: E/AndroidRuntime(1054): ... 11 more 07-16 00:43:32.255: I/Process(1054): Sending signal. PID: 1054 SIG: 9

1 Answer

Daniel Hartin
Daniel Hartin
18,106 Points

Firstly I'm not all that good at reading these error messages myself so can't help there much it seems a bit of a wading through the crap game to me as well at the minute.

From the message posted above though on line 10 is does say:

java.lang.ClassCastException: android.widget.TextView cannot be cast to android.widget.Button

so your code has thrown a ClassCastException. You have probably accidentally defined a TextView as a Button go back to your XML file and make sure all your id's match up with the correct cast in your Java code, this doesn't get highlighted as a syntax error as your code is probably correct (syntax) only it's defining the wrong view that's all.

I looked through the code and haven't defined a TextView as a Button anywhere

Thanks for the help. Lets see if anyone has insight into this.