getDrawable上的错误

时间:2011-05-20 16:16:35

标签: android

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

它说:对于新的View.OnClickListener(){}类型,方法getDrawable(int)是未定义的。

如何解决这个问题?

3 个答案:

答案 0 :(得分:4)

试试这个:

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

答案 1 :(得分:0)

这是一个范围问题。

假设您所有此代码所在的类具有签名:

public class MyAct extends Activity

然后你必须打电话

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

在OnClickListener内部。

答案 2 :(得分:0)

通过您的活动名称冷却它.this.getDrawable(R.drawable.test);