Android 应用程序变为空白并暂停所有线程:问题

Android 应用程序变为空白并暂停所有线程:问题,android,Android,我对一个小安卓项目有意见。 我创建了一个刽子手游戏,当我在变量“palavra”中放置一个重复字母的单词时,应用程序变为空白 while(parciais.contains(Character.toString(letras_possiveis[numero].charAt(0)))){ numero = random.nextInt(26);} 我相信这个问题与这段代码有关: while(parciais.contains(Character.toString(letras_possivei

我对一个小安卓项目有意见。 我创建了一个刽子手游戏,当我在变量“palavra”中放置一个重复字母的单词时,应用程序变为空白

while(parciais.contains(Character.toString(letras_possiveis[numero].charAt(0)))){ numero = random.nextInt(26);}
我相信这个问题与这段代码有关:

while(parciais.contains(Character.toString(letras_possiveis[numero].charAt(0)))){ numero = random.nextInt(26);}
我相信这一点,因为当我像评论一样定义这篇文章时,问题就不再发生了。我在这里搜索并听到一些关于在onCreate方法中放置太多命令的消息,最好将代码放置在onCreateView()方法中,但我正在自学,我还不知道这个方法。 谢谢大家

while(parciais.contains(Character.toString(letras_possiveis[numero].charAt(0)))){ numero = random.nextInt(26);}
我的主要活动:

while(parciais.contains(Character.toString(letras_possiveis[numero].charAt(0)))){ numero = random.nextInt(26);}
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());

}
}

你要去收集垃圾了。如果你收集这么多垃圾,你一定是在创建大量的对象。请确保您没有过度分配,特别是在诸如onDraw或onMeasure之类的关键路径中。对不起,我是个新手,对代码了解不多:(我能做些什么来解决我的问题?垃圾收集对我来说是一个未知的话题。谢谢你的回答。执行更少的分配并找到你的漏洞。垃圾收集是你在程序中犯严重错误的标志,而不是“在这里添加这一行,它就会被修复”问题。你正在进行垃圾收集。如果你收集这么多垃圾,你一定在创建大量对象。请确保你没有过度分配,尤其是在onDraw或onMeasure这样的关键路径中。对不起,我是个新手,我对代码不太了解:(我能做些什么来解决我的问题?垃圾收集对我来说是一个未知的话题。谢谢你的回答。执行更少的分配并找到你的漏洞。垃圾收集是你在程序中犯严重错误的标志,而不是“在这里添加这一行,它就会被修复”的问题。