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 Build a Simple Android App (2014) Improving Our Code Simple Refactoring: Using a Class

Vinay Mishra
Vinay Mishra
5,496 Points

The new code after refactoring is not working.

I revised the code according to this video, however, now the code is not working and is showing 3 errors.

The 3 errors are:

Error:(10, 13) error: cannot find symbol class Factbook

Error:(42, 31) error: cannot find symbol variable mFactBook

This option was provided to me:

  • Try: Run with --stacktrace option to get the stack trace. Run with --info or --debug option to get more log output.

My code for funfactsactivity.java is

''' import android.os.Bundle; import android.support.design.widget.FloatingActionButton; import android.support.design.widget.Snackbar; import android.support.v7.app.AppCompatActivity; import android.support.v7.widget.Toolbar; import android.view.View; import android.view.Menu; import android.view.MenuItem; import android.widget.TextView; import android.widget.Button;

import java.util.Random;

public class FunFactsActivity extends AppCompatActivity {

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_fun_facts);
    Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
    setSupportActionBar(toolbar);

    FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab);
    fab.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {

            Snackbar.make(view, "Replace with your own action", Snackbar.LENGTH_LONG)
                    .setAction("Action", null).show();
        }
    });

    //Declare our view variables and assign the views from the layout files
    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);
}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.menu_fun_facts, menu);
    return true;
}

} '''

My code for factbook.java is

''' import java.util.Random;

public class FactBook {

private Factbook mFactBook = new FactBook();

//Member variables (properties about the object)

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.",
        "Tree is actually not in a tree"};

//Method (abilities: things the objects 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;

}

} '''

5 Answers

If you refactored the code you may just have to rebuild/clean the project. You should try this if you haven't already done so.

Vinay Mishra
Vinay Mishra
5,496 Points

Hi Luka - I have already done that and still no success.

Hmm...maybe try to import the Factbook class to your Main Activity.

Vinay Mishra
Vinay Mishra
5,496 Points

Could you please guide me as to how do i import that. Thanks for your help.

Scratch what i said early, you need to declare your variable for this activity. above your @ Override onCreate() method trying inserting this line:

private FactBook mFactBook = new FactBook();

Vinay Mishra
Vinay Mishra
5,496 Points

Thanks. One error has now disappeared. However, this error still remains:

"Error:(10, 13) error: cannot find symbol class Factbook"

Thank you for helping and bearing with my questions. I am new to Android and Java and hence these questions.

I believe in your FactBook.java class you shouldn't have the: private Factbook mFactBook = new FactBook();

I'm not sure if this is the issue but it may help.

I believe in your FactBook.java class you shouldn't have the: private Factbook mFactBook = new FactBook();

I'm not sure if this is the issue but it may help.