在getDrawable上出现错误

3
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);
        }
    }
};

我在“getDrawable”处遇到了错误。
getDrawable(R.drawable.test);

提示信息为:The method getDrawable(int) is undefined for the type new View.OnClickListener(){}.

如何解决?

3个回答

4
尝试这个:

试试这个:

MyActivity.this.getResources().getDrawable(R.id.test);

0

这是一个作用域问题。

假设你的类,其中包含所有这些代码,具有以下签名:

public class MyAct extends Activity

然后你必须调用

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

在 OnClickListener 内部。


我已经这样做了,但现在我得到了以下错误信息:“方法getDrawable(int)在类型MyActivity中未定义” - HunterX86

0

通过YourActivityName.this.getDrawable(R.drawable.test)来调用它。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接