I am quite new to coding and I am not that quite knowledgeable in debugging process.
As for now, this is all the information that I would be able to give, I hope someone has encountered and has a solution for this kind of problem.
Code for Main2Activity:
MediaPlayer bmusic;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main2);
bmusic = MediaPlayer.create(Main2Activity.this,R.raw.bmapp);
bmusic.setLooping(true);
bmusic.start();
button = findViewById(R.id.button);
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent intent = new Intent(Main2Activity.this, Main3Activity.class);
startActivity(intent);
}
});
}
@Override
protected void onPause() {
super.onPause();
bmusic.release();
finish();
}
Main2Activity is pretty much okay but once I click the start game button which would direct me to Main3Activity, an error pops up saying "testapp(my app name) has stopped.
Code for Main3Acitivty:
Button b_continue;
TextView tv_question;
EditText et_answer1;
List<Item> questions;
int curQuestion = 0;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main3);
b_continue = findViewById(R.id.b_continue);
tv_question = findViewById(R.id.tv_question);
et_answer1 = findViewById(R.id.et_answer1);
b_continue.setVisibility(View.INVISIBLE);
questions = new ArrayList<>();
//add all questions and answers to the game
for (int i = 0; 1 < Database.questions.length; i++){
questions.add(new Item(Database.questions[i], Database.answers[i]));
}
//shuffle the questions
Collections.shuffle(questions);
tv_question.setText(questions.get(curQuestion).getQuestion());
et_answer1.addTextChangedListener(new TextWatcher() {
@Override
public void beforeTextChanged(CharSequence s, int start, int count, int after) {
}
@Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
//check if the answer is right
if(et_answer1.getText().toString().equalsIgnoreCase(questions.get(curQuestion).getAnswer())){
b_continue.setVisibility(View.VISIBLE);
} else {
b_continue.setVisibility(View.INVISIBLE);
}
}
@Override
public void afterTextChanged(Editable s) {
}
});
b_continue.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if (curQuestion < (Database.questions.length - 1)) {
//next question
curQuestion++;
tv_question.setText(questions.get(curQuestion).getQuestion());
b_continue.setVisibility(View.INVISIBLE);
et_answer1.setText("");
} else {
//no more questions - game over
Toast.makeText(Main3Activity.this, "You won the game!", Toast.LENGTH_SHORT).show();
finish();
}
}
});
}