2016-11-17 30 views
-1

我正在嘗試爲我的應用創建圖表,並且無法將我的數據加載到圖表。它給我一個內存不足的錯誤。然後,我一直試圖通過使用AsyncTask在後臺線程中運行SQLite查詢。我試過但無法正確創建它。我在這裏發佈我的代碼,並希望有人能幫助我。非常感謝!!!!Sqlite ArrayList <Float>查詢使用AsyncTask運行

這裏是錯誤消息:

 java.lang.OutOfMemoryError: Failed to allocate a 103059952 byte allocation with 4194304 free bytes and 53MB until OOM 
                    at java.util.ArrayList.add(ArrayList.java:118)     

下面是我提供的數據源我的代碼的一部分。我從SQLite數據庫中提取數據。查詢起作用。我測試過了。我需要通過AsyncTask編寫在後臺線程中運行查詢的代碼。

 public ArrayList<Float> yData(){ 
    ArrayList<Float> ynewData=new ArrayList<Float>(); 
    SQLiteDatabase db=this.getReadableDatabase(); 
    String sql="select sum("+ TableData.TableInfo.COL_S4_AMT+") MonthPieCHartCategoryValues,c."+ TableData.TableInfo.COL_C2_Description+" Category from "+ TableData.TableInfo.DataBase_Table_Spending+" s join "+ TableData.TableInfo.DataBase_Table_Items+" i on i."+ TableData.TableInfo.COL_I1_ItemID+"=s."+ TableData.TableInfo.COL_S3_ItemID+ " join "+ TableData.TableInfo.DataBase_Table_Category+" c on c."+ TableData.TableInfo.COL_C1_CategoryID+"=i."+ TableData.TableInfo.COL_I3_CategoryID + " where strftime('%Y',"+ TableData.TableInfo.COL_S2_Spending_DT+")=strftime('%Y',Date('now')) and strftime('%m',"+ TableData.TableInfo.COL_S2_Spending_DT+")=strftime('%m',date('now')) group by c."+ TableData.TableInfo.COL_C2_Description+" order by Category desc"; 
    Cursor cursor=db.rawQuery(sql,null); 
    for(cursor.moveToFirst();!cursor.isAfterLast();cursor.moveToLast()){ 
     ynewData.add(cursor.getFloat(cursor.getColumnIndex(MonthPieCHartCategoryValues))); 
    } 
    cursor.close(); 
    return ynewData; 

} 
+0

明顯監守剛剛創建的無限循環......問這個問題在這裏 – Selvin

+0

無限循環前檢查條件的兩倍?哪一部分?我是Android和Java的新手。 –

+0

似乎你在邏輯基礎知識方面存在問題......如果你在每次迭代時將某些東西移動到最後位置,你將永遠不會碰到「最後」位置 – Selvin

回答

0

正如Selvin所說,你的循環將運行無限次,因爲你在循環的每次運行後都移動到最後一個元素。嘗試是這樣的:

cursor.moveToFirst(); 
while(!cursor.isAfterLast()) { 
    ynewData.add(cursor.getFloat(cursor.getColumnIndex(MonthPieCHartCategoryValues))); 
    cursor.moveToNext(); 
} 
cursor.close(); 
+0

非常感謝Rhari!我今晚會試試,明天再回到你身邊。 –

+0

這應該工作。我的錯!!! –