开发者

android java setOnClickListener error

开发者 https://www.devze.com 2023-03-21 07:38 出处:网络
I am trying to perform a routine when the btnExecute is pressed.I get an error at each onClick btnxxx.setOnClickListener stating \"The type new View.OnClickListener(){} must implement the inherited ab

I am trying to perform a routine when the btnExecute is pressed. I get an error at each onClick btnxxx.setOnClickListener stating "The type new View.OnClickListener(){} must implement the inherited abstract method View.OnClickListener.onClick(View). Please help me understand what's wrong.

package com.androidbook.triviaquiz8;

import android.os.Bundle;
import android.os.Environment;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Spinner;
import android.widget.SpinnerAdapter;

public class QuizHelpActivity extends QuizActivity {
    private EditText m1_sqs1;
    private EditText m1_rs1;
    private EditText m1_rs2;
    private EditText m1_cd;
    private EditText m1_els1;
    private EditText m1_els2;

    private EditText m1_sa_in;
    private EditText m1_sa_ft;
    private EditText m1_sa_mm;
    private EditText m1_sa_m;

    private EditText m1_ed_in;
    private EditText m1_ed_ft;
    private EditText m1_ed_mm;
    private EditText m1_ed_m;

    private Spinner m1_sqs1_spinner;
    private Spinner m1_rs1_unit;
    private Spinner m1_rs2_unit;
    private Spinner m1_cd_unit;
    private Spinner m1_els1_unit;
    private Spinner m1_els2_unit;

    private QuizHelpActivity mContext;

    private Button btnSave;
    private Button btnClear;
    private Button btnExecute;


    // 
    // onCreate - let the fun begin!
    // 

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        mContext = this;
        setContentView(R.layout.help);

        m1_sqs1 = (EditText) findViewById(R.id.m1_sqs1);
  开发者_StackOverflow中文版      m1_rs1 = (EditText) findViewById(R.id.m1_rs1);
        m1_rs2 = (EditText) findViewById(R.id.m1_rs2);
        m1_cd = (EditText) findViewById(R.id.m1_cd);
        m1_els1 = (EditText) findViewById(R.id.m1_els1);
        m1_els2 = (EditText) findViewById(R.id.m1_els2);

        m1_sa_in = (EditText) findViewById(R.id.m1_sa_in);
        m1_sa_ft = (EditText) findViewById(R.id.m1_sa_ft);
        m1_sa_mm = (EditText) findViewById(R.id.m1_sa_mm);
        m1_sa_m = (EditText) findViewById(R.id.m1_sa_m);

        m1_ed_in = (EditText) findViewById(R.id.m1_ed_in);
        m1_ed_ft = (EditText) findViewById(R.id.m1_ed_ft);
        m1_ed_mm = (EditText) findViewById(R.id.m1_ed_mm);
        m1_ed_m = (EditText) findViewById(R.id.m1_ed_m);

        // Create an OnClick Event in each button.

        Button btnExecute = (Button) findViewById(R.id.btnExecute);
        Button btnSave = (Button) findViewById(R.id.btnSave);
        Button btnClear = (Button) findViewById(R.id.btnClear);

        btnSave.setOnClickListener(new OnClickListener() {
            public void onClick(View arg0) {
            }
        });
        btnClear.setOnClickListener(new OnClickListener() {
            public void onClick(View arg0) {
            }
        });
        btnExecute.setOnClickListener(new OnClickListener() {
            private AlertDialog show;

            public void onClick(View arg0) {
                if ((m1_sqs1.getText().length() == 0)
                        || (m1_sqs1.getText().toString() == " ")) {
                    show = new AlertDialog.Builder(mContext).setTitle("Error")
                            .setMessage("The Square Side length is empty")
                            .setPositiveButton("OK", null).show();
                } else if (operator.getText().equals("")) {
                    show = new AlertDialog.Builder(mContext).setTitle("Error")
                            .setMessage(" is null").setPositiveButton(
                                    "OK", null).show();
                }
                {
                    double m1_sa_in = new Double(m1_sqs1.getText().toString())
                            * new Double(m1_sqs1.getText().toString());
                    m1_sa_in.setText(Double.toString(m1_sa_in));
                }
            }
        }


Where you have

btnSave.setOnClickListener(new OnClickListener() {   

Change that for

btnSave.setOnClickListener(new View.OnClickListener() {   

It's just the newer way, so if you are following an older tutorial or blog post, that's why it would be outdated.

Edit:

import android.view.View.OnClickListener;

View view = findViewById(android.R.id.content);
    view.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View v) {
            // do something
        }
    });


Your imports list doesn't include android.view.View, just android.view.View.OnClickListener. Without knowledge of the View object that is a parameter of onClick(), the compiler can't resolve that the implementation is complete. Personally, I would fix it by:

  1. Change (don't add) the import from android.view.View.OnClickListener to android.view.View
  2. Update your declaration as citizen conn mentioned.


I believe you can also do this:

public class QuizHelpActivity extends QuizActivity implements OnClickListener {

adding implements OnClickListener will let you use this. For example, in your onCreate, you can have:

    Button btnExecute   = (Button) findViewById(R.id.btnExecute);
    Button btnSave      = (Button) findViewById(R.id.btnSave);
    Button btnClear     = (Button) findViewById(R.id.btnClear);

    btnSave.setOnClickListener(this);
    btnClear.setOnClickListener(this);
    btnExecute.setOnClickListener(this);

Then you can implement the onClick function after onCreate, like so:

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    // the rest of your onCreate, including buttons
}

public void onClick(View v) {
    switch(v.getId()) {
      case R.id.btnExecute:
          // reference a function that would house your current onClick behavior, such as:
          execute();
          break;
      case R.id.btnSave:
          break;
      case R.id.btnClear:
          break;
    }
}

I think this is a cleaner approach with fewer nested functions.


try this

btnSave.setOnClickListener(new OnClickListener() {                           
            public void View.OnclickListener.onClick(View arg0) {
                }
            });


In this case it's maybe a better way to set an OnClick in your XML-file.

Select your 3 buttons, which have an onClickListener, in your XML-layout file. Right click on it > Properties > OnClick and for the 3 buttons give the OnClick a name (ex. onClickButtons). So, in your java code you can do like this:

public void onClickButtons(View v) {
switch (v.getId()) {
case R.id.R.id.btnExecute:
    ...
    break;
case R.id.btnSave:
    ...
    break;
case R.id.btnClear:
    ...
    break;
}

}

And let your class implement the OnClickListener.

For your AlerDialog, this is a better way to handle this:

new AlertDialog.Builder(this)
.setTitle("Error")
.setMessage("is null")
.setCancelable(false)
.setPositiveButton(
    "OK", new DialogInterface.OnClickListener() {

            public void onClick(DialogInterface dialog, int which) {


            }
        })
    .show();

Greets and have nice development!


First import the following package:

import android.view.View.OnClickListener;

Then change the following like this:

btnSave.setOnClickListener(new View.OnClickListener() {

    @Override
    public void onClick(View v) {
        // TODO Auto-generated method stub
    }

});


You can put seem This

public class MainActivity2 extends Activity implements View.OnClickListener{
Button B2;
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    B2=(Button) findViewById(R.id.BM2);
    B2.setOnClickListener(this);

}

@Override
public void onClick(View v) {
// Do anything
}


Use this:

button1.setOnClickListener((OnClickListener) this);

Do not use this:

button1.setOnClickListener(this);
0

精彩评论

暂无评论...
验证码 换一张
取 消