Where is my mistake?

Asked

Viewed 74 times

0

I’m playing a little game on Android very simple, but I’m having problems with the interface.

I created the xml interface, and I set the java class corresponding to it (setContentView...), until then ok. But I want that at the time when this Activity is called, some images are set. Through onCreate I managed to set this Imageviews, I decided to create methods that did this, and just call them inside the onCreate, but from there the problem begins, this Activity is totally black.

Here are the parts of the code. Code Activity to be opened

public class Jogo extends AppCompatActivity {
//ImageViews que conterão as imagens relacionadas aos valores
ImageView imgView1;
ImageView imgView2;
ImageView imgView3;
ImageView imgView4;
ImageView imgView5;
ImageView imgView6;
ImageView imgView7;
ImageView imgView8;
ImageView imgView9;

//ImageViews de Iguais e Mais
ImageView maisView1;
ImageView maisView2;
ImageView maisView3;
ImageView maisView4;
ImageView maisView5;
ImageView maisView6;


//Iguais
ImageView igualView1;
ImageView igualView2;
ImageView igualView3;


//========================================================================//
//BOTÃO
Button btEnvia;
//EDIT TEXT
EditText editResposta;

//========================================================================//
Problema problema = new Problema();
//

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







}



//Cria ImageViews e linka com os do Layout.
private void criaImgView (){

   //ImageViews que conterão as imagens relacionadas aos valores
    imgView1 = (ImageView) findViewById(R.id.imgView1);
    imgView2 = (ImageView) findViewById(R.id.imgView2);
    imgView3 = (ImageView) findViewById(R.id.imgView3);
    imgView4 = (ImageView) findViewById(R.id.imgView4);
    imgView5 = (ImageView) findViewById(R.id.imgView5);
    imgView6 = (ImageView) findViewById(R.id.imgView6);
    imgView7 = (ImageView) findViewById(R.id.imgView7);
    imgView8 = (ImageView) findViewById(R.id.imgView8);
    imgView9 = (ImageView) findViewById(R.id.imgView9);

    //Operadores matemáticos
    maisView1= (ImageView) findViewById(R.id.maisView1);
    maisView2= (ImageView) findViewById(R.id.maisView2);
    maisView3= (ImageView) findViewById(R.id.maisView3);
    maisView4= (ImageView) findViewById(R.id.maisView4);
    maisView5= (ImageView) findViewById(R.id.maisView5);
    maisView6= (ImageView) findViewById(R.id.maisView6);

    //IGUAL
    igualView1= (ImageView) findViewById(R.id.igualView1);
    igualView2= (ImageView) findViewById(R.id.igualView2);
    igualView3= (ImageView) findViewById(R.id.igualView3);


}

//Cria instancias dos elementos do layout.
private void intanciaObj (){

    btEnvia = (Button)findViewById(R.id.btEntra);

    editResposta = (EditText) findViewById(R.id.editResposta);

}

//Seta ImageViews
private void imageViews (){


    imgView1.setImageResource(problema.arFinal[0][0]);
    imgView2.setImageResource(problema.arFinal[0][0]);
    imgView3.setImageResource(problema.arFinal[0][0]);

    imgView4.setImageResource(problema.arFinal[1][0]);
    imgView5.setImageResource(problema.arFinal[1][0]);
    imgView6.setImageResource(problema.arFinal[0][0]);

    imgView7.setImageResource(problema.arFinal[2][0]);
    imgView8.setImageResource(problema.arFinal[1][0]);
    imgView9.setImageResource(problema.arFinal[0][0]);

}

private void preparaTudo(){

    criaImgView();
    intanciaObj();
    imageViews();

}

Here the code of the class, where I previously assemble some variables used to define values to be applied in the Activity Game (I tried first instantiating the problem class in the Activity Game, and then putting all methods of the Problem class directly in Activity, but continued with the same result )

