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
Ginger Ranslam
8,156 Pointshelp with code challenge: fun with refactoring.
I've tried several different ways to do this, but they are all guesses and when I can get the errors down to just a few syntax errors I still cannot figure out where I am going wrong. So some help would be greatly appreciated.
Challenge task 1 of 1 In the spirit of refactoring, refactor the code inside the two onClick() methods into a common method named trackButtonPress(). Make it a public method of MainActivity.java.
Here is my code.
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.Toast;
public class MainActivity extends Activity {
public static final String TAG = MainActivity.class.getSimpleName();
public Button mButton1;
public Button mButton2;
/*
* Some code has been omitted for brevity
*/
@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);
public MainActivity.class.trackButtonPress{
mButton1.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
trackButtonPress();
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) {
trackButtonPress();
Toast.makeText(MainActivity.this, "A button was pressed", Toast.LENGTH_LONG).show();
Log.i(TAG, "A button was pressed");
}
});
}
}
}
1 Answer
Daniel Hartin
18,106 PointsHi Ginger
You have the right idea adding the method call inside the onClick() method however you haven't declared the method anywhere. You also need to remove the code inside the 2 onClick() methods and paste it inside your new method. This new method should only be part of the class not declared inside any other methods it should be given the qualifiers public and void as it won't be returning a value.
Hopefully this makes it clear, I have pasted the edited code below although I haven't tried it so let me know if there is any problems.
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.Toast;
public class MainActivity extends Activity {
public static final String TAG = MainActivity.class.getSimpleName();
public Button mButton1;
public Button mButton2;
/*
* Some code has been omitted for brevity
*/
@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) {
trackButtonPress();
}
});
mButton2.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
trackButtonPress();
}
});
}
public void trackButtonPress(){
Toast.makeText(MainActivity.this, "A button was pressed", Toast.LENGTH_LONG).show();
Log.i(TAG, "A button was pressed");
}
}
Hope this helps
Daniel
Ginger Ranslam
8,156 PointsGinger Ranslam
8,156 PointsYes this worked, and it makes so much more sense what I was actually refactoring thank you!
Daniel Hartin
18,106 PointsDaniel Hartin
18,106 PointsGlad it worked for you!
MUZ1407800 Doubt Milazi
11,813 PointsMUZ1407800 Doubt Milazi
11,813 Pointsthank u it worked olso for me