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!
– novic
The
onCreate
no code! How was something supposed to happen ?– Isac
Sorry ignorance, I intended to use onCreate to define the images that will be in Imageviews. How can I set these images without onCreate?
– David Reis
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.– Isac
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.
– David Reis
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.
– David Reis
Directly -> call
setImageResource
in theonCreate
. Indirectly -> call noonCreate
the methodA
which in turn calls the methodB
who calls thesetImageResource
s– Isac