When i’m running my project there
was no error message
but when I chose one answer
there was no correct answer
from the three options,
the same thing happened to
the other questions.
I don’t know where the problem lies.
please tell me where the error is.
thank you
I’m still a beginner in Android Studio.
so I don’t really understand
QuizActivity.java
public class quizmateri1Activity extends AppCompatActivity {
private MediaPlayer mediaPlayer;
private TextView questionTV;
private RadioGroup optionRadioGroup;
private Button submitButton;
private ImageButton playButton;
private int currentQuestionIndex = 0; // index untuk pertanyaan saat ini
private Question[] questions;
private int score = 0;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_quizmateri1);
questionTV = findViewById(R.id.soal);
playButton = findViewById(R.id.soalsound);
optionRadioGroup = findViewById(R.id.optionRadioGroup);
submitButton = findViewById(R.id.next);
//pertanyaan
questions = new Question[]{
new Question("namo", new String[]{"Option 1", "Option 2", "Option 3"}, "Correct Answer 1"),
new Question("ake", new String[]{"Option 1", "Option 2", "Option 3"}, "Correct Answer 2"),
//boleh tambah pertanyaan
};
//load pertanyaan pertama
loadQuestion(currentQuestionIndex);
playButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
playAudio(questions[currentQuestionIndex].getAudioFileName());
}
});
submitButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
checkAnswer();
}
});
}
private void loadQuestion(int questionIndex) {
// set text pertanyaan
questionTV.setText("Soal " + (questionIndex + 1));
// reset pilihan jawaban
optionRadioGroup.clearCheck();
// set pilihan jawaban
RadioButton option1 = findViewById(R.id.option1);
RadioButton option2 = findViewById(R.id.option2);
RadioButton option3 = findViewById(R.id.option3);
option1.setText(questions[questionIndex].getOptions()[0]);
option2.setText(questions[questionIndex].getOptions()[1]);
option3.setText(questions[questionIndex].getOptions()[2]);
}
private void playAudio(String audioFileName) {
if (mediaPlayer != null) {
mediaPlayer.release();
}
int resId = getResources().getIdentifier(audioFileName, "raw", getPackageName());
mediaPlayer = MediaPlayer.create(this, resId);
mediaPlayer.start();
}
private void checkAnswer() {
// jawaban user
int selectedId = optionRadioGroup.getCheckedRadioButtonId();
RadioButton selectedRadioButton = findViewById(selectedId);
/// RadioButton selectedRadioButton = findViewById(optionRadioGroup.getCheckedRadioButtonId());
/// int selectedId = optionRadioGroup.indexOfChild(selectedRadioButton);
if (selectedRadioButton != null) {
String selectedAnswer = selectedRadioButton.getText().toString();
String correctAnswer = questions[currentQuestionIndex].getCorrectAnswer();
if (selectedAnswer.equals(correctAnswer)) {
score++;
Toast.makeText(this, "Correct!", Toast.LENGTH_SHORT).show();
} else {
Toast.makeText(this, "InCorrect!", Toast.LENGTH_SHORT).show();
}
currentQuestionIndex++; // pindah soal berikut
if (currentQuestionIndex < questions.length) {
loadQuestion(currentQuestionIndex);
} else {
Toast.makeText(this, "Quiz selesai! Skore Anda: " + score, Toast.LENGTH_SHORT).show();
}
} else {
Toast.makeText(this, "Silahkan pilih jawaban!", Toast.LENGTH_SHORT).show();
}
}
@Override
protected void onDestroy() {
super.onDestroy();
if (mediaPlayer != null) {
mediaPlayer.release();
}
}
}
Question.java
public class Question {
private String audioFileName;
private String[] options;
private String correctAnswer;
public Question(String audioFileName, String[] options, String correctAnswer) {
this.audioFileName = audioFileName;
this.options = options;
this.correctAnswer = correctAnswer;
}
public String getAudioFileName() {
return audioFileName;
}
public String[] getOptions() {
return options;
}
public String getCorrectAnswer() {
return correctAnswer;
}
}
activity_quiz.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#D3FBB6"
tools:context=".QuizActivity">
<ImageButton
android:id="@+id/exit"
android:layout_width="30dp"
android:layout_height="30dp"
android:layout_gravity="top|left"
android:layout_marginTop="10dp"
android:src="@drawable/ic_back"
android:layout_marginLeft="10dp"
tools:ignore="SpeakableTextPresentCheck,TouchTargetSizeCheck" />
<TextView
android:id="@+id/scoreTextView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center|top"
android:layout_marginLeft="130dp"
android:layout_marginTop="10dp"
android:text="Scroe : 0"
android:textColor="@color/black"
android:textSize="20sp"
android:textStyle="bold" />
<Chronometer
android:id="@+id/timer"
android:layout_width="100dp"
android:layout_height="100dp"
android:layout_marginLeft="100dp"
android:layout_marginTop="6dp"
android:visibility="gone" />
<androidx.cardview.widget.CardView
android:id="@+id/cardv"
android:layout_width="270dp"
android:layout_height="130dp"
android:layout_below="@+id/scoreTextView"
android:layout_marginLeft="50dp"
android:layout_marginTop="30dp"
android:elevation="5dp"
app:cardCornerRadius="15dp">
<TextView
android:id="@+id/soal"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:layout_marginTop="10dp"
android:text="Pertanyaan"
android:textSize="20sp"
android:textColor="@color/black"
android:textStyle="bold"/>
<ImageButton
android:id="@+id/soalsound"
android:layout_width="50dp"
android:layout_height="50dp"
android:layout_below="@+id/soal"
android:layout_gravity="center"
android:layout_marginTop="10dp"
android:src="@drawable/ic_volume"
tools:ignore="SpeakableTextPresentCheck" />
</androidx.cardview.widget.CardView>
<RadioGroup
android:id="@+id/optionRadioGroup"
android:layout_width="381dp"
android:layout_height="272dp"
android:layout_below="@+id/cardv"
android:layout_marginTop="20dp"
android:layout_marginStart="0dp"
android:layout_marginEnd="5dp"
android:padding="20dp"
android:elevation="5dp">
<RadioButton
android:id="@+id/option1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="15dp"
android:elevation="5dp"
android:background="@drawable/optiondesain"
android:freezesText="true"
android:padding="16dp"
android:text="Option 1" />
<RadioButton
android:id="@+id/option2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="20dp"
android:elevation="5dp"
android:background="@drawable/optiondesain"
android:freezesText="true"
android:padding="16dp"
android:text="Option 2" />
<RadioButton
android:id="@+id/option3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="20dp"
android:elevation="5dp"
android:background="@drawable/optiondesain"
android:freezesText="true"
android:padding="16dp"
android:text="Option 3" />
</RadioGroup>
<Button
android:id="@+id/next"
android:layout_width="270dp"
android:layout_height="wrap_content"
android:layout_below="@+id/optionRadioGroup"
android:layout_marginLeft="50dp"
android:text="SELANJUTNYA"
android:textSize="20sp"/>
</RelativeLayout>
2
Answers
To me it looks like a logic problem. Your answers are "Option 1", "Option 2" and "Option 3". Whatever you select, it’s not going to be equal to "Correct answer 1", so as implemented now, it will always say "InCorrect!" and move to the next question, or finish. Change "Correct answer 1" to either "Option 1", "Option 2" or "Option 3", so one of them is equal to the correct answer:
So the correct answer for first question will be the second radio button "Option 2" and for the second question "Option 3" will be the correct answer.
It seems that in the "checkAnswer" function, the 2 String you compare will never be the same. When you create a question, you make the correct answer
"Correct answer 1"
and the options as"Option 1", "Option 2", "Option 3"
. So when you check if the answer was correct, you always get false since"Correct answer 1"
is not one of the option.Instead of setting the correctAnswer as "correct answer 1" in the constructor, you could set it as the index of the correct answer. So when you call getCorrectAnswer you could
return options[correctAnswerIndex]
Hopefully this helps solves your issue