目前正在进行测验应用,并出现这些错误。尝试寻找答案,但没有任何帮助。
Practice.java
public class Practice extends Activity {
public static final String QUIZNUM = "QUIZNUM";
public static final int Quiz1 = 1;
public static final int Quiz2 = 2;
public static final int Quiz3 = 3;
public static final int NUMBER_OF_QUESTIONS = 10;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.practice);
//Menu buttons
Button quiz1 = (Button) findViewById(R.id.q1Btn);
quiz1.setOnClickListener(new PracticeButton(Quiz1, NUMBER_OF_QUESTIONS));
Button quiz2 = (Button) findViewById(R.id.q2Btn);
quiz2.setOnClickListener(new PracticeButton(Quiz2, NUMBER_OF_QUESTIONS));
Button quiz3 = (Button) findViewById(R.id.q3Btn);
quiz3.setOnClickListener(new PracticeButton(Quiz3, NUMBER_OF_QUESTIONS));
}
class PracticeButton implements OnClickListener {
int quizNum;
int numQuestions;
public PracticeButton(int quizNum, int numQuestions){
this.quizNum = quizNum;
this.numQuestions = numQuestions;
}
public void onClick(View v){
List<Question> questions = getQuestionSetFromDb();
CurrentQuiz c = new CurrentQuiz();
c.setQuestions(questions);
c.setNumRounds(numQuestions);
c.setQuizNum(quizNum);
((MLearningApp)getApplication()).setCurrentGame(c);
startActivity(new Intent(QuestionActivity.this, QuestionActivity.class);
}
}
// Method that retrieves a random set of questions from db
private List<Question> getQuestionSetFromDb() throws Exception {
DBHelper myDbHelper = new DBHelper(QuestionActivity.this);
try {
myDbHelper.createDataBase();
} catch (IOException ioe) {
throw new Exception("Unable to create database");
}
try {
myDbHelper.openDataBase();
} catch (SQLException sqle) {
throw sqle;
}
List<Question> questions = myDbHelper.getQuestionSet(quizNum, numQuestions);
myDbHelper.close();
return questions;
}
}
在
中收到错误讯息1 - QuestionActivity.this 在范围内无法访问QuestionActivity类型的封闭实例
startActivity(new Intent(QuestionActivity.this, QuestionActivity.class);
2 - (QuestionActivity.this) 在范围
中无法访问QuestionActivity类型的封闭实例DBHelper myDbHelper = new DBHelper(QuestionActivity.this);
3 - (quizNum,numQuestions)
- numQuestions无法解析为a
变量
- quizNum无法解析为
变量
List<Question> questions = myDbHelper.getQuestionSet(quizNum, numQuestions);
答案 0 :(得分:0)
更改此
startActivity(new Intent(QuestionActivity.this, QuestionActivity.class);
到
startActivity(new Intent(Practice.this, QuestionActivity.class);
阅读
的显式意图http://developer.android.com/guide/components/intents-filters.html
更改此
DBHelper myDbHelper = new DBHelper(QuestionActivity.this);
到
DBHelper myDbHelper = new DBHelper(Practice.this);
第三次
int numQuestions;
//declared in class PracticeButton. not available in getQuestionSetFromDb().
您的PracticeButton
课程在getQuestionSetFromDb()
之前结束。
在getQuestionSetFromDb()
课程内移动PractiveButton
。
无论如何都要掌握教程并阅读文档。