App turns white and suspending all threads on Logcat

Asked

Viewed 47 times

1

Guys, I’m studying Android on my own and in my studies I created a hangman game, only when I replace the word to be discovered in String "word" with a word that contains repeated letters like "Horse" the app turns all white for a few moments in the emulator and then stops responding.

I believe the error appears because of this method:

while(parciais.contains(Character.toString(letras_possiveis[numero].charAt(0)))){
    numero = random.nextInt(26);
}

I believe that because when I put the code as a comment the error stops appearing. I searched on the net and read something saying that can not put too many command on Oncreate, because it causes error.

package mateus.studio.com.forca;

import android.graphics.Color;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;
import java.util.Random;

public class MainActivity extends AppCompatActivity {

String palavra = "CAVALO", parciais = "", parciais_botoes = "", letrasacertadas="";
String[] letras_possiveis = {"A", "B", "C", "D", "E", "F", "G", "H", "I", "J", "K", "L", "M", "N", "O", "P", "Q", "R", "S", "T", "U", "V", "W", "X", "Y", "Z"};
char[] letras_botoes = new char[25];
String[] letras_certas = new String[palavra.length()];
int erros = 0;

TextView txt_tela, txt_erro;
ImageView imagem;


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

txt_tela = (TextView) findViewById(R.id.txt_palavra);
txt_erro = (TextView) findViewById(R.id.txt_erro);
imagem = (ImageView) findViewById(R.id.img_status);

int botoes_letras[] = {R.id.button0, R.id.button1, R.id.button2, R.id.button3, R.id.button4,
        R.id.button5, R.id.button6, R.id.button7, R.id.button8, R.id.button9,
        R.id.button10, R.id.button11, R.id.button12, R.id.button13, R.id.button14, R.id.button15,
        R.id.button16,R.id.button17, R.id.button18, R.id.button19, R.id.button20, R.id.button21,
        R.id.button22, R.id.button23, R.id.button24};



  for (int a = 0; a < 25; a++){
    try {
        letras_botoes[a] = palavra.charAt(a);
        parciais = parciais + letras_botoes[a];
    }catch (StringIndexOutOfBoundsException e){
        Random random = new Random();
        int numero = random.nextInt(26);

        //verifica se a letra gerada pelo random já saiu e só sai do loop quando tem-se uma nova

        while(parciais.contains(Character.toString(letras_possiveis[numero].charAt(0)))){
            numero = random.nextInt(26);
        }
        //Coloca as letras no letras_botões (Mas só depois que formar o nome)

        letras_botoes[a] = letras_possiveis[numero].charAt(0);
        parciais = parciais + letras_botoes[a];
    }
}

//fOR QUE BUGA CASO TENHA LETRAS IGUAIS
 for (int a = 0; a< 25; a++) {
     Random aleatatorio = new Random();
     int al = aleatatorio.nextInt(25);
     while (parciais_botoes.contains(Character.toString(letras_botoes[al]))) {// Código que buga a parada toda
         al = aleatatorio.nextInt(25);                                         // Código que buga a parada toda
     }

     parciais_botoes = parciais_botoes + letras_botoes[al];

     //aQUI DEVE SE COLOCAR O CODIGO DE INSTANCIAR OS BOTO
     final Button botao;
     botao = (Button) findViewById(botoes_letras[a]);
     botao.setText(Character.toString(letras_botoes[al]));


 }

for (int a = 0; a < letras_certas.length; a++){
    if (letras_certas[a] == null){
        letras_certas[a]="_ ";
    }
}

for (int a = 0; a < palavra.length(); a++){
    txt_tela.setText("_ " + txt_tela.getText());
}

}



public void verifica_letra(View view) {
    letrasacertadas = txt_tela.getText().toString();
txt_tela.setText("");
            for (int i = 0; i < palavra.length(); i++) {
            String texto = ((Button) view).getText().toString() ;
            if (palavra.charAt(i) == texto.charAt(0)) {
                letras_certas[i] = texto;

                //  ta de boa, não mexe, pelo amor de Jeová

            } else {
            ((Button)         view).setBackgroundColor(Color.parseColor("#800000"));
        }

    verifica_erro(view);
}

 for (int a = 0 ; a < letras_certas.length ; a++){
 txt_tela.setText(txt_tela.getText() + letras_certas[a]);

 }
}    
   public void verifica_erro (View view){
if (palavra.contains(((Button) view).getText().toString())) ((Button) view).setBackgroundColor(Color.parseColor("#006400"));
else {
    ((Button) view).setBackgroundColor(Color.parseColor("#800000"));
    erros ++;

}

   if (letrasacertadas.equals(palavra)){
       setContentView(R.layout.venceu);
   }else {
   switch (erros/palavra.length()){
   case 0:
       imagem.setImageResource(R.drawable.base);
       break;

   case 1:
       imagem.setImageResource(R.drawable.a);
       break;
   case 2:
       imagem.setImageResource(R.drawable.b);
       break;
   case 3:
       imagem.setImageResource(R.drawable.c);
       break;

   case 4:
       imagem.setImageResource(R.drawable.d);
       break;

   case 5:
       imagem.setImageResource(R.drawable.e);
       break;

   case 6:
       imagem.setImageResource(R.drawable.f);
       break;

   default:
       setContentView(R.layout.perdeu);
       break;
   }}

   txt_erro.setText("Erros: "+erros/palavra.length());

}
}

