2015-12-08 26 views
0

所以我打算儘可能清楚地分解下來。如何解決將parsefile數組轉換爲Byte數組時出現越界錯誤?

1)所以我從Parse查詢ParseFile圖像數組,我想將它們轉換成位圖數組。所以這裏是代碼。

public void queryImage() { 
    ParseQuery<ParseObject> query = ParseQuery.getQuery("TestObject"); 

    //So lets queryImage all the images that belong to the user. 
    query.setLimit(2); 
    query.whereExists("ImageFile"); 
    query.orderByDescending("createdAt"); 
    try { 
     List<ParseObject> lists2 = query.find(); 
     for(ParseObject cardImage : lists2) { 
      ParseImageFileArrayList.add((ParseFile) cardImage.get("ImageFile")); 
     } 
     convertFileArray(ParseImageFileArrayList);//Here is another I created 
    } catch(ParseException e) { 
     e.printStackTrace(); 
    } 
} 

2)現在,在該方法我稱爲convertFileArray()方法,該方法在於parsefile陣列並將其轉換成一個位圖。這是代碼。

private void convertFileArray(ArrayList<ParseFile> arrayList) { 
    for(ParseFile file: arrayList) { 
     if(file != null) { 
      file.getDataInBackground(new GetDataCallback() { 
       @Override 
       public void done(byte[] bytes, ParseException e) { 
        if(e == null) { 
         bmp1 = BitmapFactory.decodeByteArray(bytes,0,bytes.length); 
         BitmapArray.add(bmp1); 
        } else { 

        } 
       } 
      }); 
     } 
    } 
} 

3)好了,現在我要遍歷名稱的字符串,「約翰,瑪麗,亞當,蘇珊等),並在該位置上的每個名字,我想從添加相應的圖片因此,如果John在名稱Array的位置0處,我應該將John的位置0處的圖像從我的Byte數組添加到我稱之爲「CardModel」的類中,並且請注意,這3種方法都是所有1級。

public void queryDoneSetCards() {//Here we need to make an Array of Cardmodels. 
     for(int i = 0; i < queryCardNames.size(); i++) { 
      CardModel cardModel = new CardModel(); 
      cardModel.setImageAsBitmap(BitmapArray.get(i));//HERE IS WHERE IM GETTING THE ERROR! 
      CardModelArray.add(cardModel); 
     } 
} 

4)最後當我到另一個活動,在用戶點擊「完成」(該queryImage方法被稱爲第一,它具有convertFileArray()方法而越發調用),然後調用queryDoneSetCards()方法。我得到這個索引越界異常,我不知道爲什麼它不工作。

Process: com.lorentzos.swipecards.example, PID: 23182 
java.lang.IndexOutOfBoundsException: Invalid index 0, size is 0 


12-08 13:26:47.218 23182-23182/com.lorentzos.swipecards.example E/AndroidRuntime: FATAL EXCEPTION: main 
      Process: com.lorentzos.swipecards.example, PID: 23182 
      java.lang.IndexOutOfBoundsException: Invalid index 0, size is 0 
      at java.util.ArrayList.throwIndexOutOfBoundsException(ArrayList.java:255) 
      at java.util.ArrayList.get(ArrayList.java:308) 
      at com.lorentzos.swipecards.MyParse.MyParse.queryDoneSetCards(MyParse.java:285) 
      at com.lorentzos.swipecards.MyParse.MyParse.convertFileArray(MyParse.java:267) 
      at com.lorentzos.swipecards.MyParse.MyParse.queryImage(MyParse.java:143) 
      at com.lorentzos.swipecards.CardFinal.Card_FINALE.onClick(Card_FINALE.java:258) 
      at android.view.View.performClick(View.java:4803) 
      at android.view.View$PerformClick.run(View.java:19981) 
      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:5430) 
      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:913) 
      at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:706) 
+0

請爲其添加完整的堆棧跟蹤並出現界限錯誤。 – doubleA

+0

您的錯誤出現在'queryDoneSetCards()'方法的for循環中。嘗試設置一些斷點或在其中放置一些日誌語句以確保queryCardNames集合具有數據並存在 – doubleA

+1

@doubleA querycardNames很好。它給我一個cardModel.setImageAsBitmap(BitmapArray.get(i))的錯誤;顯示我的BitmapArray是空的......也許是因爲我在後臺線程中執行它。 convertFileArray()方法中是否有替代file.getDataInBackground? – TheQ

回答

1

queryCardNames的大小是不一樣的BitmapArray,這就是爲什麼你會得到IndexOutOfBoundsException異常

queryDoneSetCards()方法,改變

for(int i = 0; i < queryCardNames.size(); i++) 

for(int i = 0; i < BitmapArray.size(); i++) 
+0

我試過了,BitmapArray也給了我一個indexOutOfBounds的懷疑。這是說大小是0 .. – TheQ

+0

如果BitmapArray大小爲0,它將不會進入循環,如何獲得IndexOutOfBoundsException ?? – Rami

+0

那麼,當我使用for循環查詢卡名稱數組並具有cardModel.setImageAsBitmap(BitmapArray.get(i));在for循環中,我得到了indexoutofBounds異常。但是如果我做你發佈的內容,我仍然會得到索引超出範圍的例外。導致:java.lang.IndexOutOfBoundsException:索引0無效,大小爲0 – TheQ

1

雖然與解析工作,我發現,這是把一些我的電話變成一個異步任務非常有用。特別是某些數據依賴於其他數據被取回的部分。只是將所有數據都放到異步任務中,然後使用它而非嘗試鏈接異步解析調用是很有用的。

private class MyAsync extends AsyncTask<> { 

    protected void doInBackground() { 
     queryImage() 
    } 

    protected void onPostExecute(Long result) { 
     queryDoneSetCards(); 
    } 
} 

public void queryImage() { 
    ParseQuery<ParseObject> query = ParseQuery.getQuery("TestObject"); 

    //So lets queryImage all the images that belong to the user. 
    query.setLimit(2); 
    query.whereExists("ImageFile"); 
    query.orderByDescending("createdAt"); 
    try { 
     List<ParseObject> lists2 = query.find(); 
     for(ParseObject cardImage : lists2) { 
      ParseImageFileArrayList.add((ParseFile) cardImage.get("ImageFile")); 
     } 
     convertFileArray(ParseImageFileArrayList);//Here is another I created 
    } catch(ParseException e) { 
     e.printStackTrace(); 
    } 
} 

你的轉換文件數組是同步的。

private void convertFileArray(ArrayList<ParseFile> arrayList) { 
    for(ParseFile file: arrayList) { 
     if(file != null) { 
      byte[] bytes = file.getData(); 
      bmp1 = BitmapFactory.decodeByteArray(bytes,0,bytes.length); 
      BitmapArray.add(bmp1); 
     } 
    } 
} 

然後在異步任務的onPostExecute()調用queryDoneSetCards。在執行queryDoneSetCards()循環之前,您還應該檢查一下以確保您的BitmapArray和名稱陣列具有相同的大小。

+0

工作很漂亮!謝謝你,先生! – TheQ