【问题标题】:Error when trying to use OnClickListener尝试使用 OnClickListener 时出错
【发布时间】:2015-10-08 03:03:24
【问题描述】:

尝试在 android studio 中使用 OnClickListener 和 OnClick 方法时,我的代码出现错误。特别是 OnClickListener 上的错误是

"Class '从 OnClickListener 派生的匿名类'必须是 在中声明抽象或实现抽象方法“onClick(View)” 'OnClickListener'"

我很困惑,因为我使用的是 OnClick(View),而且我使用它的方式与我过去使用它的方式相同(没有问题)。 OnClick 还说它从未使用过。提前感谢您提供的任何帮助。我的代码如下:

import android.app.AlertDialog;
import android.content.res.AssetManager;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.Spinner;
import android.widget.TextView;

import java.io.BufferedReader;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

public class App1Act1 extends AppCompatActivity {
    String storage;
    int[] testAverages;
    int[] testScores;
    String[] studentNames;
    String[] arrayStorage;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_app1_act1);



        /*for(i=0; i<11; i++){
            studentNames[i] = inputT.split(" "); */
        //error message
        AlertDialog.Builder dlgAlert = new AlertDialog.Builder(this);
        //StringTokenizer
        AssetManager am = getAssets();

        try {
            InputStream inputT = am.open("grades.txt");
            InputStreamReader inputStreamReader = new InputStreamReader(inputT);
            BufferedReader bufferedReader = new BufferedReader(inputStreamReader);
            storage = " ";
            StringBuilder stringBuilder = new StringBuilder();

            while (( (storage = bufferedReader.readLine())) !=null) {
                stringBuilder.append(inputT);
            }

            inputT.close();
            }



        catch(FileNotFoundException e) {

            dlgAlert.setMessage("File was not found, please import file and try again.");
            dlgAlert.setTitle("Error Message...");
            dlgAlert.setPositiveButton("OK", null);
            dlgAlert.setCancelable(true);
            dlgAlert.create().show();
        }

        catch(IOException e){
            dlgAlert.setMessage("Oops!  Something happened!"); //in the tradition of windows 10
            dlgAlert.setTitle("Error Message...");
            dlgAlert.setPositiveButton("OK", null);
            dlgAlert.setCancelable(true);
            dlgAlert.create().show();
        }

        finally {


        }

    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.menu_app1_act1, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        int id = item.getItemId();

        //noinspection SimplifiableIfStatement
        if (id == R.id.action_settings) {
            return true;
        }

        return super.onOptionsItemSelected(item);
    }

    @Override
    protected void onStart () {
        super.onStart();
        arrayStorage = storage.split("\\s+");

        //array to store names
        for (int i=0, s=0; i <= 59; i+=6, s++) {

            studentNames[s] = arrayStorage[i] + arrayStorage[i+1];
        }
        //parse string scores to in array
        for(int i=2, p=0; i<=59; i+=6, p+=4) {
            testScores[p] = Integer.parseInt(arrayStorage[i]);
            testScores[p+1] = Integer.parseInt(arrayStorage[i+1]);
            testScores[p+2] = Integer.parseInt(arrayStorage[i+2]);
            testScores[p+3] = Integer.parseInt(arrayStorage[i+3]);
        }
        //calculate and store student averages

        for(int i=0, p=0; i<=59; i+=4, p++) {
            testAverages[p] = (testScores[i] + testScores[i+1] + testScores[i+2] +testScores[i+3]) / 4;
        }

        List<String> spinnerArray = new ArrayList<String>(Arrays.asList(studentNames));

        ArrayAdapter<String> adapter = new ArrayAdapter<String>(
                this, android.R.layout.simple_spinner_item, spinnerArray);

        adapter.setDropDownViewResource(android.R.layout.simple_spinner_item);
        final Spinner sItems = (Spinner) findViewById(R.id.spinner);
        sItems.setAdapter(adapter);

        final TextView textView = (TextView) findViewById(R.id.textView);

        //set up button for getting grade
        Button getGrade = (Button) findViewById(R.id.button);
            getGrade.setOnClickListener(new View.OnClickListener() {

                String selected = sItems.getSelectedItem().toString();

                public void OnClick(View v) {
                    if (selected.equals(studentNames[0])) {
                        textView.setText(testAverages[0]);
                    }
                    else if (selected.equals(studentNames[1])) {
                        textView.setText(testAverages[1]);
                    }
                    else if (selected.equals(studentNames[2])) {
                        textView.setText(testAverages[2]);
                    }
                    else if (selected.equals(studentNames[3])) {
                        textView.setText(testAverages[3]);
                    }
                    else if (selected.equals(studentNames[4])) {
                        textView.setText(testAverages[4]);
                    }
                    else if (selected.equals(studentNames[5])) {
                        textView.setText(testAverages[5]);
                    }
                    else if (selected.equals(studentNames[6])) {
                        textView.setText(testAverages[6]);
                    }
                    else if (selected.equals(studentNames[7])) {
                        textView.setText(testAverages[7]);
                    }
                    else if (selected.equals(studentNames[8])) {
                        textView.setText(testAverages[8]);
                    }
                    else if (selected.equals(studentNames[9])) {
                        textView.setText(testAverages[9]);
                    }




                }
            });

    }
}

【问题讨论】:

标签: java android android-studio onclicklistener


【解决方案1】:

是因为你的方法名称,你应该改变

OnClick()

 onClick()

然后把@Override注解放在上面...应该实现未实现的方法

getGrade.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {

            }
       });

【讨论】:

  • 啊,这确实解决了问题,感谢 Sheychan,尽管我现在觉得自己非常愚蠢。
  • 一年前我也遇到过同样的问题,振作起来
猜你喜欢
  • 2020-07-31
  • 1970-01-01
  • 2017-07-18
  • 2017-02-08
  • 2018-12-02
  • 2020-09-07
  • 2021-09-14
  • 2019-04-09
  • 1970-01-01
相关资源
最近更新 更多