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 trialEric Wolkoff
4,475 PointsHelp Errors in fun fact code. Not sure why please help
In Android Studio, I cant get the emulator to run funfact app. Im getting these errors in my code. When watching the video these things do not happen to Ben. Does it matter that Im using an ASUS and he is using a mac? see below the errors I need help to fix errors and get back on track. Thanks
Error:(16, 13) error: cannot find symbol class FactBook Error:(16, 38) error: cannot find symbol class FactBook Error:(21, 32) error: cannot find symbol variable activity_fun_facts Error:Execution failed for task ':app:compileDebugJava'.
Compilation failed; see the compiler error output for details. Information:BUILD FAILED see my code below package com.webdzine4u.eric.funfacts;
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;
import java.util.Random;
public class FunFactActivity extends Activity {
private FactBook mFactBook = new FactBook();
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_fun_facts);
//Declare our View variables and assign the Views from the layout file
final TextView factLabel = (TextView) findViewById(R.id.factTextView);
Button showFactButton = (Button) findViewById(R.id.showFactButton);
View.OnClickListener listener = new View.OnClickListener() {
@Override
public void onClick(View view) {
String fact = mFactBook.getFact();
// Update the label with our dynamic fact
factLabel.setText(fact);
}
};
showFactButton.setOnClickListener(listener);
}
}
8 Answers
Kate Hoferkamp
5,205 PointsYeah sure, just ignore the ColorWheel things, they are for the next step in the App.
Here's FunFactActivity.java:
package com.example.peter.funfacts;
import android.app.Activity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;
public class FunFactsActivity extends Activity {
public static final String TAG = FunFactsActivity.class.getSimpleName();
private FactBook mFactBook = new FactBook();
private ColorWheel mColorWheel = new ColorWheel();
public FunFactsActivity() {
super();
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_fun_facts);
//Declare View Variables and Assign them the Views from the layout file
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() {
@Override
public void onClick(View view) {
//Update factLabel with dynamic fact
factLabel.setText(mFactBook.getFact());
int color = mColorWheel.getColor();
relativeLayout.setBackgroundColor(color);
showFactButton.setTextColor(color);
}
};
showFactButton.setOnClickListener(listener);
//Display a message to user when activity is created that disappears
Toast.makeText(this, "Yay! Our activity was created.", Toast.LENGTH_LONG).show();
//Display a message to log when activity is created
Log.d(TAG,"We're logging from the onCreate() method");
}
}
I believe the Toast and Log were eventually deleted from the project, but I kept them for reference.
Here is the FactBook.java
package com.example.peter.funfacts;
import java.util.Random;
public class FactBook {
//Member Variables
public 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
public String getFact(){
//Update fact label with new fact
String fact = "";
//Randomly select a fact
Random randomGenerator = new Random();
int randomNumber = randomGenerator.nextInt(mFacts.length);
//Convert random number to text fact
fact = mFacts[randomNumber];
return fact;
}
}
Hope that helps, let me know if you need any help!
Stone Preston
42,016 Pointsis your class named FactBook? check your FactBook.java file and see how you spelled it. you may have named it Factbook with a lowercase b or something
Eric Wolkoff
4,475 Pointsok. so I manually typed import FactBook. Please see below. FactBook still shows up in red and when I hover the mouse over FactBook the message says" cannot resolve the symbol 'FactBook'....see below
package com.webdzine4u.eric.funfacts;
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;
import java.util.Random;
import FactBook;
public class FunFactActivity extends Activity {
private FactBook mFactBook = new FactBook();
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_fun_facts);
//Declare our View variables and assign the Views from the layout file
final TextView factLabel = (TextView) findViewById(R.id.factTextView);
Button showFactButton = (Button) findViewById(R.id.showFactButton);
View.OnClickListener listener = new View.OnClickListener() {
@Override
public void onClick(View view) {
String fact = mFactBook.getFact();
// Update the label with our dynamic fact
factLabel.setText(fact);
}
};
showFactButton.setOnClickListener(listener);
}
}
Eric Wolkoff
4,475 Pointshere is the FactBook.java file code
package com.webdzine4u.eric.funfacts;
/**
-
Created by Eric on 9/5/2014. */ public class FactBook { //member variables public 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." }; //method (abilities: things the object can do)
public String getFact(){
String fact = ""; // Randomly select a fact Random randomGenerator = new Random(); //Construct a new Random number generator int randomNumber = randomGenerator.nextInt(mFacts.length); fact = mFacts[randomNumber]; return fact;
} }
Kate Hoferkamp
5,205 PointsYou should not have to import FactBook since it is a public class that is already in your package source.. That may solve a problem or two.
In fact, these are the only imports you should need for your FunFactsActivity.java:
import android.app.Activity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;
Also, The Random import you have on your FunFactActivity.java file should actually be on your FactBook.java file since that is what is using the Random function.
I can show you my full code, if you think that may help you as well...
Stone Preston
42,016 Pointsok I was wrong about the import. since they are in the same package folder you shouldnt have to import it. so remove the import line and make sure your FactBook class is in the same folder as FunFactsActivity.
Eric Wolkoff
4,475 PointsCan I see your full code for both FunFactActivity.java and FactBook.java. I tried hovering over all items in red and pushing alt + enter hoping that would fix this mess but it started asking me to create a layout resource. Im not sure what to do here. Is there a way to fix my code doing it that way.
Charles Titherley
1,006 PointsHi. I had the same problem and found it was simply because i had mistakenly created the FactBook folder in the FunFacksActivity folder. When i moved it into the com.teamTreehouse.funfact folder the problem was fixed.