我有一個名爲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());
應用程序崩潰。
爲什麼會發生這種情況?我做錯了什麼?謝謝!!!
你會得到什麼錯誤信息?顯示完整的堆棧跟蹤。 –