2012-12-22 59 views
-1

我有一個名爲listOfDrills的列表,它有Iterator drillIt,爲什麼迭代器沒有next()值(我很痛,列表不是空的,因爲它可以很好地與listOfDrills.get(0)一起工作)。下面是代碼:Android應用程序崩潰,導致迭代器

package com.simplemathgame; 

import java.util.ArrayList; 
import java.util.Collections; 
import java.util.Iterator; 
import java.util.List; 
import java.util.Random; 

import android.app.Activity; 
import android.os.Bundle; 
import android.util.Log; 
import android.view.View; 
import android.view.View.OnClickListener; 
import android.widget.Button; 
import android.widget.TextView; 

public class GamePlayActivity extends Activity { 
    int addDrills; 
    int subDrills; 
    int mulDrills; 
    int divDrills; 
    int minBound; 
    int maxBound; 

    TextView drillTextPlace; 
    Button nextButton; 
    Button backButton; 

    List<Drill> listOfDrills = new ArrayList<Drill>(); 
    Iterator<Drill> drillIt = listOfDrills.iterator(); 

    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_game_play); 

     //get values from other activity 
     Bundle extras = getIntent().getExtras(); 
     addDrills = extras.getInt("addDrills"); 
     subDrills = extras.getInt("subDrils"); 
     mulDrills = extras.getInt("mulDrills"); 
     divDrills = extras.getInt("divDrills"); 
     minBound = extras.getInt("minBound"); 
     maxBound = extras.getInt("maxBound"); 

     for(int i = 0; i < addDrills; i++){ 
      AddDrill tmpDrill = new AddDrill(minBound,maxBound); 
      listOfDrills.add(tmpDrill); 
     } 

     for(int i = 0; i < subDrills; i++){ 
      SubDrill tmpDrill = new SubDrill(minBound,maxBound); 
      listOfDrills.add(tmpDrill); 
     } 

     for(int i = 0; i < mulDrills; i++){ 
      MulDrill tmpDrill = new MulDrill(minBound,maxBound); 
      listOfDrills.add(tmpDrill); 
     } 

     for(int i = 0; i < divDrills; i++){ 
      DivDrill tmpDrill = new DivDrill(minBound,maxBound); 
      listOfDrills.add(tmpDrill); 
     } 

     long seed = System.nanoTime(); 
     Collections.shuffle(listOfDrills, new Random(seed)); 

     drillTextPlace = (TextView) findViewById(R.id.drill_text); 
     nextButton = (Button) findViewById(R.id.next_button); 
     backButton = (Button) findViewById(R.id.back_button); 

     if(drillIt.hasNext()){ 
      drillTextPlace.setText((drillIt.next()).printDrill()); 
     } 

     nextButton.setOnClickListener(new OnClickListener(){ 

      @Override 
      public void onClick(View v) { 
       // TODO Auto-generated method stub 
       if(drillIt.hasNext()){ 
        drillTextPlace.setText(drillIt.next().printDrill()); 
       } 
      } 
     }); 
    } 

} 

代碼永遠不會進入中:

if(drillIt.hasNext()){ 
      drillTextPlace.setText((drillIt.next()).printDrill()); 
     } 

,如果我刪除if語句,只留下drillTextPlace.setText((drillIt.next()).printDrill()); 應用程序崩潰。

爲什麼會發生這種情況?我做錯了什麼?謝謝!!!

+1

你會得到什麼錯誤信息?顯示完整的堆棧跟蹤。 –

回答

5

您應該在填充列表後創建迭代器。如文檔中看到:和的ListIterator此類的迭代器返回

的迭代器 方法是快速失敗的:如果列表在任何 時間,通過結構改性創建迭代器之後,以任何方式,除了 迭代器自己的remove或add方法,迭代器將拋出一個 ConcurrentModificationException。

http://docs.oracle.com/javase/6/docs/api/java/util/ArrayList.html

移動

Iterator<Drill> drillIt = listOfDrills.iterator(); 

只是之前:你是太早創建迭代器

if (drillIt.hasNext()) { 
    drillTextPlace.setText((drillIt.next()).printDrill()); 
} 
0

,需要創建它,你想用它之前。您在空時創建它。即使你每次打電話時都會有效,你會前進一步,永不退後......