ImageView和ArrayList

时间:2014-05-16 18:06:59

标签: java android

我正在编写Android应用,但它崩溃了。这是代码:

public class Images extends Activity {

    private ImageView d1;
    private ArrayList<Integer> listaImagenes=new ArrayList<Integer>();
     @Override
     public void onCreate (Bundle savedInstanceState)
     {
         super.onCreate(savedInstanceState);
         this.rellenar();
          setContentView(R.layout.imagentonas);
          d1=(ImageView)findViewById(R.id.imagenes01);
          d1.setImageResource(listaImagenes.get(0));

     }
     private void rellenar()
     {
        listaImagenes.add(R.drawable.rosa1); 
        listaImagenes.add(R.drawable.imagencilla); 
     }
}

logcat的:

>05-16 14:21:13.792: E/dalvikvm-heap(887): Out of memory on a
> 69120016-byte allocation. 05-16 14:21:13.882: E/AndroidRuntime(887):
> FATAL EXCEPTION: main 05-16 14:21:13.882: E/AndroidRuntime(887):
> Process: com.example.terceraniversario, PID: 887 05-16 14:21:13.882:
> E/AndroidRuntime(887): java.lang.OutOfMemoryError 05-16 14:21:13.882:
> E/AndroidRuntime(887):    at
> android.graphics.BitmapFactory.nativeDecodeAsset(Native Method) 05-16
> 14:21:13.882: E/AndroidRuntime(887):  at
> android.graphics.BitmapFactory.decodeStream(BitmapFactory.java:587)
> 05-16 14:21:13.882: E/AndroidRuntime(887):    at
> android.graphics.BitmapFactory.decodeResourceStream(BitmapFactory.java:422)
> 05-16 14:21:13.882: E/AndroidRuntime(887):    at
> android.graphics.drawable.Drawable.createFromResourceStream(Drawable.java:840)
> 05-16 14:21:13.882: E/AndroidRuntime(887):    at
> android.content.res.Resources.loadDrawable(Resources.java:2110) 05-16
> 14:21:13.882: E/AndroidRuntime(887):  at
> android.content.res.Resources.getDrawable(Resources.java:700) 05-16
> 14:21:13.882: E/AndroidRuntime(887):  at
> android.widget.ImageView.resolveUri(ImageView.java:638) 05-16
> 14:21:13.882: E/AndroidRuntime(887):  at
> android.widget.ImageView.setImageResource(ImageView.java:367) 05-16
> 14:21:13.882: E/AndroidRuntime(887):  at
> com.example.terceraniversario.Images.onCreate(Images.java:20) 05-16
> 14:21:13.882: E/AndroidRuntime(887):  at
> android.app.Activity.performCreate(Activity.java:5231) 05-16
> 14:21:13.882: E/AndroidRuntime(887):  at
> android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087)
> 05-16 14:21:13.882: E/AndroidRuntime(887):    at
> android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2159)
> 05-16 14:21:13.882: E/AndroidRuntime(887):    at
> android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2245)
> 05-16 14:21:13.882: E/AndroidRuntime(887):    at
> android.app.ActivityThread.access$800(ActivityThread.java:135) 05-16
> 14:21:13.882: E/AndroidRuntime(887):  at
> android.app.ActivityThread$H.handleMessage(ActivityThread.java:1196)
> 05-16 14:21:13.882: E/AndroidRuntime(887):    at
> android.os.Handler.dispatchMessage(Handler.java:102) 05-16
> 14:21:13.882: E/AndroidRuntime(887):  at
> android.os.Looper.loop(Looper.java:136) 05-16 14:21:13.882:
> E/AndroidRuntime(887):    at
> android.app.ActivityThread.main(ActivityThread.java:5017) 05-16
> 14:21:13.882: E/AndroidRuntime(887):  at
> java.lang.reflect.Method.invokeNative(Native Method) 05-16
> 14:21:13.882: E/AndroidRuntime(887):  at
> java.lang.reflect.Method.invoke(Method.java:515) 05-16 14:21:13.882:
> E/AndroidRuntime(887):    at
> com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:779)
> 05-16 14:21:13.882: E/AndroidRuntime(887):    at
> com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595) 05-16
> 14:21:13.882: E/AndroidRuntime(887):  at
> dalvik.system.NativeStart.main(Native Method) 05-16 14:22:16.452:
> E/dalvikvm-heap(1167): Out of memory on a 69120016-byte allocation.
> 05-16 14:22:16.542: E/AndroidRuntime(1167): FATAL EXCEPTION: main
> 05-16 14:22:16.542: E/AndroidRuntime(1167): Process:
> com.example.terceraniversario, PID: 1167 05-16 14:22:16.542:
> E/AndroidRuntime(1167): java.lang.OutOfMemoryError 05-16 14:22:16.542:
> E/AndroidRuntime(1167):   at
> android.graphics.BitmapFactory.nativeDecodeAsset(Native Method) 05-16
> 14:22:16.542: E/AndroidRuntime(1167):     at
> android.graphics.BitmapFactory.decodeStream(BitmapFactory.java:587)
> 05-16 14:22:16.542: E/AndroidRuntime(1167):   at
> android.graphics.BitmapFactory.decodeResourceStream(BitmapFactory.java:422)
> 05-16 14:22:16.542: E/AndroidRuntime(1167):   at
> android.graphics.drawable.Drawable.createFromResourceStream(Drawable.java:840)
> 05-16 14:22:16.542: E/AndroidRuntime(1167):   at
> android.content.res.Resources.loadDrawable(Resources.java:2110) 05-16
> 14:22:16.542: E/AndroidRuntime(1167):     at
> android.content.res.Resources.getDrawable(Resources.java:700) 05-16
> 14:22:16.542: E/AndroidRuntime(1167):     at
> android.widget.ImageView.resolveUri(ImageView.java:638) 05-16
> 14:22:16.542: E/AndroidRuntime(1167):     at
> android.widget.ImageView.setImageResource(ImageView.java:367) 05-16
> 14:22:16.542: E/AndroidRuntime(1167):     at
> com.example.terceraniversario.Images.onCreate(Images.java:20) 05-16
> 14:22:16.542: E/AndroidRuntime(1167):     at
> android.app.Activity.performCreate(Activity.java:5231) 05-16
> 14:22:16.542: E/AndroidRuntime(1167):     at
> android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087)
> 05-16 14:22:16.542: E/AndroidRuntime(1167):   at
> android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2159)
> 05-16 14:22:16.542: E/AndroidRuntime(1167):   at
> android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2245)
> 05-16 14:22:16.542: E/AndroidRuntime(1167):   at
> android.app.ActivityThread.access$800(ActivityThread.java:135) 05-16
> 14:22:16.542: E/AndroidRuntime(1167):     at
> android.app.ActivityThread$H.handleMessage(ActivityThread.java:1196)
> 05-16 14:22:16.542: E/AndroidRuntime(1167):   at
> android.os.Handler.dispatchMessage(Handler.java:102) 05-16
> 14:22:16.542: E/AndroidRuntime(1167):     at
> android.os.Looper.loop(Looper.java:136) 05-16 14:22:16.542:
> E/AndroidRuntime(1167):   at
> android.app.ActivityThread.main(ActivityThread.java:5017) 05-16
> 14:22:16.542: E/AndroidRuntime(1167):     at
> java.lang.reflect.Method.invokeNative(Native Method) 05-16
> 14:22:16.542: E/AndroidRuntime(1167):     at
> java.lang.reflect.Method.invoke(Method.java:515) 05-16 14:22:16.542:
> E/AndroidRuntime(1167):   at
> com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:779)
> 05-16 14:22:16.542: E/AndroidRuntime(1167):   at
> com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595) 05-16
> 14:22:16.542: E/AndroidRuntime(1167):     at
> dalvik.system.NativeStart.main(Native Method) 05-16 14:22:35.702:
> E/dalvikvm-heap(1210): Out of memory on a 69120016-byte allocation.
> 05-16 14:22:35.782: E/AndroidRuntime(1210): FATAL EXCEPTION: main
> 05-16 14:22:35.782: E/AndroidRuntime(1210): Process:
> com.example.terceraniversario, PID: 1210 05-16 14:22:35.782:
> E/AndroidRuntime(1210): java.lang.OutOfMemoryError 05-16 14:22:35.782:
> E/AndroidRuntime(1210):   at
> android.graphics.BitmapFactory.nativeDecodeAsset(Native Method) 05-16
> 14:22:35.782: E/AndroidRuntime(1210):     at
> android.graphics.BitmapFactory.decodeStream(BitmapFactory.java:587)
> 05-16 14:22:35.782: E/AndroidRuntime(1210):   at
> android.graphics.BitmapFactory.decodeResourceStream(BitmapFactory.java:422)
> 05-16 14:22:35.782: E/AndroidRuntime(1210):   at
> android.graphics.drawable.Drawable.createFromResourceStream(Drawable.java:840)
> 05-16 14:22:35.782: E/AndroidRuntime(1210):   at
> android.content.res.Resources.loadDrawable(Resources.java:2110) 05-16
> 14:22:35.782: E/AndroidRuntime(1210):     at
> android.content.res.Resources.getDrawable(Resources.java:700) 05-16
> 14:22:35.782: E/AndroidRuntime(1210):     at
> android.widget.ImageView.resolveUri(ImageView.java:638) 05-16
> 14:22:35.782: E/AndroidRuntime(1210):     at
> android.widget.ImageView.setImageResource(ImageView.java:367) 05-16
> 14:22:35.782: E/AndroidRuntime(1210):     at
> com.example.terceraniversario.Images.onCreate(Images.java:20) 05-16
> 14:22:35.782: E/AndroidRuntime(1210):     at
> android.app.Activity.performCreate(Activity.java:5231) 05-16
> 14:22:35.782: E/AndroidRuntime(1210):     at
> android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087)
> 05-16 14:22:35.782: E/AndroidRuntime(1210):   at
> android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2159)
> 05-16 14:22:35.782: E/AndroidRuntime(1210):   at
> android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2245)
> 05-16 14:22:35.782: E/AndroidRuntime(1210):   at
> android.app.ActivityThread.access$800(ActivityThread.java:135) 05-16
> 14:22:35.782: E/AndroidRuntime(1210):     at
> android.app.ActivityThread$H.handleMessage(ActivityThread.java:1196)
> 05-16 14:22:35.782: E/AndroidRuntime(1210):   at
> android.os.Handler.dispatchMessage(Handler.java:102) 05-16
> 14:22:35.782: E/AndroidRuntime(1210):     at
> android.os.Looper.loop(Looper.java:136) 05-16 14:22:35.782:
> E/AndroidRuntime(1210):   at
> android.app.ActivityThread.main(ActivityThread.java:5017) 05-16
> 14:22:35.782: E/AndroidRuntime(1210):     at
> java.lang.reflect.Method.invokeNative(Native Method) 05-16
> 14:22:35.782: E/AndroidRuntime(1210):     at
> java.lang.reflect.Method.invoke(Method.java:515) 05-16 14:22:35.782:
> E/AndroidRuntime(1210):   at
> com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:779)
> 05-16 14:22:35.782: E/AndroidRuntime(1210):   at
> com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595) 05-16
> 14:22:35.782: E/AndroidRuntime(1210):     at
> dalvik.system.NativeStart.main(Native Method)

如果我将listaImagenes.get(0)更改为listaImagenes.get(1),则该应用会崩溃。 我不知道为什么会这样......

1 个答案:

答案 0 :(得分:0)

所以,既然你得到OutOfMemoryError,我猜它会与你尝试加载的资源的大小有关。我会说rosa1 drawable可能很大,而imagencilla drawable可能较小。这可以解释为什么您在尝试加载imagencilla时不会崩溃,但在尝试加载rosa1时会崩溃。

您应该验证APK中包含的rosa1 drawable的大小,并尽可能缩小它。如果缩小它是不可能的,您可能必须提出一种自定义方法来加载不会尝试将其全部解码到内存中的资源。