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

Unfortunately, Crystal Ball has stopped

I am not getting an errors in my code. Here are my two java files and the info from the LogCat.

MainActivity.java

package com.example.crystalball;

import android.app.Activity; import android.os.Bundle; import android.view.Menu; import android.view.MenuItem; import android.view.View; import android.widget.Button; import android.widget.TextView;

public class MainActivity extends Activity { private CrystalBall mCrystalBall = new CrystalBall();

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

    // Declare our view variables
    final TextView answerLabel = (TextView) findViewById(R.id.textView1);
    Button getAnswerButton = (Button) findViewById(R.id.button1);

    getAnswerButton.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View v) {
            String answer = mCrystalBall.getAnAnswer();             

            // Update the label with our dynamic answer
            answerLabel.setText(answer);    
        }
    });

}


@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;
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    // Handle action bar item clicks here. The action bar will
    // automatically handle clicks on the Home/Up button, so long
    // as you specify a parent activity in AndroidManifest.xml.
    int id = item.getItemId();
    if (id == R.id.action_settings) {
        return true;
    }
    return super.onOptionsItemSelected(item);
}

}

CrystallBall.java

package com.example.crystalball;

import java.util.Random;

public class CrystalBall { // Member variables (properties about the object) public String[] mAnswers = { "It is certain", "It is decidedly so", "All signs say YES", "The stars are not aligned", "My reply is no", "It is doubtful", "Better not tell you now", "Concentrate and ask again", "Unable to answer now"};

// Methods (abilities: things the object can do)
public String getAnAnswer() {


    String answer = "";

    // Randomly select one of three answers: Yes, No, or Maybe
    Random randomGenerator = new Random(); //Construct a new Random number generator
    int randomNumber = randomGenerator.nextInt(mAnswers.length);

    // Convert the randomNumber to a text answer
    answer = mAnswers[randomNumber];

    return answer;
}

}

LogCat

08-14 11:52:48.973: D/dalvikvm(1087): GC_FOR_ALLOC freed 72K, 5% free 2909K/3052K, paused 71ms, total 83ms 08-14 11:52:48.983: I/dalvikvm-heap(1087): Grow heap (frag case) to 3.677MB for 811216-byte allocation 08-14 11:52:49.083: D/dalvikvm(1087): GC_FOR_ALLOC freed <1K, 4% free 3701K/3848K, paused 83ms, total 84ms 08-14 11:52:49.293: D/AndroidRuntime(1087): Shutting down VM 08-14 11:52:49.293: W/dalvikvm(1087): threadid=1: thread exiting with uncaught exception (group=0xb1a20ba8) 08-14 11:52:49.303: E/AndroidRuntime(1087): FATAL EXCEPTION: main 08-14 11:52:49.303: E/AndroidRuntime(1087): Process: com.example.crystalball, PID: 1087 08-14 11:52:49.303: E/AndroidRuntime(1087): 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 08-14 11:52:49.303: E/AndroidRuntime(1087): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2195) 08-14 11:52:49.303: E/AndroidRuntime(1087): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2245) 08-14 11:52:49.303: E/AndroidRuntime(1087): at android.app.ActivityThread.access$800(ActivityThread.java:135) 08-14 11:52:49.303: E/AndroidRuntime(1087): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1196) 08-14 11:52:49.303: E/AndroidRuntime(1087): at android.os.Handler.dispatchMessage(Handler.java:102) 08-14 11:52:49.303: E/AndroidRuntime(1087): at android.os.Looper.loop(Looper.java:136) 08-14 11:52:49.303: E/AndroidRuntime(1087): at android.app.ActivityThread.main(ActivityThread.java:5017) 08-14 11:52:49.303: E/AndroidRuntime(1087): at java.lang.reflect.Method.invokeNative(Native Method) 08-14 11:52:49.303: E/AndroidRuntime(1087): at java.lang.reflect.Method.invoke(Method.java:515) 08-14 11:52:49.303: E/AndroidRuntime(1087): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:779) 08-14 11:52:49.303: E/AndroidRuntime(1087): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595) 08-14 11:52:49.303: E/AndroidRuntime(1087): at dalvik.system.NativeStart.main(Native Method) 08-14 11:52:49.303: E/AndroidRuntime(1087): Caused by: java.lang.ClassCastException: android.widget.TextView cannot be cast to android.widget.Button 08-14 11:52:49.303: E/AndroidRuntime(1087): at com.example.crystalball.MainActivity.onCreate(MainActivity.java:21) 08-14 11:52:49.303: E/AndroidRuntime(1087): at android.app.Activity.performCreate(Activity.java:5231) 08-14 11:52:49.303: E/AndroidRuntime(1087): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087) 08-14 11:52:49.303: E/AndroidRuntime(1087): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2159) 08-14 11:52:49.303: E/AndroidRuntime(1087): ... 11 more

1 Answer

Marlon Copeland
Marlon Copeland
1,675 Points

.ClassCastException: android.widget.TextView cannot be cast to android.widget.Button - Found that in the Log Cat. Make sure your button1 resource isn't actually a TextView.

Hi,

It seems to be a button. I am not sure how to tell for sure that it is a button and not a TextView. It looks like a button and it says it is a button in the xml. Here is the xml code:

<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:background="@android:color/black" 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="com.example.crystalball.MainActivity$PlaceholderFragment" >

<ImageView
    android:id="@+id/imageView1"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:layout_marginTop="39dp"
    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|italic"
    android:typeface="serif" />

<TextView
    android:id="@+id/textView1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignLeft="@+id/imageView1"
    android:layout_centerVertical="true"
    android:gravity="center_horizontal"
    android:shadowColor="@android:color/white"
    android:shadowRadius="10"
    android:textColorHint="@android:color/white"
    android:textSize="32sp" />

</RelativeLayout>