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 Customizing Buttons and Labels

Thomas Salai
Thomas Salai
4,964 Points

Can not start the App anymore after this chapter.

Hi All,

I can't start the App anymore after this chapter. I have got the following error...

05-21 17:02:18.730: D/dalvikvm(953): Not late-enabling CheckJNI (already on)
05-21 17:02:21.620: D/dalvikvm(953): GC_FOR_ALLOC freed 54K, 10% free 3195K/3536K, paused 165ms, total 204ms
05-21 17:02:21.690: I/dalvikvm-heap(953): Grow heap (frag case) to 5.136MB for 1825216-byte allocation
05-21 17:02:21.790: D/dalvikvm(953): GC_FOR_ALLOC freed <1K, 7% free 4978K/5320K, paused 101ms, total 101ms
05-21 17:02:22.480: D/AndroidRuntime(953): Shutting down VM
05-21 17:02:22.480: W/dalvikvm(953): threadid=1: thread exiting with uncaught exception (group=0xb1a4eba8)
05-21 17:02:22.520: E/AndroidRuntime(953): FATAL EXCEPTION: main
05-21 17:02:22.520: E/AndroidRuntime(953): Process: net.mmlug.crystalball, PID: 953
05-21 17:02:22.520: E/AndroidRuntime(953): java.lang.RuntimeException: Unable to start activity ComponentInfo{net.mmlug.crystalball/net.mmlug.crystalball.MainActivity}: java.lang.ClassCastException: android.widget.TextView cannot be cast to android.widget.Button
05-21 17:02:22.520: E/AndroidRuntime(953):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2195)
05-21 17:02:22.520: E/AndroidRuntime(953):  at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2245)
05-21 17:02:22.520: E/AndroidRuntime(953):  at android.app.ActivityThread.access$800(ActivityThread.java:135)
05-21 17:02:22.520: E/AndroidRuntime(953):  at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1196)
05-21 17:02:22.520: E/AndroidRuntime(953):  at android.os.Handler.dispatchMessage(Handler.java:102)
05-21 17:02:22.520: E/AndroidRuntime(953):  at android.os.Looper.loop(Looper.java:136)
05-21 17:02:22.520: E/AndroidRuntime(953):  at android.app.ActivityThread.main(ActivityThread.java:5017)
05-21 17:02:22.520: E/AndroidRuntime(953):  at java.lang.reflect.Method.invokeNative(Native Method)
05-21 17:02:22.520: E/AndroidRuntime(953):  at java.lang.reflect.Method.invoke(Method.java:515)
05-21 17:02:22.520: E/AndroidRuntime(953):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:779)
05-21 17:02:22.520: E/AndroidRuntime(953):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595)
05-21 17:02:22.520: E/AndroidRuntime(953):  at dalvik.system.NativeStart.main(Native Method)
05-21 17:02:22.520: E/AndroidRuntime(953): Caused by: java.lang.ClassCastException: android.widget.TextView cannot be cast to android.widget.Button
05-21 17:02:22.520: E/AndroidRuntime(953):  at net.mmlug.crystalball.MainActivity.onCreate(MainActivity.java:20)
05-21 17:02:22.520: E/AndroidRuntime(953):  at android.app.Activity.performCreate(Activity.java:5231)
05-21 17:02:22.520: E/AndroidRuntime(953):  at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087)
05-21 17:02:22.520: E/AndroidRuntime(953):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2159)
05-21 17:02:22.520: E/AndroidRuntime(953):  ... 11 more
05-21 17:02:06.715: I/Process(953): Sending signal. PID: 953 SIG: 9
05-21 17:02:16.855: D/dalvikvm(1128): GC_FOR_ALLOC freed 39K, 10% free 3195K/3520K, paused 35ms, total 37ms
05-21 17:02:16.875: I/dalvikvm-heap(1128): Grow heap (frag case) to 5.136MB for 1825216-byte allocation
05-21 17:02:16.915: D/dalvikvm(1128): GC_FOR_ALLOC freed <1K, 7% free 4978K/5304K, paused 35ms, total 35ms
05-21 17:02:17.095: D/AndroidRuntime(1128): Shutting down VM
05-21 17:02:17.095: W/dalvikvm(1128): threadid=1: thread exiting with uncaught exception (group=0xb1a4eba8)
05-21 17:02:17.105: E/AndroidRuntime(1128): FATAL EXCEPTION: main
05-21 17:02:17.105: E/AndroidRuntime(1128): Process: net.mmlug.crystalball, PID: 1128
05-21 17:02:17.105: E/AndroidRuntime(1128): java.lang.RuntimeException: Unable to start activity ComponentInfo{net.mmlug.crystalball/net.mmlug.crystalball.MainActivity}: java.lang.ClassCastException: android.widget.TextView cannot be cast to android.widget.Button
05-21 17:02:17.105: E/AndroidRuntime(1128):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2195)
05-21 17:02:17.105: E/AndroidRuntime(1128):     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2245)
05-21 17:02:17.105: E/AndroidRuntime(1128):     at android.app.ActivityThread.access$800(ActivityThread.java:135)
05-21 17:02:17.105: E/AndroidRuntime(1128):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1196)
05-21 17:02:17.105: E/AndroidRuntime(1128):     at android.os.Handler.dispatchMessage(Handler.java:102)
05-21 17:02:17.105: E/AndroidRuntime(1128):     at android.os.Looper.loop(Looper.java:136)
05-21 17:02:17.105: E/AndroidRuntime(1128):     at android.app.ActivityThread.main(ActivityThread.java:5017)
05-21 17:02:17.105: E/AndroidRuntime(1128):     at java.lang.reflect.Method.invokeNative(Native Method)
05-21 17:02:17.105: E/AndroidRuntime(1128):     at java.lang.reflect.Method.invoke(Method.java:515)
05-21 17:02:17.105: E/AndroidRuntime(1128):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:779)
05-21 17:02:17.105: E/AndroidRuntime(1128):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595)
05-21 17:02:17.105: E/AndroidRuntime(1128):     at dalvik.system.NativeStart.main(Native Method)
05-21 17:02:17.105: E/AndroidRuntime(1128): Caused by: java.lang.ClassCastException: android.widget.TextView cannot be cast to android.widget.Button
05-21 17:02:17.105: E/AndroidRuntime(1128):     at net.mmlug.crystalball.MainActivity.onCreate(MainActivity.java:20)
05-21 17:02:17.105: E/AndroidRuntime(1128):     at android.app.Activity.performCreate(Activity.java:5231)
05-21 17:02:17.105: E/AndroidRuntime(1128):     at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087)
05-21 17:02:17.105: E/AndroidRuntime(1128):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2159)
05-21 17:02:17.105: E/AndroidRuntime(1128):     ... 11 more
05-21 17:02:19.585: I/Process(1128): Sending signal. PID: 1128 SIG: 9

