我的问题是我的switch语句无法正常工作,因为它不允许我使用bundle extra正确地从我的sqlite数据库中检索数据。
我的目标是,如果您点击“主题1'或者'主题2'按钮它将相关值放入tutorialpage.java中的附加包中,并在quizactivity.java中使用该值,它从sqlite数据库中加载qid的特定值的问题,因此topic1将有qid的问题1-3的主题和topic2将有4-5的qid。
目前没有switch语句,仅使用案例1中的代码,该程序将检索问题,直到给出的值,但是在此程序中它没有正确执行此操作,它当前只显示两个测验的问题1。下一个问题按钮不起作用,当它应该转到所选范围内的下一个问题时,就好像没有onclick方法。
正如您从一些注释掉的部分中看到的那样,我也得到了nullpointerexceptions。
我的文件如下:
tutorialpage.java,教程页面xml有两个按钮,其中包含Topic1和Topic2的id,这是一个switch语句,用于为单击该按钮添加一个额外的intent。
javax.sound.sampled.SourceDataLine
QuizActivity.java,这是Switch语句没有正确检索intent的文件,所以它只是出于某种原因检索第一个问题。
public void GotoTheQuizButton (View view) {
Intent intentStart = new Intent(tutorialpage.this, QuizActivity.class);
Bundle bundle = new Bundle();
switch(view.getId()) {
case R.id.Topic1:
/* intentStart.putExtra(Quiz_NUMBER, 1);*/
bundle.putInt(Quiz_NUMBER, 1);
intentStart.putExtras(bundle);
break;
case R.id.Topic2:
/* intentStart.putExtra(Quiz_NUMBER, 2);*/
bundle.putInt(Quiz_NUMBER, 2);
intentStart.putExtras(bundle);
break;
}
startActivity(intentStart);
}
}
DBHelper.java这是我的问题存储的地方,我用qid q1-q5标记了问题。
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
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu_quiz, menu);
return true;
}
private void setQuestionView()
{
txtQuestion.setText(currentQ.getQUESTION());
rda.setText(currentQ.getAnsA());
rdb.setText(currentQ.getAnsB());
rdc.setText(currentQ.getAnsC());
qid++;
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_tutorial_quiz);
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();
Intent intent = getIntent();
Bundle extras = intent.getExtras();
int Quiz = intent.getIntExtra(tutorialpage.Quiz_NUMBER, 0);
/*
null pointer exception
int Quiz = intent.getStringExtra(tutorialpage.Quiz_NUMBER, 0);
int Quiz = extras.getInt(tutorialpage.Quiz_NUMBER);*/
/* Check the value being passed to the switch
Toast login = Toast.makeText(QuizActivity.this, "Welcome " + Quiz, Toast.LENGTH_LONG);
login.show();*/
switch(Quiz ) {
case 1:
Toast login = Toast.makeText(QuizActivity.this, "Welcome " + Quiz, Toast.LENGTH_LONG);
login.show();
butNext.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
RadioGroup grp=(RadioGroup)findViewById(R.id.radioGroup1);
RadioButton answer=(RadioButton)findViewById(grp.getCheckedRadioButtonId());
Log.d("yourans", currentQ.getANSWER() + " " + answer.getText());
if(currentQ.getANSWER().equals(answer.getText()))
{ score++; Log.d("score", "Your score" + score); }
while (qid < 3){ currentQ=quesList.get(qid);
setQuestionView();
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();
}
});
break;
case 2:
butNext.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
RadioGroup grp = (RadioGroup) findViewById(R.id.radioGroup1);
RadioButton answer = (RadioButton) findViewById(grp.getCheckedRadioButtonId());
Log.d("yourans", currentQ.getANSWER() + " " + answer.getText());
if (currentQ.getANSWER().equals(answer.getText())) {
score++;
Log.d("score", "Your score" + score);
}
while (qid >= 4 && qid <= 5) {
currentQ = quesList.get(qid);
setQuestionView();
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();
}
});
break;
}
}
}
我有一个Question.java文件,其中包含问题,ID,答案等的基本getter和setter。