"L'application s'est arrêté" dès que je cliques sur un bouton

Fermé
guineeguinee Messages postés 191 Date d'inscription jeudi 8 décembre 2016 Statut Membre Dernière intervention 30 août 2023 - 23 oct. 2017 à 11:26
BunoCS Messages postés 15472 Date d'inscription lundi 11 juillet 2005 Statut Modérateur Dernière intervention 25 mars 2024 - 24 oct. 2017 à 09:13
Bonjour,
Je développe une application Android de Quizz avec Android Studio, j'ai utilisé un textview pour la question, trois boutons pour les choix de réponse, quand j'installe mon app sur mon appareil physique ou je l'ouvre avec l'AVD, j'ai la première question qui s'affiche si je cliques sur n'importe lequel des boutons, l'application s'arrête.
Merci d'avance pour votre aide!


4 réponses

BunoCS Messages postés 15472 Date d'inscription lundi 11 juillet 2005 Statut Modérateur Dernière intervention 25 mars 2024 3 894
Modifié le 23 oct. 2017 à 13:30
Hello (ça faisait longtemps, tiens ;) ),

Qu'as-tu dans les logs? Tu dois sûrement avoir un problème dans le onClickListener...

@+ 
Buno, Modo CS-CCM 
L'urgent est fait, l'impossible est en cours. Pour les miracles, prévoir un délai... 
The urgent is done, the impossible is underway. For miracles, provide for a delay...
1
guineeguinee Messages postés 191 Date d'inscription jeudi 8 décembre 2016 Statut Membre Dernière intervention 30 août 2023 38
Modifié le 23 oct. 2017 à 15:59
Ah oui ça faisait longtemps :)
En tout cas voici mon MainActivity.java

package com.quizzfouta.gn;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;

import java.util.Random;

public class Main2Activity extends AppCompatActivity {



private Questions mQuestions = new Questions();
private TextView mScoreView;
private TextView mQuestionView;
private Button mButtonChoice1;
private Button mButtonChoice2;
private Button mButtonChoice3;
private Button btnquit;

private String mAnswer;
private int mScore = 0;
private int mQuestionNumber = 0;


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

mButtonChoice1= (Button) findViewById(R.id.btn1);
mButtonChoice2= (Button) findViewById(R.id.btn2);
mButtonChoice3= (Button) findViewById(R.id.btn3);
btnquit= (Button) findViewById(R.id.btnquit);
mScoreView= (TextView) findViewById(R.id.score);
mQuestionView= (TextView) findViewById(R.id.tvq);

updateQuestion();

// Debut des boutons Ecouteurs

//Ecouteurs bouton 1
mButtonChoice1.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
if (mButtonChoice1.getText()== mAnswer){
mScore = mScore + 1;
updateScore(mScore);
updateQuestion();
//On ajoute un toast
Toast.makeText(Main2Activity.this, "C'est correct", Toast.LENGTH_SHORT).show();
} else {
Toast.makeText(Main2Activity.this, "C'est faux", Toast.LENGTH_SHORT).show();
updateQuestion();

}
}
});
//Ecouteurs Bouton 2
mButtonChoice2.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
if (mButtonChoice2.getText()== mAnswer){
mScore = mScore + 1;
updateScore(mScore);
updateQuestion();
//On ajoute un toast
Toast.makeText(Main2Activity.this, "C'est correct", Toast.LENGTH_SHORT).show();
} else {
Toast.makeText(Main2Activity.this, "C'est faux", Toast.LENGTH_SHORT).show();
updateQuestion();

}
}
});

//Ecouteurs bouton 3
mButtonChoice3.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
if (mButtonChoice3.getText()== mAnswer){
mScore = mScore + 1;
updateScore(mScore);
updateQuestion();
//On ajoute un toast
Toast.makeText(Main2Activity.this, "C'est correct", Toast.LENGTH_SHORT).show();
} else {
Toast.makeText(Main2Activity.this, "C'est faux", Toast.LENGTH_SHORT).show();
updateQuestion();

}
}
});
//Fin des écouteurs

}

private void updateQuestion() {

mQuestionView.setText(mQuestions.getQuestions(mQuestionNumber));
mButtonChoice1.setText(mQuestions.getChoice1(mQuestionNumber));
mButtonChoice2.setText(mQuestions.getChoice2(mQuestionNumber));
mButtonChoice3.setText(mQuestions.getChoice3(mQuestionNumber));

mAnswer = mQuestions.getCorrectAnswer(mQuestionNumber);
mQuestionNumber++;
}

private void updateScore(int point){
mScoreView.setText("" + mScore);
}
}

0
BunoCS Messages postés 15472 Date d'inscription lundi 11 juillet 2005 Statut Modérateur Dernière intervention 25 mars 2024 3 894 > guineeguinee Messages postés 191 Date d'inscription jeudi 8 décembre 2016 Statut Membre Dernière intervention 30 août 2023
23 oct. 2017 à 17:33
1. je ne comprends pas comment cela fonctionne: dans le
onCreate()
, tu appelle
updateQuestions()
mais ton tableau de questions est a priori vide -> crash
2. on ne compare pas des chaînes de caractères avec "==". Regarde
string.equals()
.
3. tes 3 callback onClickListener sont identiques -> il faut factoriser pour minimiser les sources d'erreur
0
guineeguinee Messages postés 191 Date d'inscription jeudi 8 décembre 2016 Statut Membre Dernière intervention 30 août 2023 38 > BunoCS Messages postés 15472 Date d'inscription lundi 11 juillet 2005 Statut Modérateur Dernière intervention 25 mars 2024
23 oct. 2017 à 17:40
Le tableau de question n'est pas vide, j'ai quelques questions dedans

package com.quizzfouta.gn;

public class Questions {
private String mQuestions[] = {
"Qui est le premier Almamy du Fouta ?",
"Qui est l'auteur de Oogirde Malal ?",
"Parmis ces trois livres, lequel parle d'héritage?",
};

private String mChoices[][] = {
{
"Almamy Bocar Biro", "Alpha Ibrahima Sambegou", "Almamy Ibrahima Sory Maoudho",
"Thierno Sadou Dalein", "Thierno Ibrahima Dama", "Thierno Samba Mombeya",
"Oogirde Malal", "Shukrul Ilaahi", "Nushu Ruaati",

}
};

private String mCorrectAnswer[] = {"Alpha Ibrahima Sambegou", "Thierno Samba Mombeya", "Shukrul Ilaahi", };

public String getQuestions(int a){
String question = mQuestions[a];
return question;
}
public String getChoice1(int a){
String choice0 = mChoices[a][0];
return choice0;
}
public String getChoice2(int a){
String choice1 = mChoices[a][1];
return choice1;
}
public String getChoice3(int a){
String choice2 = mChoices[a][2];
return choice2;
}
public String getCorrectAnswer(int a){
String answer = mCorrectAnswer[a];
return answer;
}
}
0