我的测验应用程序崩溃,希望听到正确/错误答案的声音。这曾经工作,现在它没有,我不明白为什么。有人可以帮帮我吗?代码:
import java.util.List;
import android.media.MediaPlayer;
import android.os.Bundle;
import android.app.Activity;
import android.content.Intent;
import android.util.Log;
import android.view.Menu;
import android.view.View;
import android.widget.Button;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.TextView;
import android.widget.Toast;
import com.example.vtorferreira.R;
public class QuizActivity extends Activity {
List<Question> quesList;
int score=0;
int qid=0;
Question currentQ;
TextView txtQuestion;
RadioButton rda, rdb, rdc;
Button butNext;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_quiz);
final MediaPlayer wrong_answer= MediaPlayer.create(this, R.raw.wrong_answer);
final MediaPlayer correct_answer= MediaPlayer.create(this, R.raw.correct_answer);
DbHelper db=new DbHelper(this);
quesList=db.getAllQuestions();
currentQ=quesList.get(qid);
txtQuestion=(TextView)findViewById(R.id.textView1);
rda=(RadioButton)findViewById(R.id.radio0);
rdb=(RadioButton)findViewById(R.id.radio1);
rdc=(RadioButton)findViewById(R.id.radio2);
butNext=(Button)findViewById(R.id.button1);
setQuestionView();
butNext.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
RadioGroup grp=(RadioGroup)findViewById(R.id.radioGroup1);
RadioButton answer=(RadioButton)findViewById(grp.getCheckedRadioButtonId());
if(currentQ.getANSWER().equals(answer.getText()))
{
correct_answer.start();
score++;
Toast.makeText(getApplicationContext(),"Correct!", Toast.LENGTH_SHORT).show();
}else{
wrong_answer.start();
Toast.makeText(getApplicationContext(),"Wrong...", Toast.LENGTH_SHORT).show();
}
if(qid<9){
currentQ=quesList.get(qid);
setQuestionView();
}else{
Intent intent = new Intent(QuizActivity.this, ResultActivity.class);
Bundle b = new Bundle();
b.putInt("score", score); //Your score
intent.putExtras(b); //Put your score to your next Intent
startActivity(intent);
finish();
}
}
});
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.activity_quiz, menu);
return true;
}
private void setQuestionView()
{
txtQuestion.setText(currentQ.getQUESTION());
rda.setText(currentQ.getOPTA());
rdb.setText(currentQ.getOPTB());
rdc.setText(currentQ.getOPTC());
qid++;
}
public void onBackPressed(){
}
}
日志: java.lang.NullPointerException:尝试在空对象引用上调用虚方法'void android.media.MediaPlayer.start()'
提前致谢
答案 0 :(得分:0)
您应该在correct_answer.prepare();
correct_answer.start();