My activity_main.xml look like that

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context="net.mmlug.crystalball.MainActivity$PlaceholderFragment" >

    <ImageView
        android:id="@+id/imageView1"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:scaleType="fitCenter"
        android:src="@drawable/ball01" />

    <Button
        android:id="@+id/button1"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        android:layout_alignParentLeft="true"
        android:text="Enlighten Me!"
        android:textColor="#3f0f7f"
        android:textSize="24sp"
        android:textStyle="bold"
        android:typeface="serif" />

    <TextView
        android:id="@+id/textView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_above="@+id/button1"
        android:layout_alignLeft="@+id/imageView1"
        android:layout_marginBottom="210dp"
        android:gravity="center_horizontal"
        android:shadowColor="@android:color/white"
        android:textColor="@android:color/white"
        android:textSize="32sp" />

</RelativeLayout>

Many thanks in advance.

Best Regards, Thomas

2 Answers

Chris Shaw
Chris Shaw
26,676 Points

Hi Thomas,

You have a typecast exception in your code, simply ensure you're defining the correct class type and try building your app again.

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

Thomas Salai
Thomas Salai
4,964 Points

HI Chris,

Thanks.! Actually the typecase is correct. Anyhow, I can ran again the app without any changes in the code. :-)

´´´ final TextView answerLabel = (TextView) findViewById(R.id.textView1); Button getAnswerButton = (Button) findViewById(R.id.button1); ´´´