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

Unfortunately, Crystall Ball has stopped

I select the project i want to run then "Run As Android Application" a message shows up like "Unfortunately, Crystall Ball has stopped" i think that started when i added the picture but i dont know certainly. I tried to download the latest version but still crash.

7 Answers

Harry James
Harry James
14,780 Points

Hello again,

Could you try renaming

    Button getAnswerButton = (Button)findViewById(R.id.btnEnligth);

to

    Button getAnswerButton = (Button)findViewById(R.id.button1);

for me and see if that fixes the issue?

(In your MainActivity.java file)

Hiya,

Can you have a look through the LogCat to see where the error is occurring? Just post the contents of the LogCat window in here as that might help identify the problem.

Cheers,

Steve.

Harry James
Harry James
14,780 Points

Agree with Steve, please provide the error log from LogCat. It should be in red text (You can filter in LogCat so that only errors are displayed as well).


This is the error


07-18 00:59:34.580: E/AndroidRuntime(858): 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


MainActivity.java


public class MainActivity extends Activity {

private CrystallBall myCrystallBall = new CrystallBall();
@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.btnEnligth);

    getAnswerButton.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View v) {
            // TODO Auto-generated method stub


    answerLabel.setText(myCrystallBall.getAnAnswer());          }
    });
}


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

CrystallBall.java


import java.util.Random;

public class CrystallBall {

private String []answer = {"It is certain",
        "You are programming like a turtle",
        "I hope this get faster 'cause 'm getting bored",
        "Yes, this is android",
"I like the way you look at me"};

public String getAnAnswer(){


    Random randomGenerator = new Random();
    int randomNumber = randomGenerator.nextInt(answer.length);
    return answer[randomNumber];
}

}


activity_main.xml


<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="com.example.crystallball.MainActivity$PlaceholderFragment" >

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

<Button
    android:id="@+id/button1"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_alignBottom="@+id/imageView1"
    android:layout_centerHorizontal="true"
    android:text="Enlighten me!" />

<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="149dp"
    android:text="You&apos;ll be so proud of your first application"
    android:textSize="32sp"
    android:typeface="serif" />

</RelativeLayout>

Problem fixed. Thanks a lot Harry.

The IDE wasn't working until restart. "R.id.btnEnlight" was right until i restart it then showed me an error. Do I have to restart every time I run the application??

Harry James
Harry James
14,780 Points

Ah. In that case, it was just an issue with Eclipse.

For future reference, you can clean your project instead of restarting. Simply click Project on the top bar and then hit clean. Select the project(s) you wish to clean and then Eclipse will be forced to re-check for errors.

Harry James
Harry James
14,780 Points

Another thing, you may be required to organise your imports whilst doing this. If so, use

Ctrl+Shift+O on a Windows computer or Cmd+Shift+O on a Mac.

Glad you got that sorted.

Do you need the final in front of the TextView?

Still, as long as it works, that's great!

Steve.

Thanks a lot Harry and great advices.

Steve, without final in front of the TextView fails. Don't know why but it requires that. I'll figure out why is needed.

Thanks Steve to.