public class Problema {

//**VARIAVEIS RELACIONADAS A PARTE DOS VALORES DO PROBLEMA**

//=============================================================================================//
//MATRIZ PROBLEMA (VALORES)
int[][] matrizProblema = new int[3][4];
//array com os 3 valores
int[] arrayVal = new int[3];
//variavel define valor maximo numeros
int limite ;
//============================================================================================//
ArrayList<Icon> ico = new ArrayList<Icon>();

//**VÁRIAVEIS RELACIONADAS AS IMAGENS**
//============================================================================================//
//Array para guardar os caminhos das imagens
int[][] arrayImg =new int[3][2];
//Array que guarda os indices das imagens a serem usadas
int[] valoresImg = new int[3];
//Numero max de imagens no projeto
int numMaxImg = 3;
//Array final das imagens
int[][] arFinal = new int[3][2];
//============================================================================================//
public Problema (){

    montaMatriz();
    preparaImg();

}

//**PARTE RELACIONADA AOS VALORES.**
//============================================================================================//

//SORTEIA OS VALORES DO PROBLEM
private void sorteiaValor() {

    do {

        for (int i = 0; i < 3; i++) {

            arrayVal[i] = (int) (Math.random()*limite);
        }

    }
    while (arrayVal[0] == arrayVal[1] || arrayVal[0] == arrayVal[2] || arrayVal[1] == arrayVal[2]);
}

//MONTA MATRIZ
private void montaMatriz () {
    sorteiaValor();
    montaPrimeiraLinha();
    montaSegundaLinha();
    montaTerceiraLinha();

}
private void montaPrimeiraLinha(){
    for(int i=0;i<3;i++){
        matrizProblema[0][i]= arrayVal[0];
    }
    matrizProblema[0][4]= arrayVal[0]*3;
}
private void montaSegundaLinha(){

    for(int i=0; i<2;i++){
        matrizProblema[1][i]= arrayVal[1];

    }
    matrizProblema[1][2]= arrayVal[0];

    matrizProblema[1][3]= matrizProblema[1][0]+ matrizProblema[1][1]+matrizProblema[1][2];
}
private void montaTerceiraLinha(){
    for(int i = 0 ; i<3;i++){
        matrizProblema[2][i]= arrayVal[i];
    }
    matrizProblema[2][3]= matrizProblema[2][0]+matrizProblema[2][1]+matrizProblema[2][2];    }

//============================================================================================//

//**PARTE RELACIONADA AS IMAGENS.**
//============================================================================================//
//Chama todos os metodos relacionados nas imagens
private void preparaImg(){

    preencheImgs();
    sorteiaValorImgs();
    escolheImgs();
}

//Serve para preencher um array com as refêrencias das imagens

private void preencheImgs (){

    arrayImg[0][0] = R.drawable.caveira1;
    arrayImg[0][1] = R.drawable.caveira2;

    arrayImg[1][0] = R.drawable.download1;
    arrayImg[1][1] = R.drawable.download2;

    arrayImg[2][0] = R.drawable.menos1;
    arrayImg[2][1] = R.drawable.menos2;

}

//Sorteia valores para decidir qual imagem será relacionada com cada valor.
private void sorteiaValorImgs() {

    do {

        for (int i = 0; i < 3; i++) {

            valoresImg[i] = (int) (Math.random()*numMaxImg);
        }

    }
    while (valoresImg[0] == valoresImg[1] || valoresImg[0] == valoresImg[2] || valoresImg[1] == valoresImg[2]);
    }

//Monta um array, utilizando o array com os caminhos das imagens e o array com indices sorteados das imagens
private void escolheImgs(){

    for (int i =0;i< valoresImg.length;i++ ){
        int temp = valoresImg[i]-1;
        for (int j=0; i<2;i++ ) {
            switch (valoresImg[i]) {
                case 1:
                    arFinal[i][j] = arrayImg[temp][j];
            }
        }
    }

}}

Error returned:

