Matt
Matt

Reputation: 81

Error when trying to use OnClickListener

I'm getting an error in my code when trying to use OnClickListener and the OnClick method in android studio. Specifically the error on OnClickListener is

"Class 'Anonymous class derived from OnClickListener' must either be declared abstract or implement abstract method 'onClick(View)' in 'OnClickListener'"

I'm confused because I am using OnClick(View), and I'm using it the same way I've used it in the past (without problems). Also OnClick says it is never used. Thanks in advance for any help you can provide. My code is as follows:

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




                }
            });

    }
}

Upvotes: 0

Views: 1093

Answers (1)

Sheychan
Sheychan

Reputation: 2436

It is because of your method name, you should change

OnClick()

on to

 onClick()

then put the @Override annotation on top of it... unimplemented methods should be implemented

getGrade.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {

            }
       });

Upvotes: 1

Related Questions