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 trialNils Garland
18,416 PointsUnfortunately, Fun Facts has stopped. What's wrong?
The error says exactly as above when I try to run the application. But when I check for errors in my code, I can't find any.
In logcat, the most relevant issue I could find regarding my code was (note the last line)
Caused by: java.lang.ClassCastException: android.support.constraint.ConstraintLayout cannot be cast to android.widget.RelativeLayout
at se.andaz.funfacts.FunFactsActivity.onCreate(FunFactsActivity.java:32)
My FunFactsActivity.java file looks like this:
package se.andaz.funfacts;
import android.graphics.Color;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.RelativeLayout;
import android.widget.TextView;
import java.util.Random;
public class FunFactsActivity extends AppCompatActivity {
private FactBook mFactBook = new FactBook();
private ColorWheel mColorWheel = new ColorWheel();
// Declare our view variables
private TextView mFactTextView;
private Button mShowFactButton;
private RelativeLayout mRelativeLayout;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_fun_facts);
// Assign the Views from the layout file to the corresponding variable.
mFactTextView = (TextView) findViewById(R.id.factTextView);
mShowFactButton = (Button) findViewById(R.id.funFactButton);
mRelativeLayout = (RelativeLayout) findViewById(R.id.relativeLayout);
View.OnClickListener listener = new View.OnClickListener() {
@Override
public void onClick(View v) {
String fact = mFactBook.getFact();
int color = mColorWheel.getColor();
// Update the screen with our dynamic fact.
mFactTextView.setText(fact);
mShowFactButton.setTextColor(color);
mRelativeLayout.setBackgroundColor(color);
}
};
mShowFactButton.setOnClickListener(listener);
}
}
My FactBook.java file:
package se.andaz.funfacts;
import java.util.Random;
public class FactBook {
// Fields (Member Variables) - Properties about the object
private final String[] mFacts = {
"Ants stretch when they wake up in the morning.",
"Ostriches can run faster than horses.",
"Olympic gold medals are actually made mostly of silver.",
"You are born with 300 bones; by the time you are an adult you will have 206.",
"It takes about 8 minutes for light from the Sun to reach Earth.",
"Some bamboo plants can grow almost a meter in just one day.",
"The state of Florida is bigger than England.",
"Some penguins can leap 2-3 meters out of the water.",
"On average, it takes 66 days to form a new habit.",
"Mammoths still walked the earth when the Great Pyramid was being built." };
// Methods - Actions the object can take
public String getFact()
{
String fact = "";
// Randomly select a fact.
Random randomGenerator = new Random();
int randomNumber = randomGenerator.nextInt(mFacts.length);
fact = mFacts[randomNumber];
return fact;
}
}
And finally the colorwheel.java file:
package se.andaz.funfacts;
import android.graphics.Color;
import java.util.Random;
public class ColorWheel {
// Fields (Member Variables) - Properties about the object
private final String[] mColors = {
"#39add1", // light blue
"#3079ab", // dark blue
"#c25975", // mauve
"#e15258", // red
"#f9845b", // orange
"#838cc7", // lavender
"#7d669e", // purple
"#53bbb4", // aqua
"#51b46d", // green
"#e0ab18", // mustard
"#637a91", // dark gray
"#f092b0", // pink
"#b7c0c7" // light gray
};
// Methods - Actions the object can take
public int getColor()
{
String color = "";
// Randomly select a fact.
Random randomGenerator = new Random();
int randomNumber = randomGenerator.nextInt(mColors.length);
color = mColors[randomNumber];
int colorAsInt = Color.parseColor(color);
return colorAsInt;
}
}
1 Answer
Jason Wiram
42,762 PointsBased on the log, you are trying to cast a ConstraintLayout to a RelativeLayout (and Android won't do that). In the following line, you are attempting the cast:
mRelativeLayout = (RelativeLayout) findViewById(R.id.relativeLayout);
It looks strange to me that the View you are finding by id is named relativeLayout, but it is a ConstraintLayout (based on the attempted cast that is throwing an exception). Perhaps that View should be a RelativeLayout? Then you would no longer need the cast.
Nils Garland
18,416 PointsNils Garland
18,416 PointsFixed it, thanks a lot!