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
chandlerknaus
1,635 PointsFun Facts Code? Error?
Sadly I had to pause treehouse and wait until I get a macbook but I need the fun facts source code for a project I am doing. This is what I have
//FUN FACTS ACTIVITY
public class FactBookActivity extends Activity {
public Facts mFacts = new Facts();
public Color mColor = new Color();
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_fact_book);
}
final TextView factLabel = (TextView) findViewById(R.id.factTextView);
final Button showFactButton = (Button) findViewById(R.id.showFactButton);
final RelativeLayout relativeLayout = (RelativeLayout) findViewById(R.id.relativeLayout);
View.OnClickListener listener = new View.OnClickListener() { (*It says this is never used?*)
public void onClick(View view) {
String fact = mFacts.getFact();
//Update the label with our dynamic fact
factLabel.setText(fact);
int color = mColor.getcolor();
relativeLayout.setBackgroundColor(color);
showFactButton.setTextColor(color);
}
};
}
//COLOR
public class Color {
public 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
};
public int getcolor() {
String color = ""; (*Variable color is redundant?*)
//Randomly select a fact
Random randomGenerator = new Random(); // Construct a new random generator
int randomNumber = randomGenerator.nextInt(mColors.length);
color = mColors[randomNumber];
return android.graphics.Color.parseColor(color);
}
}
//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:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
android:paddingBottom="@dimen/activity_vertical_margin"
tools:context=".FactBookActivity"
android:background="#ff51b46d"
android:id="@+id/relativeLayout">
<TextView
android:text="@string/Did_you_know"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/TextView"
android:textSize="24sp"
android:textColor="#80ffffff" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/Fun_Facts"
android:id="@+id/factTextView"
android:layout_centerVertical="true"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:textSize="24sp"
android:textColor="#ffffff" />
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Show_Another_Fun_Fact"
android:id="@+id/showFactButton"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true"
android:background="#ffffffff"
android:textColor="#ff51b46d" />
</RelativeLayout>
//FACTS
public class Facts {
public String[] mFacts = {
"Ants stretch when they wake up in the morning."};
public String getFact() {
int i = 0;
String fact = ""; (*Variable facts is redundant?*)
fact = mFacts[i];
i++;
if (i >= 2) { //or better mFacts.length --> end of the array
i = 0; //beginns with the first fact (*The Value 0 assigned to 'i' is never used?*)
}
return fact;
}
}
Harry James
14,780 PointsHarry James
14,780 PointsHey Chandler!
Could you please provide the error log from LogCat? It helps us to get straight to the root of the problem :)