Ticklistener 中的 Arrayupdate 应用程序崩溃

App Crash on Arrayupdate within Ticklistener

我现在正在使用我的第一个应用程序,但我遇到了有关 Arraylist 对象更新的问题。

这是代码:

package com.example.stopwatchmulti;

import androidx.appcompat.app.AppCompatActivity;

import android.os.Bundle;
import android.os.SystemClock;
import android.util.Log;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.Chronometer;
import android.widget.ListView;
import android.widget.TextView;

import java.util.ArrayList;

public class Main2Activity extends AppCompatActivity {

    //Define Variables and Arrays

    String ergebnis;
    String stopTime;
    int anzahl;
    int hours;
    int minutes;
    int seconds;
    Chronometer chronometer;
    long pauseOffset;
    boolean running;
    int arrayelements = anzahl - 1;
    long timeElapsed;


    ListView customListview;
    ArrayList<UserIDs> userList;
    ArrayList<String> stoppedTime;
    CustomListview adapter;

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

        ergebnis = getIntent().getExtras().getString("Ergebnis");

        anzahl = Integer.parseInt(ergebnis);

        chronometer = findViewById(R.id.chronometer);

        customListview = (ListView) findViewById(R.id.customListview);

        userList = new ArrayList<>();

        // Add Lines in favour of UserChoice in First activity

        for (int i = 1; i <= anzahl; i++) {

            UserIDs user = new UserIDs(String.valueOf(i), "Chronometer" + String.valueOf(i), stopTime);
            userList.add(user);

        }

        stopTime = String.valueOf(hours)+":"+String.valueOf(minutes)+":"+String.valueOf(seconds);

        // Run Custom ArrayAdapter
        adapter = new CustomListview(this, R.layout.usertableobjects, userList);
        customListview.setAdapter(adapter);

        }

        public void startChronometer(View v) {
            if (!running) {
                chronometer.setBase(SystemClock.elapsedRealtime());
                chronometer.start();
                running = true;
                new Thread(new updateStoptime()).start();

            }
        }

    public void pauseChronometer(View v) {
            if (running) {
                chronometer.stop();
                running = false;
            }
        }

        public void resetChronometer(View v) {
            chronometer.setBase(SystemClock.elapsedRealtime());
        }

    class updateStoptime implements Runnable {

        @Override
        public void run() {
            chronometer.setOnChronometerTickListener(new Chronometer.OnChronometerTickListener() {
                @Override
                public void onChronometerTick(Chronometer chronometer) {

                    timeElapsed = SystemClock.elapsedRealtime() - chronometer.getBase();

                    hours   = (int) (timeElapsed / 3600000);
                    minutes = (int) (timeElapsed - hours * 3600000 / 6000);
                    seconds = (int) (timeElapsed - hours * 3600000 - minutes * 60000) / 1000;

                    stopTime = String.valueOf(hours)+":"+String.valueOf(minutes)+":"+String.valueOf(seconds);

                    Log.i("Zeit:", String.valueOf(stopTime));

                }
            });

            while(running == true){

                for (int i = 1; i <= anzahl; i++) {

                    userList.get(arrayelements).setUserTime(stopTime);
                    Log.i("LoopNr:", String.valueOf(i));
                }
            }
        }
    }
}

while 循环中arraylist 的更新是用户单击按钮时应用程序崩溃的原因。如果我只是循环并在日志中打印循环数,一切正常。

是不是dada太多了?我该如何解决?

PS。基本上我只需要实际的倒计时,当用户单击按钮然后用更新的数组更新列表视图时。因此,如果有更好更智能的方法来实现这一点而不是使用 while 循环,我将不胜感激。

提前致谢

快速更正:


// Here anzahl is not initialized and is being referred to in the next line.
// Please initialize anzahl first;
   int anzahl;
   int arrayelements = anzahl - 1;

然后,将您的 while 循环更改为类似这样的内容。

   while(running == true){

      for (int i = 1; i <= anzahl; i++) {

         /* here, just call "i -1". this is basically equal to "arrayelements"
            which is not properly referenced up in your code.*/

         userList.get(i - 1).setUserTime(stopTime);
         Log.i("LoopNr:", String.valueOf(i));
      }
    }