2014-10-12 87 views
0

我已經盡力了,但是我無法找到解決此問題的方法。 我爲Android設備編寫了一個Soundboard,其中每個聲音以及其按鈕圖像都是動態加載的。 這裏是我的代碼的關鍵段落:Samsung Galaxy設備上的內存不足

private void loadButtonColors(){ 
    int rescount = AppConfig.BUTTONBACKGROUNDSRES(getApplicationContext()).size(); 
    SharedPreferences prefs = getSharedPreferences("buttoncolors", 0); 
    String colors = null; 
    colors = prefs.getString("colors", null); 
    List<String> tempList = new ArrayList<String>(); 
    if(colors != null){ 
     tempList.addAll(Arrays.asList(TextUtils.split(colors, "~"))); 
    } 
    if(colors == null || tempList.size() != MainActivity.SOUNDBUTTONS.size()){ 
     for(@SuppressWarnings("unused") SoundButton s : MainActivity.SOUNDBUTTONS){ 
      int randomint = new Random().nextInt(rescount); 
      Integer randominteger = Integer.valueOf(randomint); 
      String randomstring = String.valueOf(randominteger); 

      tempList.add(randomstring); 
     } 

     prefs.edit().putString("colors", TextUtils.join("~", tempList)).commit(); 



    } 

    MainActivity.BUTTONCOLORS = (ArrayList<String>) tempList; 

} 

後來按鈕的圖像將與圖像了以下陣列在錯誤似乎出現的顯示。

public static ArrayList<Drawable> BUTTONBACKGROUNDSRES(Context context){ 
    ArrayList<Drawable> tempList = new ArrayList<Drawable>(); 
    tempList.add(context.getResources().getDrawable(R.color.button_lblue)); 
    tempList.add(context.getResources().getDrawable(R.color.button_lime)); 
    tempList.add(context.getResources().getDrawable(R.color.button_orange)); 
    tempList.add(context.getResources().getDrawable(R.color.button_pink)); 
    tempList.add(context.getResources().getDrawable(R.color.button_red)); 
    tempList.add(context.getResources().getDrawable(R.color.button_yellow)); 
    return tempList; 
}; 

產生的錯誤看起來是這樣的:

java.lang.OutOfMemoryError 
at android.graphics.BitmapFactory.nativeDecodeAsset(Native Method) 
at android.graphics.BitmapFactory.decodeStream(BitmapFactory.java:587) 
at android.graphics.BitmapFactory.decodeResourceStream(BitmapFactory.java:422) 
at android.graphics.drawable.Drawable.createFromResourceStream(Drawable.java:840) 
at android.content.res.Resources.loadDrawable(Resources.java:2150) 
at android.content.res.Resources.getDrawable(Resources.java:715) 
at android.graphics.drawable.StateListDrawable.inflate(StateListDrawable.java:176) 
at android.graphics.drawable.Drawable.createFromXmlInner(Drawable.java:937) 
at android.graphics.drawable.Drawable.createFromXml(Drawable.java:877) 
at android.content.res.Resources.loadDrawable(Resources.java:2132) 
at android.content.res.Resources.getDrawable(Resources.java:715) 
at com.centivo.inscope21soundboard.AppConfig.BUTTONBACKGROUNDSRES(AppConfig.java:45) 
at com.centivo.inscope21soundboard.LauncherActivity.loadButtonColors(LauncherActivity.java:128) 
at com.centivo.inscope21soundboard.LauncherActivity.access$1(LauncherActivity.java:127) 
at com.centivo.inscope21soundboard.LauncherActivity$1.run(LauncherActivity.java:43) 
at android.os.Handler.handleCallback(Handler.java:733) 
at android.os.Handler.dispatchMessage(Handler.java:95) 
at android.os.Looper.loop(Looper.java:136) 
at android.app.ActivityThread.main(ActivityThread.java:5105) 
at java.lang.reflect.Method.invokeNative(Native Method) 
at java.lang.reflect.Method.invoke(Method.java:515) 
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:792) 
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:608) 
at dalvik.system.NativeStart.main(Native Method) 

如果你想檢查出的應用程序訪問the Playstore

編輯:每個按鈕圖像的大小約爲70KB。

+0

你的按鈕圖像有多大? – immibis 2014-10-12 10:16:38

+0

每個圖像大約70kb – 2014-10-12 10:29:07

+0

是70kb壓縮還是未壓縮? – immibis 2014-10-12 10:29:35

回答

0

如果內存不足,請將tempList存儲在BUTTONBACKGROUNDSRES中的成員變量或靜態變量中。看起來你每次調用BUTTONBACKGROUNDSRES時,都會加載一組新的Drawables。如果這些總是相同的,那麼從零開始頻繁加載它們然後將tempList留給垃圾收集是沒有意義的。只需創建一次列表,然後引用它。