android中可能出现的错误?

时间:2017-03-05 08:22:54

标签: java android list

我想由于某种原因,下面的程序正在通过条件验证并执行下一个条件的行...

我的这个程序应该将值4和0添加到listIntercedeOut,但是在将它们添加到列表后立即删除...

  • 如果我们在for()方法之后立即放置listIntercedeOut.add(),则不会删除 之后...

  • 此外,如果我们停用对代码发表评论的两条listIntercedeOut.remove()行,则代码可以正常运行...

我已经用TextView和Button建立了一个小界面来检查列表的值,我已经从我的主程序中做了很多修改......那些"几乎是空的& #34;那里需要条件......

此外,intercede(boolean)方法将始终在示例中返回true。

AddDisciplina.java:

import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.util.SparseArray;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;

import java.util.ArrayList;
import java.util.List;

public class AddDisciplina extends AppCompatActivity {

Button buttonCheck;

TextView text;

private List<Integer> listIntercedeIn = new ArrayList<>();
private List<Integer> listIntercedeOut = new ArrayList<>();
private SparseArray<String> sparseSeg=new SparseArray<>();

String time = "00:00";
String time2 = "01:30";
int var = 3;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.layout_add_disciplina);
    if(getSupportActionBar() != null) {
        getSupportActionBar().setDisplayHomeAsUpEnabled(true);
    }
    getSupportActionBar().setHomeButtonEnabled(true);

    buttonCheck = (Button)findViewById(R.id.buttonCheck);
    text = (TextView)findViewById(R.id.text);

    buttonCheck.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            String items="";
            for(int i = 0; i < listIntercedeOut.size(); i++){
                items = items + "-" + String.valueOf(listIntercedeOut.get(i));
            }
            text.setText(items);
        }
    });
    sparseSeg.put(0, "01:00/02:00");
    sparseSeg.put(4, "00:00/01:30");
    abrirTimePicker(false);
}

private void abrirTimePicker(final boolean inicio){
    if(inicio) {
        if (time.equals("")) {
        }
    } else {
        if((1<0)||(1==0 && 30<=0)){
        }
        switch (var){
            case 3:
                for (int i = 0; i < sparseSeg.size(); i++) {
                String[] hs = sparseSeg.valueAt(i).split("/");
                int idDoLL = sparseSeg.keyAt(i);
                if ((inicio && !hs[1].equals("vazio") && intercede(hs[0], hs[1], time, time2)) ||
                        (!inicio && !hs[1].equals("vazio") && intercede(time, time2, hs[0], hs[1]))) {
                    if (inicio) { // DO SOMETHING
                    } else {
                        // SHOULD ALWAYS END HERE //
                        listIntercedeOut.add(4);
                        listIntercedeOut.add(idDoLL);

              // IF WE RELEASE THIS FOR, EVERYTHING WORKS FINE

                                /*for(i = 1; i < listIntercedeOut.size(); i++) {
                                }*/
                    }
                } else if (inicio && listIntercedeIn.contains(4) && listIntercedeIn.contains(0)) {
                    listIntercedeIn.remove(Integer.valueOf(4));
                    listIntercedeIn.remove(Integer.valueOf(idDoLL));


                } else if (!inicio && listIntercedeOut.contains(4) && listIntercedeOut.contains(0)) {

     // IF WE DISABLE THE FOLLOWING 2 LINES, EVERYTHING WORKS FINE

                    listIntercedeOut.remove(Integer.valueOf(4));
                    listIntercedeOut.remove(Integer.valueOf(0));
                }
            }
                break;
        }
    }
}

private boolean intercede (String horario1, String horario2, String horario3, String horario4){
    String[] hm1 = horario1.split(":");
    int horas1 = Integer.parseInt(hm1[0]);
    int minutos1 = Integer.parseInt(hm1[1]);
    int ms1 = (horas1 * 3600000) + (minutos1 * 60000);

    String[] hm2 = horario2.split(":");
    int horas2 = Integer.parseInt(hm2[0]);
    int minutos2 = Integer.parseInt(hm2[1]);
    int ms2 = (horas2 * 3600000) + (minutos2 * 60000);

    String[] hm3 = horario3.split(":");
    int horas3 = Integer.parseInt(hm3[0]);
    int minutos3 = Integer.parseInt(hm3[1]);
    int ms3 = (horas3 * 3600000) + (minutos3 * 60000);

    String[] hm4 = horario4.split(":");
    int horas4 = Integer.parseInt(hm4[0]);
    int minutos4 = Integer.parseInt(hm4[1]);
    int ms4 = (horas4 * 3600000) + (minutos4 * 60000);

    return (ms1<ms3 && ms3<ms2) || (ms1<ms4 && ms4<ms2);
}
}

add_disciplina_layout.xml:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical">
    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/text"/>
    <Button
        android:id="@+id/buttonCheck"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="CHECK LIST"/>
</LinearLayout>

有人可以解释一下这里发生了什么吗?提前致谢

1 个答案:

答案 0 :(得分:1)

你的sparseSeg列表中可能有一些与你的第三个条件循环相匹配的值。记下一些日志,看看发生了什么:

for (int i = 0; i < sparseSeg.size(); i++) {
                String[] hs = sparseSeg.valueAt(i).split("/");
                int idDoLL = sparseSeg.keyAt(i);
                if ((inicio && !hs[1].equals("vazio") && intercede(hs[0], hs[1], time, time2)) ||
                        (!inicio && !hs[1].equals("vazio") && intercede(time, time2, hs[0], hs[1]))) {
                    if (inicio) { // DO SOMETHING
                    } else {
                        // SHOULD ALWAYS END HERE //
                        listIntercedeOut.add(4);
                        listIntercedeOut.add(idDoLL);

                        Log.i("1 CONDITION", i + "");
              // IF WE RELEASE THIS FOR, EVERYTHING WORKS FINE

                                /*for(i = 1; i < listIntercedeOut.size(); i++) {
                                }*/
                    }
                } else if (inicio && listIntercedeIn.contains(4) && listIntercedeIn.contains(0)) {
                    listIntercedeIn.remove(Integer.valueOf(4));
                    listIntercedeIn.remove(Integer.valueOf(idDoLL));

                    Log.i("2 CONDITION", i + "");

                } else if (!inicio && listIntercedeOut.contains(4) && listIntercedeOut.contains(0)) {

     // IF WE DISABLE THE FOLLOWING 2 LINES, EVERYTHING WORKS FINE

                    listIntercedeOut.remove(Integer.valueOf(4));
                    listIntercedeOut.remove(Integer.valueOf(0));

                    Log.i("3 CONDITION", i + "");
                }
            }

我认为至少有一个值与最后一个条件匹配,所以,您的值或从列表中删除。 你应该修改你的循环系统条件。

希望这有帮助。

抱歉我的英语很差。

(我做了一点修正)