2014-10-12 85 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留给垃圾收集是没有意义的。只需创建一次列表,然后引用它。