java - 尝试使用 OnClickListener 时出错

标签 java android android-studio onclicklistener

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

"Class 'Anonymous class derived from OnClickListener' must either be declared abstract or implement abstract method 'onClick(View)' in '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]);
                    }




                }
            });

    }
}

最佳答案

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

OnClick()

前往

 onClick()

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

getGrade.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {

            }
       });

关于java - 尝试使用 OnClickListener 时出错,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33005887/

相关文章:

java - 我应该将 Bean 验证与 EJB 3.1、JSF2.0 和 JPA 一起使用吗?

Android 仅使用支持库中的 ViewPager

android - 在 Eclipse 和 IDEA 中使用 gradle 项目

java - Maven AppAssembler 找不到类

java - proguard 解析 android.txt 文件中的异常

java - Android:TextInputLayout 不会将提示居中

android - 如何将数据从 Activity 发送到 AlertDialog 以显示它们

android - 方法 doInBackground 不会从父类(super class)中覆盖

java - “调用需要 API 级别 23”错误,但 API 1 的 FrameLayout 上存在 getForeground()

java.io.FileNotFoundException :/storage/emulated/0/Android/data/MyApplication/MyFile. ICS。 (没有这样的文件或目录)/Android Studio/Ical4j