Button.setOnClickListener(listener)上的错误;

时间:2011-05-20 08:37:00

标签: 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);

ServiceButtonLeft.setOnClickListener(listener);
ServiceButtonRight.setOnClickListener(listener);

View.OnClickListener listener = 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);
        }
    }
};

我在这两行上出错:在“听众”一词

ServiceButtonLeft.setOnClickListener(listener);
ServiceButtonRight.setOnClickListener(listener);

4 个答案:

答案 0 :(得分:0)

 public class MyActivity extends Activity implements OnclickListener{
    private int ServiceLeft;
    private int ServiceRight;
    private int Services;

@Override
public void onCreate(Bundle savedInstance){
super(savedInstance);
setContent(R.layout.main);
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);

//set Listeners for your buttons
ServiceButtonLeft.setOnClickListener(this);
ServiceButtonRight.setOnClickListener(this);
 }
@Override
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);
}

} }

答案 1 :(得分:0)

尝试使用后,您可以创建“监听器”。试试这种方式。

View.OnClickListener listener = 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);
    }
}
};

ServiceButtonLeft.setOnClickListener(listener);
ServiceButtonRight.setOnClickListener(listener);

答案 2 :(得分:0)

在变量创建之前不可能使用(甚至引用)变量。你需要使用“listener”移动这两行

ServiceButtonLeft.setOnClickListener(listener);
ServiceButtonRight.setOnClickListener(listener);

在您创建它之后,即您在上面提供的代码段中的最后一行。然后它会工作。

答案 3 :(得分:0)

试试这个:

public OnClickListener listener = new OnClickListener() {
    @Override
    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);
    }
}
};

已编辑的部分: 尝试使用以下代码替换您的问题代码:

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

OnClickListener listener;     //This line is the only change to your code.
ServiceButtonLeft.setOnClickListener(listener);
ServiceButtonRight.setOnClickListener(listener);

View.OnClickListener listener = 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);
        }
    }
};