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

Pretty Little Things - Adding An Image... When i get to the section of running the app at 7:29, Im getting "Unfortunately, CrystalBall has stopped"

The app has been running fine through all the lessons up to this point....im not sure what i could have missed in setting up the images correctly....has anyone else had this problem

2 Answers

Here is what is in the Log Cat....it says that I cannont cast ImageView to TextView....although I dont know how I did that:

02-16 04:22:25.928: E/AndroidRuntime(1064): FATAL EXCEPTION: main
02-16 04:22:25.928: E/AndroidRuntime(1064): Process: com.example.crystallball, PID: 1064
02-16 04:22:25.928: E/AndroidRuntime(1064): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.crystallball/com.example.crystallball.MainActivity}: java.lang.ClassCastException: android.widget.ImageView cannot be cast to android.widget.TextView
02-16 04:22:25.928: E/AndroidRuntime(1064):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2176)
02-16 04:22:25.928: E/AndroidRuntime(1064):     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2226)
02-16 04:22:25.928: E/AndroidRuntime(1064):     at android.app.ActivityThread.access$700(ActivityThread.java:135)
02-16 04:22:25.928: E/AndroidRuntime(1064):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1397)
02-16 04:22:25.928: E/AndroidRuntime(1064):     at android.os.Handler.dispatchMessage(Handler.java:102)
02-16 04:22:25.928: E/AndroidRuntime(1064):     at android.os.Looper.loop(Looper.java:137)
02-16 04:22:25.928: E/AndroidRuntime(1064):     at android.app.ActivityThread.main(ActivityThread.java:4998)
02-16 04:22:25.928: E/AndroidRuntime(1064):     at java.lang.reflect.Method.invokeNative(Native Method)
02-16 04:22:25.928: E/AndroidRuntime(1064):     at java.lang.reflect.Method.invoke(Method.java:515)
02-16 04:22:25.928: E/AndroidRuntime(1064):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:777)
02-16 04:22:25.928: E/AndroidRuntime(1064):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:593)
02-16 04:22:25.928: E/AndroidRuntime(1064):     at dalvik.system.NativeStart.main(Native Method)
02-16 04:22:25.928: E/AndroidRuntime(1064): Caused by: java.lang.ClassCastException: android.widget.ImageView cannot be cast to android.widget.TextView
02-16 04:22:25.928: E/AndroidRuntime(1064):     at com.example.crystallball.MainActivity.onCreate(MainActivity.java:20)
02-16 04:22:25.928: E/AndroidRuntime(1064):     at android.app.Activity.performCreate(Activity.java:5243)
02-16 04:22:25.928: E/AndroidRuntime(1064):     at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087)
02-16 04:22:25.928: E/AndroidRuntime(1064):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2140)
02-16 04:22:25.928: E/AndroidRuntime(1064):     ... 11 more
'''


Here is also, my code

```java
package com.example.crystallball;

import android.app.Activity;
import android.os.Bundle;
import android.view.Menu;
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;
    }

}

Found the solution here:

http://stackoverflow.com/questions/11191040/android-java-lang-classcastexception-android-widget-imageview-cannot-be-cast-t

Eclipse tends to mess up your resources every now and then. This leads to some odd behavior such as strings and images being swapped all over your app, and more commonly classCastException(s), which happen when Eclipse switches your Views' ids around.

A few solutions to that problem:

Clean your project.

Modify an xml layout file and save.

Delete your R file. (Don't worry it will be automatically generated again).

Basically anything that makes your project rebuild and re-generate the R file.