我正在尝试创建一个检查学生字母等级的应用程序。用户从列表中选择学生的姓名,并显示姓名和字母等级。当我尝试运行程序时,我一直在崩溃。
这是我的java代码:
public class GradeChecker extends AppCompatActivity {
//Variable and object declaration
TextView name;
TextView ltrGrade;
Spinner spinner;
int nameColor = getResources().getColor(R.color.name_color);
int gradeColor = getResources().getColor(R.color.grade_color);
float textSize = getResources().getDimension(R.dimen.textsize);
BufferedReader reader;
ArrayList<String> list = new ArrayList<>();
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_grade_checker);
//Object initialization
name = (TextView) findViewById(R.id.displayName);
ltrGrade = (TextView) findViewById(R.id.displayltrGrade);
ltrGrade.setTextColor(gradeColor);
ltrGrade.setTextSize(textSize);
name.setTextSize(textSize);
spinner = (Spinner)findViewById(R.id.spinner);
//Read data from file
try {
AssetManager am = getAssets();
InputStream input = am.open("grades.txt");
reader = new BufferedReader(new InputStreamReader(input));
String line = reader.readLine().replaceAll("Name", "").replaceAll("Test1", "").replaceAll("Test2", "").replaceAll("[0-9]]", "");
while (line != null) {
list.add(line);
} input.close();
} catch (IOException e) {
name.setText("Student not found");
}
ArrayAdapter<String> dataAdapter = new ArrayAdapter<>(this,
android.R.layout.simple_spinner_item);
dataAdapter.notifyDataSetChanged();
spinner.setAdapter(dataAdapter);
spinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
@Override
public void onItemSelected(AdapterView<?> adapterView, View view, int i, long l) {
//Reading data for spinner items
try {
AssetManager am = getAssets();
InputStream input = am.open("grades.txt");
reader = new BufferedReader(new InputStreamReader(input));
String line = reader.readLine();
String na = reader.readLine().replaceAll("[0-9]", " ");
String gr = reader.readLine().replaceAll("[A-za-z]", " ");
String[] str = gr.split(" ");
int[] numbers = new int[str.length];
for (int x = 0; x < numbers.length; x++) {
numbers[x] = Integer.parseInt(str[i]);
Arrays.sort(numbers);
}
String total = Arrays.toString(numbers);
int avg = Integer.parseInt(total) / str.length;
while (line != null) {
name.setText(na);
if (avg >= 93) {
ltrGrade.setText("A");
}
if (avg >= 88 && avg != 93) {
ltrGrade.setText("B+");
}
if (avg >= 83 && avg != 88) {
ltrGrade.setText("B");
}
if (avg >= 78 && avg != 83) {
ltrGrade.setText("C+");
}
if (avg >= 73 && avg != 78) {
ltrGrade.setText("C");
}
if (avg >= 65 && avg != 72) {
ltrGrade.setText("D");
}
if (avg < 65) {
ltrGrade.setText("F");
}
} input.close();
} catch (IOException e) {
name.setText("Student not found");
}
}
@Override
public void onNothingSelected(AdapterView<?> adapterView) {
}
});
}
}
非常感谢任何帮助。