尝试在数组中查找最大值和最小值时,由于某种原因,我的应用程序崩溃了。它曾经工作过,但现在我做到了,因此数据保存在文件中。函数是否需要额外的参数才能使其正常工作?

public class MainActivity extends AppCompatActivity {
ArrayList<String> pavardes = new ArrayList<String>();
ArrayList<Integer> amziai = new ArrayList<Integer>();
ArrayList<String> issilavinimai = new ArrayList<String>();

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

    TextView rez = findViewById(R.id.rez);
    rez.setText("");

    try {
        FileInputStream fis = openFileInput("Karnauskas.12");
        InputStreamReader isr = new InputStreamReader(fis);
        BufferedReader bufferedReader = new BufferedReader(isr);

        String line = "";
        while ((line = bufferedReader.readLine()) != null) {
            String[] mas = line.split(" - ");
            if (mas.length == 3) {
                pavardes.add(mas[0]);
                amziai.add(Integer.parseInt(mas[1]));
                issilavinimai.add(mas[2]);
            }
        }
    } catch (Exception e) {
        rez.setText("Klaida");
    }


}
public void saugoti(View v) {
    EditText pavard = findViewById(R.id.pavarde);
    pavardes.add(pavard.getText().toString());

    EditText amz = findViewById(R.id.amzius);
    amziai.add(Integer.parseInt(amz.getText().toString()));

    Spinner issilav = findViewById(R.id.issilavinimas);
    issilavinimai.add(issilav.getSelectedItem().toString());

    TextView rez = findViewById(R.id.rez);
    rez.setText("Išsaugota");

    pavard.setText("");
    amz.setText("");
    issilav.setSelection(1);
}

public void kitasLangas(View v) {
    Intent duom = new Intent(this, langas2.class);
    duom.putStringArrayListExtra("pavardes", pavardes);
    duom.putIntegerArrayListExtra("amziai", amziai);
    duom.putStringArrayListExtra("issilavinimai", issilavinimai);
    startActivity(duom);
}

public void skaiciuoti(View v){
    int min = 999;
    int max = 0;
    String min_pavarde = "";
    String min_issilavinimas = "";
    String max_pavarde = "";
    String max_issilavinimas = "";
    TextView rez = findViewById(R.id.rez2);
    if (amziai.isEmpty()){
        rez.setText("No data");
    } else {
        for (int i = 0; i < amziai.size(); i++) {
            if (min > amziai.get(i)) {
                min = amziai.get(i);
                min_pavarde = pavardes.get(i);
                min_issilavinimas = issilavinimai.get(i);
            }
            if (max < amziai.get(i)) {
                max = amziai.get(i);
                max_pavarde = pavardes.get(i);
                max_issilavinimas = issilavinimai.get(i);
            }
        }
        rez.setText("MIN: "+min_pavarde+" "+min+" "+min_issilavinimas+"\n");
        rez.append("MAX: "+max_pavarde+" "+max+" "+max_issilavinimas+"\n");
    }
}

public void valyti(View v) {
    pavardes.clear();
    amziai.clear();
    issilavinimai.clear();
    TextView rez = findViewById(R.id.rez);
    rez.setText("Išvalyta");
}

@Override
protected void onStop() {
    super.onStop();

    TextView rez = findViewById(R.id.rez);
    try {
        FileOutputStream fos = openFileOutput("Karnauskas.12", Context.MODE_PRIVATE);
        for(int i = 0; i < pavardes.size(); i++){
            String eilute = pavardes.get(i)+" - "+amziai.get(i)+ " - "+issilavinimai.get(i)+"\n";
            fos.write(eilute.getBytes());
        }
        fos.close();
        rez.setText("Išsaugota");
    } catch (FileNotFoundException e) {
        rez.setText("Klaida 1");
    } catch (IOException e) {
        rez.setText("Klaida 2");
    }
}


我猜问题出在@override部分,因为删除该部分不会再使应用程序崩溃,并且最小/最大值仅给出“无数据”,但随后所有其他按钮使应用程序崩溃。我真的很感谢您的帮助!

最佳答案

minmax的逻辑错误。如下进行:

public void skaiciuoti(View v) {
    TextView rez = findViewById(R.id.rez2);
    // Check amziai, pavardes, and issilavinimai for null before performing any
    // operation on them.
    if (amziai != null && pavardes != null && issilavinimai != null && amziai.size() > 0 && pavardes.size() > 0
            && issilavinimai.size() > 0) {
        // Assign the first element to min and max
        int min = amziai.get(0);
        int max = amziai.get(0);
        String min_pavarde = pavardes.get(0);
        String min_issilavinimas = issilavinimai.get(0);
        String max_pavarde = pavardes.get(0);
        String max_issilavinimas = issilavinimai.get(0);
        // Loop from index 1. Also, compare the loop counter with the size of amziai,
        // pavardes, and issilavinimai.
        for (int i = 1; i < amziai.size() && i < pavardes.size() && i < issilavinimai.size(); i++) {
            if (min > amziai.get(i)) {
                min = amziai.get(i);
                min_pavarde = pavardes.get(i);
                min_issilavinimas = issilavinimai.get(i);
            }
            if (max < amziai.get(i)) {
                max = amziai.get(i);
                max_pavarde = pavardes.get(i);
                max_issilavinimas = issilavinimai.get(i);
            }
        }
        rez.setText("MIN: " + min_pavarde + " " + min + " " + min_issilavinimas + System.getProperty ("line.separator"));
        rez.append("MAX: " + max_pavarde + " " + max + " " + max_issilavinimas);
    } else {
        rez.setText("No data");
    }
}


如有任何疑问/问题,请随时发表评论。

10-05 17:49