10-18 22:12:14.815 30508-30508/com.belialgames.rachakuka E/Trace: error opening trace file: No such file or directory (2)
10-18 22:12:15.147 30508-30508/com.belialgames.rachakuka E/dalvikvm: Could not find class 'android.graphics.drawable.RippleDrawable', referenced from method android.support.v7.widget.AppCompatImageHelper.hasOverlappingRendering
10-18 22:12:15.649 30508-30508/com.belialgames.rachakuka E/MMUMapper: fail to register MVA, unsupported format(0x5)
10-18 22:12:15.796 30508-30508/com.belialgames.rachakuka E/MMUMapper: fail to register MVA, unsupported format(0x5)
10-18 22:12:21.347 30508-30508/com.belialgames.rachakuka E/MMUMapper: fail to register MVA, unsupported format(0x5)
  • Very complicated to find the error is too much code, gives a breakpoint and tests the application in debug mode!

  • The onCreate no code! How was something supposed to happen ?

  • Sorry ignorance, I intended to use onCreate to define the images that will be in Imageviews. How can I set these images without onCreate?

  • Whatever is meant to happen when an Activity opens has to be on onCreate directly or indirectly. If you explain better what exactly you are trying to do, it is easier to help.

  • I understood what you said, I ended up deleting the methods I was calling inside onCreate because I saw that they were a part of the cause of the bug.

  • What did you mean indirectly? Maybe that’s what I need, I can set the images using onCreate, but one by one, what visually does not please me, I tried to use methods inside it, but I think that then begins my problem.

  • Directly -> call setImageResource in the onCreate. Indirectly -> call no onCreate the method A which in turn calls the method B who calls the setImageResources

Show 2 more comments

1 answer

0


To my knowledge you must instantiate the views in the method OnCreate() since he’s with the layout of xml, try to put all the findViewById in the OnCreate() (can leave it out as is, so other methods can also use them), the screen must be completely black because you misread your views, so you can not match them to any image. You don’t call your method either preparaTudo() then even if I had everything right I think the screen would still be black (I program in Xamarin.Android, but the logic is the same for what I know, including just putting out of the method it already points out an error).

  • Really, I posted the code without the prepareTudo(), I took it before because I saw that it had something to do with the error. Let me ask a question, this Activity that I’m calling, depends on some settings, so I created its layout in xml, and at this moment that I call it, I want to put in it, certain images and values, replacing these default values of the layout. Is my reasoning right or has some better way of doing it?

  • I don’t quite understand what you mean, you’re asking if it’s best to set the image by XML or by code Java? If that’s it I think it doesn’t matter, but the way you’re doing it is also possible, you just need to put all those "findViewById" within the method OnCreate() to instantiate them correctly. Then you call the imageViews() to set some image for the imageviews. Also missing is the event by clicking on the Button (which I think would be calling some of the class methods Problema), but I guess this you’ll do after

  • Responding to what you said above ("I can set the images using onCreate, but one by one, which visually does not please me, I tried to use methods within it, but I think that this starts my problem."), you can set these images in a method as it is currently, the problem is instantiating them outside the OnCreate, you have to declare the variables out of it (to be able to use from outside, since he is protected) and instantiate them within it, from then on you can set the images in other methods and etc

  • My question is: Do I need to put these default values to just change them on onCreate, or do you have any way to already open the layout with the values and images I want? not so having to define them in onCreate. PS: could just arrow them in xml, but I still need to decide through some methods, which images and values I will use.

  • I understand, thank you for your attention and help.

  • You can set them by XML with the android:src="@drawable/nome da imagem" (had not seen the PS), as can also set them by another method as you are doing, the problem (as far as I know) is just the findViewById outside the OnCreate(), since he has to stay inside. By the way if the doubt has been resolved marks the V there below my answer to help people with similar doubts and help me too haha. Try putting these findViewById within the OnCreate() and then calls the imageViews() to set the images the same way you are to see if it works

  • 1

    Indirectly, you gave me the answer I needed, VLW.

Show 2 more comments

Browser other questions tagged

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