Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/196.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 在textView中逐个显示ArrayList中的值_Java_Android - Fatal编程技术网

Java 在textView中逐个显示ArrayList中的值

Java 在textView中逐个显示ArrayList中的值,java,android,Java,Android,我试图在一段时间后在单行文本视图上逐个显示ArrayList中的值。如何在不阻塞主线程的情况下实现这一点 我已经编写了可以使用Thread.sleep执行此操作的代码,但运行几秒钟后,活动就会崩溃。我已使用For Loop&Thread.sleep在一段时间间隔后迭代每个ArrayList值 当活动崩溃时,运行几秒钟后,我将获得IndexOutOfBondException public void errorRepeater() { Thread t = new Thread() {

我试图在一段时间后在单行文本视图上逐个显示ArrayList中的值。如何在不阻塞主线程的情况下实现这一点

我已经编写了可以使用Thread.sleep执行此操作的代码,但运行几秒钟后,活动就会崩溃。我已使用For Loop&Thread.sleep在一段时间间隔后迭代每个ArrayList值

当活动崩溃时,运行几秒钟后,我将获得
IndexOutOfBondException

public void errorRepeater() {

    Thread t = new Thread() {

        @Override
        public void run() {
            //  !isInterrupted()

            while (!isInterrupted()) {
                for (xz = 0; xz < errorList.size(); xz++) {
                    try {
                        Thread.sleep(2000);  //1000ms = 1 sec

                        runOnUiThread(new Runnable() {

                            @Override
                            public void run() {
                                String sErrorList = errorList.get(xz);
                                String sErrorListOkBox = errorListOkBox.get(xz);
                                Log.i("MQTT sErrorList", sErrorList);
                                TextView tvC1HPLP = findViewById(R.id.errormsg);
                                tvC1HPLP.setText(sErrorList);
                                TextView tvok = findViewById(R.id.ok);
                                tvok.setText(sErrorListOkBox);
                                rl.setBackgroundResource(R.drawable.errorred);
                                tvC1HPLP.setTextColor(Color.RED);

                            }
                        });

                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                }
            }
        }
    };

    t.start();

}
public void errorRepeater(){
线程t=新线程(){
@凌驾
公开募捐{
//!isInterrupted()
而(!isInterrupted()){
对于(xz=0;xz

textView应该在ArrayList中逐个显示值,而不会使活动崩溃。

仅供参考,您可以尝试以下方法

   // You can define those both textview globally.
   TextView tvC1HPLP = findViewById(R.id.errormsg);
   TextView tvok = findViewById(R.id.ok);

   Handler mHandler = new Handler();
   final Runnable runnable = new Runnable() {
     int count = 0;
     @Override
     public void run() {

         String sErrorList = errorList.get(count%errorList.size);
         String sErrorListOkBox = errorListOkBox.get(count%errorListOkBox.size);

         tvC1HPLP.setText(sErrorList);

         tvok.setText(sErrorListOkBox);
         rl.setBackgroundResource(R.drawable.errorred);
         tvC1HPLP.setTextColor(Color.RED);
         count++;
         mHandler.postDelayed(this, 4000); // four second in ms
     }
   };
   mHandler.postDelayed(runnable, 1000);

什么是errorListOkBox?如果它们是两个列表,它们的大小是不同的。OrlistOkbox也是ArrayList。两个ArrayList的大小相同。虽然运行数秒后的活动崩溃是不同的,但这就是问题所在,这对我来说似乎很好。当手机从睡眠模式切换到唤醒模式时,它会使活动崩溃。它在“String sErrorList=errorList.get(count%errorList.size());”行上显示“java.lang.arithmetricexception:divide by zero”;“我应该在执行之前检查非零列表大小吗?添加了对零大小的检查。现在它工作正常。