我的应用程序有问题。它说无效索引0,大小0 android sqlite。这是日志猫
09-01 12:58:47.903: E/AndroidRuntime(15196): FATAL EXCEPTION: main
09-01 12:58:47.903: E/AndroidRuntime(15196): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.mapeh.subject/com.mapeh.subject.assessment.MusicLessonAssessment}: java.lang.IndexOutOfBoundsException: Invalid index 0, size is 0
09-01 12:58:47.903: E/AndroidRuntime(15196): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2180)
09-01 12:58:47.903: E/AndroidRuntime(15196): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2230)
09-01 12:58:47.903: E/AndroidRuntime(15196): at android.app.ActivityThread.access$600(ActivityThread.java:141)
09-01 12:58:47.903: E/AndroidRuntime(15196): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1234)
09-01 12:58:47.903: E/AndroidRuntime(15196): at android.os.Handler.dispatchMessage(Handler.java:99)
09-01 12:58:47.903: E/AndroidRuntime(15196): at android.os.Looper.loop(Looper.java:137)
09-01 12:58:47.903: E/AndroidRuntime(15196): at android.app.ActivityThread.main(ActivityThread.java:5041)
09-01 12:58:47.903: E/AndroidRuntime(15196): at java.lang.reflect.Method.invokeNative(Native Method)
09-01 12:58:47.903: E/AndroidRuntime(15196): at java.lang.reflect.Method.invoke(Method.java:511)
09-01 12:58:47.903: E/AndroidRuntime(15196): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
09-01 12:58:47.903: E/AndroidRuntime(15196): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
09-01 12:58:47.903: E/AndroidRuntime(15196): at dalvik.system.NativeStart.main(Native Method)
09-01 12:58:47.903: E/AndroidRuntime(15196): Caused by: java.lang.IndexOutOfBoundsException: Invalid index 0, size is 0
09-01 12:58:47.903: E/AndroidRuntime(15196): at java.util.ArrayList.throwIndexOutOfBoundsException(ArrayList.java:251)
09-01 12:58:47.903: E/AndroidRuntime(15196): at java.util.ArrayList.get(ArrayList.java:304)
09-01 12:58:47.903: E/AndroidRuntime(15196): at com.mapeh.subject.musiclesson.GamePlay.getNextQuestion(GamePlay.java:102)
09-01 12:58:47.903: E/AndroidRuntime(15196): at com.mapeh.subject.assessment.MusicLessonAssessment.onCreate(MusicLessonAssessment.java:34)
09-01 12:58:47.903: E/AndroidRuntime(15196): at android.app.Activity.performCreate(Activity.java:5104)
09-01 12:58:47.903: E/AndroidRuntime(15196): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1080)
09-01 12:58:47.903: E/AndroidRuntime(15196): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2144)
09-01 12:58:47.903: E/AndroidRuntime(15196): ... 11 more
这是代码:
private Question currentQ;
private GamePlay currentGame;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.musiclessonassessment);
/**
* Configure current game and get question
*/
currentGame = ((MapehSubjectApplication)getApplication()).getCurrentGame();
currentQ = currentGame.getNextQuestion(); <~~~~~ Error here
Button nextBtn = (Button) findViewById(R.id.next);
nextBtn.setOnClickListener(this);
/**
* Update the question and answer options..
*/
setQuestions();
}
/**
* Method to set the text for the question and answers from the current games
* current question
*/
private void setQuestions() {
//set the question text from current question
String question = Utility.capitalise(currentQ.getQuestion()) + "?";
TextView qText = (TextView) findViewById(R.id.question);
qText.setText(question);
//set the available options
List<String> answers = currentQ.getQuestionOptions();
TextView option1 = (TextView) findViewById(R.id.answer1);
option1.setText(Utility.capitalise(answers.get(0)));
TextView option2 = (TextView) findViewById(R.id.answer2);
option2.setText(Utility.capitalise(answers.get(1)));
TextView option3 = (TextView) findViewById(R.id.answer3);
option3.setText(Utility.capitalise(answers.get(2)));
TextView option4 = (TextView) findViewById(R.id.answer4);
option4.setText(Utility.capitalise(answers.get(3)));
}
@Override
public void onClick(View arg0) {
//Log.d("Questions", "Moving to next question");
/**
* validate a checkbox has been selected
*/
if (!checkAnswer()) return;
/**
* check if end of game
*/
if (currentGame.isGameOver()){
//Log.d("Questions", "End of game! lets add up the scores..");
//Log.d("Questions", "Questions Correct: " + currentGame.getRight());
//Log.d("Questions", "Questions Wrong: " + currentGame.getWrong());
Intent i = new Intent(this, GameOver.class);
startActivity(i);
finish();
}
else{
Intent i = new Intent(this, MusicLessonAssessment.class);
startActivity(i);
finish();
}
}
@Override
public boolean onKeyDown(int keyCode, KeyEvent event)
{
switch (keyCode)
{
case KeyEvent.KEYCODE_BACK :
return true;
}
return super.onKeyDown(keyCode, event);
}
/**
* Check if a checkbox has been selected, and if it
* has then check if its correct and update gamescore
*/
private boolean checkAnswer() {
String answer = getSelectedAnswer();
if (answer==null){
//Log.d("Questions", "No Checkbox selection made - returning");
return false;
}
else {
//Log.d("Questions", "Valid Checkbox selection made - check if correct");
if (currentQ.getAnswer().equalsIgnoreCase(answer))
{
//Log.d("Questions", "Correct Answer!");
currentGame.incrementRightAnswers();
}
else{
//Log.d("Questions", "Incorrect Answer!");
currentGame.incrementWrongAnswers();
}
return true;
}
}
/**
*
*/
private String getSelectedAnswer() {
RadioButton c1 = (RadioButton)findViewById(R.id.answer1);
RadioButton c2 = (RadioButton)findViewById(R.id.answer2);
RadioButton c3 = (RadioButton)findViewById(R.id.answer3);
RadioButton c4 = (RadioButton)findViewById(R.id.answer4);
if (c1.isChecked())
{
return c1.getText().toString();
}
if (c2.isChecked())
{
return c2.getText().toString();
}
if (c3.isChecked())
{
return c3.getText().toString();
}
if (c4.isChecked())
{
return c4.getText().toString();
}
return null;
}
GamePlay.class
public class GamePlay {
private int numRounds;
private int difficulty;
private String playerName;
private int right;
private int wrong;
private int round;
private List<Question> questions = new ArrayList<Question>();
/**
* @return the playerName
*/
public String getPlayerName() {
return playerName;
}
/**
* @param playerName the playerName to set
*/
public void setPlayerName(String playerName) {
this.playerName = playerName;
}
/**
* @return the right
*/
public int getRight() {
return right;
}
/**
* @param right the right to set
*/
public void setRight(int right) {
this.right = right;
}
/**
* @return the wrong
*/
public int getWrong() {
return wrong;
}
/**
* @param wrong the wrong to set
*/
public void setWrong(int wrong) {
this.wrong = wrong;
}
/**
* @return the round
*/
public int getRound() {
return round;
}
/**
* @param round the round to set
*/
public void setRound(int round) {
this.round = round;
}
/**
* @param difficulty the difficulty to set
*/
public void setDifficulty(int difficulty) {
this.difficulty = difficulty;
}
/**
* @return the difficulty
*/
public int getDifficulty() {
return difficulty;
}
/**
* @param questions the questions to set
*/
public void setQuestions(List<Question> questions) {
this.questions = questions;
}
/**
* @param q the question to add
*/
public void addQuestions(Question q) {
this.questions.add(q);
}
/**
* @return the questions
*/
public List<Question> getQuestions() {
return questions;
}
public Question getNextQuestion(){
//get the question
Question next = questions.get(this.getRound()); <~~~~~ Error here
//update the round number to the next round
this.setRound(this.getRound()+1);
return next;
}
/**
* method to increment the number of correct answers this game
*/
public void incrementRightAnswers(){
right ++;
}
/**
* method to increment the number of incorrect answers this game
*/
public void incrementWrongAnswers(){
wrong ++;
}
/**
* @param numRounds the numRounds to set
*/
public void setNumRounds(int numRounds) {
this.numRounds = numRounds;
}
/**
* @return the numRounds
*/
public int getNumRounds() {
return numRounds;
}
/**
* method that checks if the game is over
* @return boolean
*/
public boolean isGameOver(){
return (getRound() >= getNumRounds());
}
更新:
来自 DBAdapter 的代码片段:
public List<Question> getQuestionSet(int difficulty, int numQ){
List<Question> questionSet = new ArrayList<Question>();
Cursor c = myDataBase.rawQuery("SELECT * FROM tblQuestions WHERE Lesson = " + difficulty +
" ORDER BY RANDOM() LIMIT " + numQ, null);
while (c.moveToNext()){
//Log.d("QUESTION", "Question Found in DB: " + c.getString(1));
Question q = new Question();
q.setQuestion(c.getString(1));
q.setAnswer(c.getString(5));
q.setOption1(c.getString(2));
q.setOption2(c.getString(3));
q.setOption3(c.getString(4));
q.setRating(difficulty);
questionSet.add(q);
}
return questionSet;
}
代码:
/**
* Listener for game menu
*/
@Override
public void onClick(View v) {
Intent i;
switch (v.getId()){
case R.id.assess :
//once logged in, load the main page
//Log.d("LOGIN", "User has started the game");
//Get Question set //
List<Question> questions = getQuestionSetFromDb();
//Initialise Game with retrieved question set ///
GamePlay c = new GamePlay();
c.setQuestions(questions);
c.setNumRounds(getNumQuestions());
((MapehSubjectApplication)getApplication()).setCurrentGame(c);
//Start Game Now.. //
i = new Intent(this, MusicLessonAssessment.class);
startActivityForResult(i, Constants.PLAYBUTTON);
break;
case R.id.exit :
finish();
break;
}
}
/**
* Method that retrieves a random set of questions from
* the database for the given difficulty
* @return
* @throws Error
*/
private List<Question> getQuestionSetFromDb() throws Error {
int diff = getDifficultySettings();
int numQuestions = getNumQuestions();
DBAdapter myDBAdapter = new DBAdapter(this);
try {
myDBAdapter.createDataBase();
} catch (IOException ioe) {
throw new Error("Unable to create database");
}
try {
myDBAdapter.openDataBase();
}catch(SQLException sqle){
throw sqle;
}
List<Question> questions = myDBAdapter.getQuestionSet(diff, numQuestions);
myDBAdapter.close();
return questions;
}
/**
* Method to return the difficulty settings
* @return
*/
private int getDifficultySettings() {
SharedPreferences settings = getSharedPreferences(Constants.SETTINGS, 0);
int diff = settings.getInt(Constants.DIFFICULTY, Constants.MUSIC);
return diff;
}
/**
* Method to return the number of questions for the game
* @return
*/
private int getNumQuestions() {
SharedPreferences settings = getSharedPreferences(Constants.SETTINGS, 0);
int numRounds = settings.getInt(Constants.NUM_ROUNDS, 10);
return numRounds;
}
我不知道我在这里缺少什么,但我相信我的数据库有记录,我仔细检查了它。非常感谢您的帮助。谢谢。