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

Noah Schill
Noah Schill
10,020 Points

return type?

I specified the return type for the method as void. Why am I still getting an error?

MainActivity.java
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() {
            trackButtonPress(View view);
        });

        mButton2.setOnClickListener(new View.OnClickListener() {
             trackButtonPress(View view);
        });

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

    }
}

1 Answer

Quincy Leito
Quincy Leito
20,503 Points

Take away the parameter. Like this;

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

And when you call the method in the buttons also.

trackButtonPress();

Then you're good to go.