2016-03-05 94 views
-1

在物理電話上測試Android應用程序。我使用Android Studio。我不知道爲什麼我得到java.lang.IndexOutOfBoundsException:BufferedReader從txt文件轉換爲陣列

Process: com.packtpub.libgdx.outtacluck.android, PID: 27731 
java.lang.IndexOutOfBoundsException: Invalid index 4, size is 0 
     at java.util.ArrayList.throwIndexOutOfBoundsException(ArrayList.java:255) 
     at java.util.ArrayList.get(ArrayList.java:308) 
     at com.packtpub.libgdx.outtacluck.game.WorldController.highScoreCheck(WorldController.java:1048) 
     at com.packtpub.libgdx.outtacluck.game.WorldController.update(WorldController.java:375) 
     at com.packtpub.libgdx.outtacluck.screens.GameScreen.render(GameScreen.java:40) 
     at com.packtpub.libgdx.outtacluck.screens.DirectedGame.render(DirectedGame.java:67) 
     at com.badlogic.gdx.backends.android.AndroidGraphics.onDrawFrame(AndroidGraphics.java:424) 
     at android.opengl.GLSurfaceView$GLThread.guardedRun(GLSurfaceView.java:1531) 
     at android.opengl.GLSurfaceView$GLThread.run(GLSurfaceView.java:1248) 

但我從來都沒有被添加到scoresData陣列

這裏的類。我宣佈它一樣

public class ScoreManager { 


public List<Double> scoresData = new ArrayList<Double>(); 


public ScoreManager() 
{ 
try{ 
     // Open the file that is the first 
     FileInputStream fstream = new FileInputStream(Constants.SCORES_TRACKER); 

     // Use DataInputStream to read binary NOT text. 
     BufferedReader br = new BufferedReader(new InputStreamReader(fstream)); 

     String strLine; 


     //Read File Line By Line 
     while ((strLine = br.readLine()) != null) { 
      // Add number from file to list 
      scoresData.add(Double.parseDouble(strLine)); 
     } 
     //Close the input stream 
     br.close(); 

     System.out.println(scoresData); 
    }catch (Exception e){ 
     e.printStackTrace(); 
     } 
    System.out.println("scoresData.size is " + scoresData.size()); 

    } 

public static double parseInt(String input) throws NullPointerException, ParseException{ 
    if(input == null){ 
     throw new NullPointerException(); 
    } 

    input = input.trim(); 

    NumberFormat numberFormat = NumberFormat.getNumberInstance(Locale.US); 
    ParsePosition parsePosition = new ParsePosition(0); 
    Number number = numberFormat.parse(input, parsePosition); 

    if(parsePosition.getIndex() != input.length()){ 
     throw new ParseException("Invalid input", parsePosition.getIndex()); 
    } 

    return number.doubleValue(); 
} 


public int getArrayElement(int index) 
{ 
    if(scoresData.isEmpty()) 
    { 
     System.out.println("scoresData is empty"); 
     return 0; 
    } 
    else 
    { 
     double x = scoresData.get(index); 
     return (int) x; 
    } 
    } 
} 
+0

什麼是在這條線:'在com.packtpub.libgdx.outtacluck.game.WorldController.highScoreCheck(WorldController.java:1048)'? –

+0

line1048是double lowestTopScore = scoreManager.scoresData.get(4); // arraylist應該總是被排序,所以我抓住最後一個元素 – user3846448

+1

嗯,這是你的問題。你試圖從一個空的ArrayList中獲取一個項目。另外,請閱讀如何使用列表:http://stackoverflow.com/questions/6231973/difference-between-list-list-listt-liste-and-listobject –

回答

-2

我剛剛測試了這一點,它填充arraylist就好了。根據上述代碼,我認爲您的數據列表聲明不正確。嘗試這個。

List<Double> scoresData = new ArrayList<Double>(); 
+0

這是如何回答問題/解釋錯誤? –

+0

它只是沒有。 –

+0

我編輯了我的問題以包含課程。我認爲我以同樣的方式宣佈它 – user3846448