2017-03-18 1331 views
-1

我想提出一個測驗應用程序,並就死在下面的錯誤以下如何應對ava.lang.IndexOutOfBoundsException:無效的指數4,大小爲4

E/AndroidRuntime: FATAL EXCEPTION: main 
                    Process: com.example.owner.quiz, PID: 25307 
                    java.lang.IndexOutOfBoundsException: Invalid index 4, size is 4 
                     at java.util.ArrayList.throwIndexOutOfBoundsException(ArrayList.java:255) 
                     at java.util.ArrayList.get(ArrayList.java:308) 
                     at com.example.owner.quiz.MainActivity$1.onClick(MainActivity.java:50) 
                     at android.view.View.performClick(View.java:4791) 
                     at android.view.View$PerformClick.run(View.java:19884) 
                     at android.os.Handler.handleCallback(Handler.java:739) 
                     at android.os.Handler.dispatchMessage(Handler.java:95) 
                     at android.os.Looper.loop(Looper.java:135) 
                     at android.app.ActivityThread.main(ActivityThread.java:5268) 
                     at java.lang.reflect.Method.invoke(Native Method) 
                     at java.lang.reflect.Method.invoke(Method.java:372) 
                     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:902) 
                     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:697) 

我logcat的給予當我點擊該(MainActivity.java:50)它會去的主要活動 並顯示currentQ=quesList.get(qid);這 以下是我在這裏的主要活動代碼

public class MainActivity 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_main); 
    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())) 
      { 
       score ++ ; 
      } 
      currentQ=quesList.get(qid); 
      setQuestionView(); 
     } 
    }); 
} 

誰能告訴我如何處理這個

+1

你的錯誤說明了一切。大小是4,所以'quesList.get(4)'會拋出異常。 –

+1

可能的重複[什麼導致java.lang.ArrayIndexOutOfBoundsException,我該如何防止它?](http://stackoverflow.com/questions/5554734/what-c​​auses-a-java-lang-arrayindexoutofboundsexception-and-how- ()) –

回答

-1

您嘗試使用驗證,如果,見爲例

if (qid < quesList.size()) { 
    currentQ=quesList.get(qid); 
} 
1

你改變qid 4的地方,這不是你的包含代碼的價值,但你quesList只有4個項目長,所以有效的指標因爲它是0-3

-1

這可能是由於sqlite索引從1開始(而不是像List這樣的0)引起的,您能顯示您的DbHelper類代碼嗎?特別是getAllQuestions()方法。

+0

public List getAllQuestions(){ List quesList = new ArrayList (); String selectQuery =「SELECT * FROM」+ TABLE_QUEST; dbase = this.getReadableDatabase();遊標cursor = dbase.rawQuery(selectQuery,null); –

+0

if(cursor.moveToFirst()){ do問題解答= new問題(); quest.setID(cursor.getInt(0)); quest.setQUESTION(cursor.getString(1)); quest.setANSWER(cursor.getString(2)); quest.setOPTA(cursor.getString(3)); quest.setOPTB(cursor.getString(4)); quest.setOPTC(cursor.getString(5)); quesList.add(quest); } while(cursor.moveToNext()); } –

+0

好的,這看起來不錯,你會從這個方法中得到一定大小的「問題」列表。請注意,您將問題ID設置爲光標中的0位置。我assum這是你的自動增量ID,corect我如果我錯了,這個自動增量開始值1。現在我需要看看你是否使用此ID來從你的MainActivity qustion列表中獲取問題索引。讓我看看你在主要活動中改變可用的「qid」值的地方。 – nir

相關問題