Why the app does not leave the white screen and does not give errors?

0

I'm making a pretty basic app with a screen that simulates a bingo card.

The problem is that when I run the code, without checking for repeated numbers (because there are no repeated numbers in a bingo card), the program runs quietly:

public class MainActivity extends AppCompatActivity {

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

        Random nRand = new Random();
        ArrayList<View> btn;
        int[] sorteados = new int[25];
        View cartela = findViewById(R.id.CartelaMain); //CartelaMain é o layout principal onde estão os 25 botões e um TextView
        btn = cartela.getTouchables();

        for(int cont = 0;cont < btn.size();cont++){
            int numero = nRand.nextInt(99) + 1; //Definindo o numero randômico
            Button botao = (Button) btn.get(contA); //Instanciando o botão
            String txt1 = Integer.toString(numero); //Passando de int para String
            botao.setText(txt1); //Set texto do botao
        }
    }

Now when I apply the verification:

public class MainActivity extends AppCompatActivity {

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

        Random nRand = new Random();
        ArrayList<View> btn;
        int[] sorteados = new int[25];
        View cartela = findViewById(R.id.CartelaMain); //CartelaMain é o layout principal onde estão os 25 botões e um TextView
        btn = cartela.getTouchables();

        for (int contA = 0; contA < btn.size(); contA++) { //Este método gera os numeros randomicametne e verifica por repetição dos numeros sorteados 
            int numero = nRand.nextInt(99) + 1;
            Boolean continuar = true;
            do {
                Boolean sair = true;
                for (int contB = 0; contB < btn.size(); contB++) {
                    int aux = sorteados[contB];
                    if(aux == numero){
                        numero = nRand.nextInt(99) + 1;
                        continuar = !sair;
                        contB = contB - 1;
                    }
               }

            } while (!continuar) ;

            sorteados[contA] = numero; //Armazenamento do numero no Array
            Button botao = (Button) btn.get(contA); //Instanciando o botão
            String txt1 = Integer.toString(numero); //Passando de int para String
            botao.setText(txt1); //Set texto do botão

        }

    }

The app simply does not leave the white screen. There is no record of compile errors by Android Studio and I have also debugged about 4 times.

I found the solution to the above situation, there was an infinite loop loop:

int number = nRand.nextInt (99) + 1;             Boolean continue = true;             of {                 Boolean exit = true;                 for (int contB = 0; contB When declared this way, the loop created for verification was not effective. Ex:

for (cont = 0; contd

asked by anonymous 31.05.2017 / 06:53

1 answer

0
for (int contA = 0; contA < btn.size(); contA++) {
        int numero = nRand.nextInt(99) + 1;
        Boolean continuar = true;
        for (int contB = 0; contB < btn.size(); contB++) {
             int aux = sorteados[contB];
             if(aux == numero){
                continuar = false;
                contA -= 1;
                break;
             }
        }
        if(continuar == true){
           sorteados[contA] = numero;
           Button botao = (Button) btn.get(contA);
           String txt1 = Integer.toString(numero);
           botao.setText(txt1);
        }
    }

I did not test the code, but here you have a function that should do what you want, if you have any questions or do not understand something says.

As I said, I did not test the code and if you have any errors say, that I try to solve.

I think this is not the best method to handle this, but I think it will work, good luck!

I think you could use ArrayUtils.contains(array, key); - I've never used it but it's a matter of experimenting :) I'll make the code here with this solution, try it and then say whether it worked or not.

for (int contA = 0; contA < btn.size(); contA++) {
        int numero = nRand.nextInt(99) + 1;
        if(ArrayUtils.contains(btn, numero);){
           sorteados[contA] = numero;
           Button botao = (Button) btn.get(contA);
           String txt1 = Integer.toString(numero);
           botao.setText(txt1);
        }else{
           contA -= 1;
        }
    }

I repeat, I have not tested any of these codes, and there may be some errors, if any and if you can not solve it say.

    
31.05.2017 / 10:46