That’s what I see in the logcat:

01-19 22:26:47.815 30117-30117/? E/Zygote: v2
01-19 22:26:47.815 30117-30117/? I/libpersona: KNOX_SDCARD checking this for 10116
01-19 22:26:47.815 30117-30117/? I/libpersona: KNOX_SDCARD not a persona
01-19 22:26:47.815 30117-30117/? I/SELinux: Function: selinux_compare_spd_ram , priority [2] , priority version is VE=SEPF_SM-    G530H_5.0.2-1_0018
01-19 22:26:47.815 30117-30117/? E/SELinux: [DEBUG] get_category: variable seinfo: default sensitivity: NULL, cateogry: NULL
01-19 22:26:47.815 30117-30117/? I/art: Late-enabling -Xcheck:jni
01-19 22:26:47.905 30117-30117/? D/TimaKeyStoreProvider: in addTimaSignatureService
01-19 22:26:47.915 30117-30117/? D/TimaKeyStoreProvider: Cannot add TimaSignature Service, License check Failed
01-19 22:26:47.915 30117-30117/? D/ActivityThread: Added TimaKesytore provider
01-19 22:26:48.135 30117-30117/mateus.studio.com.forca D/DisplayManager: DisplayManager()
01-19 22:26:48.355 30117-30130/mateus.studio.com.forca I/art: Background partial concurrent mark sweep GC freed 247(41KB) AllocSpace objects, 0(0B) LOS objects, 62% free, 4MB/12MB, paused 11.957ms total 45.761ms
01-19 22:26:52.035 30117-30123/mateus.studio.com.forca W/art: Suspending all threads took: 21.840ms
01-19 22:26:52.545 30117-30123/mateus.studio.com.forca W/art: Suspending all threads took: 30.195ms
01-19 22:26:53.055 30117-30123/mateus.studio.com.forca W/art: Suspending all threads took: 38.104ms
01-19 22:26:58.055 30117-30123/mateus.studio.com.forca W/art: Suspending all threads took: 23.713ms
01-19 22:26:58.545 30117-30123/mateus.studio.com.forca W/art: Suspending all threads took: 17.573ms
01-19 22:26:59.795 30117-30130/mateus.studio.com.forca W/art: Suspending all threads took: 5.640ms
01-19 22:26:59.825 30117-30130/mateus.studio.com.forca I/art: Background sticky concurrent mark sweep GC freed 176441(5MB) AllocSpace objects, 0(0B) LOS objects, 30% free, 8MB/12MB, paused 6.563ms total 48.098ms
01-19 22:27:00.315 30117-30130/mateus.studio.com.forca W/art: Suspending all threads took: 8.270ms
01-19 22:27:00.355 30117-30130/mateus.studio.com.forca I/art: Background sticky concurrent mark sweep GC freed 173108(5MB) AllocSpace objects, 0(0B) LOS objects, 30% free, 8MB/12MB, paused 9.271ms total 53.534ms
01-19 22:27:00.545 30117-30123/mateus.studio.com.forca W/art: Suspending all threads took: 5.498ms
01-19 22:27:02.585 30117-30123/mateus.studio.com.forca W/art: Suspending all threads took: 49.126ms
01-19 22:27:03.455 30117-30129/mateus.studio.com.forca I/art: WaitForGcToComplete blocked for 34.767ms for cause HeapTrim
01-19 22:27:06.065 30117-30123/mateus.studio.com.forca W/art: Suspending all threads took: 22.617ms
01-19 22:27:09.065 30117-30123/mateus.studio.com.forca W/art: Suspending all threads took: 12.519ms
01-19 22:27:12.085 30117-30123/mateus.studio.com.forca W/art: Suspending all threads took: 25.450ms
01-19 22:27:19.105 30117-30123/mateus.studio.com.forca W/art: Suspending all threads took: 25.492ms
01-19 22:27:23.495 30117-30129/mateus.studio.com.forca I/art:     WaitForGcToComplete blocked for 27.446ms for cause HeapTrim
  • Have you tried looking at the debugger what’s going on? The log doesn’t seem enough.

No answers

Browser other questions tagged

You are not signed in. Login or sign up in order to post.