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

James N
James N
17,864 Points

i don't get "getFact" in my auto complete

when i type String fact = mFactBook. autocomplete pops up but the teacher said that there would be a "getFact" option so i type it in manually and i get an error! here is my code for FunFactsActivity.java:

package james.funfacts;

import android.app.Activity;

import android.os.Bundle;

import android.widget.Button;
import android.widget.TextView;
import android.view.View;



public class FunFactsActivity extends Activity {

    private FactBook mFactBook = new FactBook();


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_fun_facts);


        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();

                factLabel.setText("");
            }

        };
        showFactButton.setOnClickListener(listener);
    }
}

here is my code for FactBook.java:

package james.funfacts;
import android.view.View;
import java.util.Random;

public class FactBook {

            public void onClick(View view) {
                String[] facts = {
                        "The wireless controller only charges over usb, not communicates.",
                        "Your xbox controller will work on a computer or android device.(like the one you are using now!)",
                        "The free xbox account is called 'xbox live silver'.",
                        "You can copy cds to your xbox.",
                        "The xbox kinect can track how many calories you burn.",
                        "Your kinect play space does not have to be 6-10 feet.",
                        "The rotating d-pad can be used in an X position.",
                        "You need to pay for xbox live gold to play online multiplayer.",
                        "You cannot use an xbox 360 controller on xbox one.",
                        "The xbox 360 is compatible with original xbox games.

                };

                String fact;
                Random randomGenerator = new Random();
                int randomNumber = randomGenerator.nextInt(facts.length);

                fact = facts[randomNumber];
    }
}

as you can see i put in my own custom facts. can someone please help me?

1 Answer

Your error is very straight forward. You are on the right path, but just small error

In your class FactBook.java you have defined the method called OnClick (View view)

But somehow in your main class i.e. FunFactsActivity.java you have called getFact method.

Since there is no method called getFact in FactBook, its obvious it wont show.

Just change the onClick method in FactBook.java i.e.

public class FactBook {

            public void onClick(View view) {
                String[] facts = {
// ... other code

//change the above to 
public class FactBook {

            public String getFact() {
                String[] facts = {
// ... other code

Change this and it will work fine else let us know if it doesn't