我尝试在Android studio中使用OnClickListener和OnClick方法时,我的代码出错了。具体来说,OnClickListener上的错误是
" Class'从OnClickListener派生的匿名类'必须是 声明抽象或实现抽象方法' onClick(查看)'在 ' 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]);
}
}
});
}
}
答案 0 :(得分:1)
由于您的方法名称,您应该更改
OnClick()
到
onClick()
然后将@Override注释放在它上面......应该实现未实现的方法
getGrade.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
}
});