我的測驗應用程序崩潰時,預計聽到正確/錯誤的答案的聲音。這曾經工作,現在它不,我不明白爲什麼。有人能幫助我嗎?的代碼:應用程序與媒體播放器崩潰
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:嘗試上的空對象引用調用虛擬方法 '無效android.media.MediaPlayer.start()'
預先感謝
'如果創建失敗MediaPlayer.create'返回null ... https://developer.android.com/reference/android/media/MediaPlayer.html#create(android.content .Context,int) –
你的問題是與文件本身。您應該添加一個受支持的合成文件https://developer.android.com/guide/appendix/media-formats.html – zombie