Error in getDrawable

private int ServiceLeft;
private int ServiceRight;
private int Services;

final TextView countTextViewPlusL = (TextView) findViewById(R.id.TextViewCountL);
final Button ServiceButtonLeft = (Button) findViewById(R.id.ButtonServiceLeft);
final TextView countTextViewPlusR = (TextView) findViewById(R.id.TextViewCountR);
final Button ServiceButtonRight = (Button) findViewById(R.id.ButtonServiceRight);

View.OnClickListener listner = new View.OnClickListener() {
    public void onClick(View v) {
        switch(v.getId()) {
           case R.id.ButtonServiceRight:
                ServiceRight++;
                break;
           case R.id.ButtonServiceLeft:
                ServiceLeft++;
                break;
        }
        if(Services % 2 == 0) {
            getDrawable(R.drawable.test);
        }
    }
};

I get an error "getDrawable"

getDrawable(R.drawable.test);

he says: The getDrawable (int) method is undefined for the type new View.OnClickListener () {}.

how to solve it?

+3
source share
3 answers

Try the following:

MyActivity.this.getResources().getDrawable(R.id.test);
+4
source

This is a problem with the area.

Suppose your class that contains all this code has a signature:

public class MyAct extends Activity

Then you have to call

MyAct.this.getDrawable (R.drawable.test);

Being inside OnClickListener.

0
source

Call it through your name: YourActivityName.this.getDrawable (R.drawable.test);

0
source

All Articles