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 Implementing Designs for Android Customizing a ListView for the Inbox Fun with Refactoring

Adam Lewandowski
Adam Lewandowski
6,763 Points

"Fun with Refactoring"

So I have finished the Ribbit app, but at the time I could not figure out why this code challenge was not correct. Looking back at it.. I am still not sure why it isn't correct. My OCD does not like that unfilled bar in my track completion!

The error says "Bummer! Unknown error occurred - please try again or contact Treehouse help." and rather than the preview showing the usual 'compiler' errors, it does not show anything useful.

Am I missing something obvious? Or has anyone else run into this problem?

 @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        mButton1 = (Button)findViewById(R.id.button1);
        mButton2 = (Button)findViewById(R.id.button2);

        trackButtonPress();
    }

    public void trackButtonPress() {
            mButton1.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Toast.makeText(MainActivity.this, "A button was pressed", Toast.LENGTH_LONG).show();
                Log.i(TAG, "A button was pressed");
            }
        });

        mButton2.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Toast.makeText(MainActivity.this, "A button was pressed", Toast.LENGTH_LONG).show();
                Log.i(TAG, "A button was pressed");
            }
        });
    }

2 Answers

Ben Jakuben
STAFF
Ben Jakuben
Treehouse Teacher

You still need to attach OnClickListeners to each button. (You could use the same onClickListener for both, but that's not how this challenge is setup.) All you need to do is refactor the code inside the onClick() methods into a new method. Hope this helps!

Adam Lewandowski
Adam Lewandowski
6,763 Points

That was it.. silly me. Thank you for the reply.

Looking forward to your next course/project!

adnanalvee2
adnanalvee2
6,862 Points

Could u explain what you did after this, Im sort of confused.

Ben Jakuben
Ben Jakuben
Treehouse Teacher

In this code challenge we are simply trying to remove the code that is duplicated. Notice that the two lines in each onClick listener are exactly the same. What you need to do is make a new method that runs those two lines of code, and then call that new method inside each onClick listener.

This might seem like a silly example with only two lines of code, but imagine we have 10 or 100 lines of code that are exactly the same in two places. Then you can see how having one method for all those lines of code can be better organized than having the same code all over the place.

adnanalvee2
adnanalvee2
6,862 Points

Thanks a lot Ben, I got it. I often get what to do but get confused how to organize the code though I have taken courses on Java.

This is what I got, but yeah the coding is wrong, could you assist me.

public class MainActivity extends Activity {

    public static final String TAG = MainActivity.class.getSimpleName();

    public Button mButton1;
    public Button mButton2;

             @Override

    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        mButton1 = (Button)findViewById(R.id.button1);
        mButton2 = (Button)findViewById(R.id.button2);

        trackButtonPress();
    }

    public void trackButtonPress() {
                       @Override
            public void onClick(View view) {
                Toast.makeText(MainActivity.this, "A button was pressed", Toast.LENGTH_LONG).show();
                Log.i(TAG, "A button was pressed");
            }

        }

      mButton1.setOnClickListener(new View.OnClickListener() {        
            public void trackButtonPress()
            }

    }


        mButton2.setOnClickListener(new View.OnClickListener() {          
            public void trackButtonPress()
            }

    }
Ben Jakuben
Ben Jakuben
Treehouse Teacher

Okay, this code has 3 declarations of trackButtonPress() (public void trackButtonPress()), but you can only have one. You need to add one definition at the end and then call it in each onClickListener. Give it a shot, and if you still have trouble, post your new code in a new question and tag me with "@Ben Jakuben" to take a look at it.

adnanalvee2
adnanalvee2
6,862 Points

Here is what I have, it should be ok now but It doesn't specify any error except the message "Don't forget to add the new method to the MainActivity class!" I moved the method here and there, nothing happens. Ben Jakuben;

public class MainActivity extends Activity {

    public static final String TAG = MainActivity.class.getSimpleName();

    public Button mButton1;
    public Button mButton2;

      public void treehousebutton() {       
        Toast.makeText(MainActivity.this, "A button was pressed", Toast.LENGTH_LONG).show();
        Log.i(TAG, "A button was pressed");

}


    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);



        mButton1 = (Button)findViewById(R.id.button1);
        mButton2 = (Button)findViewById(R.id.button2);

        mButton1.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                treehousebutton();
            }
        });

        mButton2.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                treehousebutton();
            }
        });
    }


}


}```
Ben Jakuben
Ben Jakuben
Treehouse Teacher

The new method must be named trackButtonPress() to pass. Rename treehousebutton() to trackButtonPress() and you should be good. :)

adnanalvee2
adnanalvee2
6,862 Points

haha...how could I change the method name..strange! Don't even remember lol. Thanks Ben Jakuben . You are the best.