1

I'm trying to make Android onClick() method easier to read, so I wrap it like this

public void click(Button bt) {
    bt.setOnClickListener(new View.OnClickListener() {
        public void onClick(View v) {}
    });
}

When I try to run it like this

button = (Button) findViewById(R.id.btn1);

click(button) {
    @Override
    public void onClick(View v) {
        Log.v("hi", "test");

    }
}

or

button = (Button) findViewById(R.id.btn1);

click(button) {
    @Override
    public void onClick(View v) {
        Log.v("hi", "test");

    }
};

or

button = (Button) findViewById(R.id.btn1);

click(button) {
    @Override
    public void onClick(View v) {
        Log.v("hi", "test");

    };
};

it gives the following errors:

[error] /home/retok/proj/src/com/hellow/app/HelloActivity.java:32: ';' expected
[error] /home/retok/proj/src/com/hellow/app/HelloActivity.java:34: ';' expected
[error] /home/retok/proj/src/com/hellow/app/HelloActivity.java:34: ';' expected
[error] (compile:compileIncremental) javac returned nonzero exit code

Why is this? As you can see I put ; where needed. And I can't put ; after public void onClick(View v) { and click(button) { because that would close the method and give error anyway.

Magnilex
  • 11,584
  • 9
  • 62
  • 84
  • 1
    Not sure, but aren't you trying to override a method which is part of an anonymous class created by `new View.OnClickListener()`. – mins May 03 '15 at 12:50
  • @mins That might be it.. however I don't know if there's a way to do it other ways. That's why I wrapped it in a method to begin with. I have like 25 items I need to listen for clicks. – Reto Koradi May 03 '15 at 12:55
  • I'm not sure what you think will be "easier to read". Basically, your new method name hides what it is doing. But it's for sure that this is not valid Java syntax. Question is, what exactly do you want to achieve? Do you want to rid yourself of having to write `new`? Or what else is bothering you? – class stacker May 05 '15 at 13:21

2 Answers2

0

use this :

click(button) {
    @Override
    public void onClick((View v) {
        Log.v("hi", "test");

    }
});
Meysam
  • 694
  • 6
  • 20
  • now I'm getting `[error] /home/retok/proj/src/com/hellow/app/HelloActivity.java:32: ';' expected` `[error] /home/retok/proj/src/com/hellow/app/HelloActivity.java:34: ';' expected` `[error] /home/retok/proj/src/com/hellow/app/HelloActivity.java:34: ';' expected` `[error] /home/retok/proj/src/com/hellow/app/HelloActivity.java:37: illegal start of expression` – Reto Koradi May 03 '15 at 12:48
0

You have an error here:

click(button) {
    @Override

click(button)is seen as a method call, the rest of the line is ignored at the time, but the call lacks a ;.

It seems you are trying to override an method part of an anonymous class created by new View.OnClickListener(), but this is not possible / allowed.

My understanding is that you want to have a single button handler for multiple buttons. This is indeed possible, but you don't have the correct approach.

From this question, this non-selected answer shows how to handle multiple buttons in a single location:

@Override
public void onCreate(Bundle savedInstanceState) {
        button1.setOnClickListener(onClickListener);
        button2.setOnClickListener(onClickListener);
        button3.setOnClickListener(onClickListener);
}

private OnClickListener onClickListener = new OnClickListener() {
     @Override
     public void onClick(final View v) {
         switch(v.getId()){
             case R.id.button1:
                  //DO something
             break;
             case R.id.button2:
                  //DO something
             break;
             case R.id.button3:
                  //DO something
             break;
         }

   }
};

(you don't need the switch if you really want to process any button the same way, which seems rather improbable).

Community
  • 1
  • 1
mins
  • 6,478
  • 12
  • 56
  • 75