2013-09-01 59 views
0

我的應用程序出現問題。它表示invalide索引0,大小爲0的android sqlite。這是logcat的無效的索引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()); 
} 

UPDATE:

代碼片段來自將對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; 
    } 

CODE:

/** 
* 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; 
} 

我不知道我在這裏失蹤,但我相信我的數據庫中有記錄,我仔細檢查了它。非常感謝幫助。謝謝。

回答

1

從這兩條線索:

at com.mapeh.subject.musiclesson.GamePlay.getNextQuestion(GamePlay.java:102) 

Question next = questions.get(this.getRound()); <~~~~~ Error here 

明顯this.getRound()回報0,但questions是空的,所以在位置沒有Question 0 UF questions。因此例外。

+0

那麼我該如何調試問題呢? – Dunkey

0

您可能還沒有添加任何問題,您問題列表對象(在遊戲類的成員變量),結果有一個空列表(大小爲0),並試圖建立索引,從而產生ArrayIndexOutOfBounds例外,因爲你不要在代碼中的任何地方使用addQuestions(...)方法。

+0

那麼我該怎麼辦?我將如何處理addQuestions方法? – Dunkey

+0

我建議你在期望的代碼點(可能在GamePlay初始化部分)填寫所需的問題列表。 我的觀點是,你必須在你的列表中有問題才能爲它們編制索引。 –

+0

嗨,這些問題已經從我的預填充分貝。請檢查我的更新信息。 – Dunkey

0

我已經看到過..有點類似於此:http://automateddeveloper.blogspot.com/2011/06/getting-started-complete-android-app_11.html

試試這個: 1.仔細檢查,如果你已經更新了數據基礎。 2.使用sqlite瀏覽器查看數據庫表格上的值是否正確。

這樣做後,如果所有值都是「1」,那麼在模擬器上運行應用程序時,檢查「難度」上的值轉到設置並選擇「EASY」。如果值爲「2」,則選擇「MEDIUM」等等。

希望幫助..

相關問題