2014-11-23 88 views
0

我有下面的代码,它设置一些文本并加载图像。Android:java.lang.OutOfMemoryError,当我尝试以编程方式设置图像视图

代码:

private void buidAndroidComponentsFromRandom(int localVarRandomChoice){ 

     switch(localVarRandomChoice){ 

      case 0: 
         /* American food */ 
         titleForRandom = (TextView) findViewById(R.id.title); 
         logoForRandom = (ImageView) findViewById(R.id.imageView1); 
         addressForRandom = (TextView) findViewById(R.id.button_text); 
         foodImageForRandom = (ImageView) findViewById(R.id.numberDays); 
         foodDescForRandom = (TextView) findViewById(R.id.foodDesc); 
         titleForRandom.setText("How about Subway?"); 
         logoForRandom.setImageResource(R.drawable.subway); 
         addressForRandom.setText("Coffman Union\n3rd Floor\nWest bank"); 
         foodImageForRandom.setImageResource(R.drawable.sandwish); 
         foodDescForRandom.setText("Fish Sandich\n$5"); 

      case 1: 
         /* Cafe */ 
         titleForRandom = (TextView) findViewById(R.id.title); 
         logoForRandom = (ImageView) findViewById(R.id.imageView1); 
         addressForRandom = (TextView) findViewById(R.id.button_text); 
         foodImageForRandom = (ImageView) findViewById(R.id.numberDays); 
         foodDescForRandom = (TextView) findViewById(R.id.foodDesc); 
         titleForRandom.setText("How about Caribou Cafe?"); 
         logoForRandom.setImageResource(R.drawable.cariboulogo); 
         addressForRandom.setText("Coffman Union\n3rd Floor\nWest bank"); 
         foodImageForRandom.setImageResource(R.drawable.cariboufood); 
         foodDescForRandom.setText("Fish Sandich\n$5"); 

      case 2: 
         /* Chinese */ 
         titleForRandom = (TextView) findViewById(R.id.title); 
         logoForRandom = (ImageView) findViewById(R.id.imageView1); 
         addressForRandom = (TextView) findViewById(R.id.button_text); 
         foodImageForRandom = (ImageView) findViewById(R.id.numberDays); 
         foodDescForRandom = (TextView) findViewById(R.id.foodDesc); 
         titleForRandom.setText("How about Panda Express?"); 
         logoForRandom.setImageResource(R.drawable.pandaexpresslogo); 
         addressForRandom.setText("Coffman Union\n3rd Floor\nWest bank"); 
         foodImageForRandom.setImageResource(R.drawable.pandaexpressfood); 
         foodDescForRandom.setText("Fish Sandich\n$5"); 

      case 3: 
         /* Italian */ 
         titleForRandom = (TextView) findViewById(R.id.title); 
         logoForRandom = (ImageView) findViewById(R.id.imageView1); 
         addressForRandom = (TextView) findViewById(R.id.button_text); 
         foodImageForRandom = (ImageView) findViewById(R.id.numberDays); 
         foodDescForRandom = (TextView) findViewById(R.id.foodDesc); 
         titleForRandom.setText("How about Topios?"); 
         logoForRandom.setImageResource(R.drawable.topiologo); 
         addressForRandom.setText("Coffman Union\n3rd Floor\nWest bank"); 
         foodImageForRandom.setImageResource(R.drawable.topiofood); 
         foodDescForRandom.setText("Fish Sandich\n$5"); 

      case 4: 
         /* Juice */ 
         titleForRandom = (TextView) findViewById(R.id.title); 
         logoForRandom = (ImageView) findViewById(R.id.imageView1); 
         addressForRandom = (TextView) findViewById(R.id.button_text); 
         foodImageForRandom = (ImageView) findViewById(R.id.numberDays); 
         foodDescForRandom = (TextView) findViewById(R.id.foodDesc); 
         titleForRandom.setText("How about Jamba Juice?"); 
         logoForRandom.setImageResource(R.drawable.jambajuicelogo); 
         addressForRandom.setText("Coffman Union\n3rd Floor\nWest bank"); 
         foodImageForRandom.setImageResource(R.drawable.jambajuicefood); 
         foodDescForRandom.setText("Fish Sandich\n$5"); 

      case 5: 
         /* Mexican */ 
         titleForRandom = (TextView) findViewById(R.id.title); 
         logoForRandom = (ImageView) findViewById(R.id.imageView1); 
         addressForRandom = (TextView) findViewById(R.id.button_text); 
         foodImageForRandom = (ImageView) findViewById(R.id.numberDays); 
         foodDescForRandom = (TextView) findViewById(R.id.foodDesc); 
         titleForRandom.setText("How about Baja Sol?"); 
         logoForRandom.setImageResource(R.drawable.bajasollogo); 
         addressForRandom.setText("Coffman Union\n3rd Floor\nWest bank"); 
         foodImageForRandom.setImageResource(R.drawable.bajasolfood); 
         foodDescForRandom.setText("Fish Sandich\n$5"); 

      case 6: 
         /* Vietnamese */ 
         titleForRandom = (TextView) findViewById(R.id.title); 
         logoForRandom = (ImageView) findViewById(R.id.imageView1); 
         addressForRandom = (TextView) findViewById(R.id.button_text); 
         foodImageForRandom = (ImageView) findViewById(R.id.numberDays); 
         foodDescForRandom = (TextView) findViewById(R.id.foodDesc); 
         titleForRandom.setText("How about Bun Mi?"); 
         logoForRandom.setImageResource(R.drawable.bunmilogo); 
         addressForRandom.setText("Coffman Union\n3rd Floor\nWest bank"); 
         foodImageForRandom.setImageResource(R.drawable.bunmifood); 
         foodDescForRandom.setText("Fish Sandich\n$5"); 

      default: 
         /* Display some shit, who cares!! */ 
         titleForRandom = (TextView) findViewById(R.id.title); 
         logoForRandom = (ImageView) findViewById(R.id.imageView1); 
         addressForRandom = (TextView) findViewById(R.id.button_text); 
         foodImageForRandom = (ImageView) findViewById(R.id.numberDays); 
         foodDescForRandom = (TextView) findViewById(R.id.foodDesc); 
         titleForRandom.setText("How about Topios?"); 
         logoForRandom.setImageResource(R.drawable.topiologo); 
         addressForRandom.setText("Coffman Union\n3rd Floor\nWest bank"); 
         foodImageForRandom.setImageResource(R.drawable.topiofood); 
         foodDescForRandom.setText("Fish Sandich\n$5"); 
     } 

    } 

当我运行代码,我得到java.lang.OutOfMemoryError错误和应用程序崩溃。这是错误的堆栈跟踪。有谁能帮我吗?

11-23 13:14:01.479: D/dalvikvm(32489): GC_FOR_ALLOC freed 255K, 11% free 14003K/15600K, paused 12ms, total 12ms 
11-23 13:14:01.479: I/dalvikvm-heap(32489): Grow heap (frag case) to 17.788MB for 3047632-byte allocation 
11-23 13:14:01.499: D/dalvikvm(32489): GC_FOR_ALLOC freed 3K, 9% free 16975K/18580K, paused 18ms, total 18ms 
11-23 13:14:01.739: D/dalvikvm(32489): GC_FOR_ALLOC freed 20K, 9% free 16955K/18580K, paused 15ms, total 15ms 
11-23 13:14:01.739: I/dalvikvm-heap(32489): Grow heap (frag case) to 22.933MB for 5419408-byte allocation 
11-23 13:14:01.759: D/dalvikvm(32489): GC_FOR_ALLOC freed 0K, 7% free 22248K/23876K, paused 20ms, total 20ms 
11-23 13:14:01.894: D/dalvikvm(32489): GC_FOR_ALLOC freed 3350K, 21% free 19000K/23876K, paused 12ms, total 12ms 
11-23 13:14:01.899: I/dalvikvm-heap(32489): Grow heap (frag case) to 39.906MB for 21123232-byte allocation 
11-23 13:14:01.914: D/dalvikvm(32489): GC_FOR_ALLOC freed <1K, 11% free 39628K/44508K, paused 19ms, total 19ms 
11-23 13:14:01.914: D/AbsListView(32489): [unregisterDoubleTapMotionListener] 
11-23 13:14:01.914: I/MotionRecognitionManager(32489): .unregisterListener :/listener count = 0->0, 
11-23 13:14:01.919: D/AbsListView(32489): unregisterIRListener() is called 
11-23 13:14:02.134: D/dalvikvm(32489): GC_FOR_ALLOC freed 112K, 12% free 39518K/44508K, paused 12ms, total 12ms 
11-23 13:14:02.134: I/dalvikvm-heap(32489): Forcing collection of SoftReferences for 37552400-byte allocation 
11-23 13:14:02.154: D/dalvikvm(32489): GC_BEFORE_OOM freed 9K, 12% free 39508K/44508K, paused 21ms, total 21ms 
11-23 13:14:02.154: E/dalvikvm-heap(32489): Out of memory on a 37552400-byte allocation. 
11-23 13:14:02.154: I/dalvikvm(32489): "main" prio=5 tid=1 RUNNABLE 
11-23 13:14:02.154: I/dalvikvm(32489): | group="main" sCount=0 dsCount=0 obj=0x41b69578 self=0x41b546d0 
11-23 13:14:02.154: I/dalvikvm(32489): | sysTid=32489 nice=-16 sched=0/0 cgrp=apps handle=1075073020 
11-23 13:14:02.154: I/dalvikvm(32489): | state=R schedstat=(1495795700 225718854 2622) utm=111 stm=38 core=0 
11-23 13:14:02.154: I/dalvikvm(32489): at android.graphics.BitmapFactory.nativeDecodeAsset(Native Method) 
11-23 13:14:02.159: I/dalvikvm(32489): at android.graphics.BitmapFactory.decodeStream(BitmapFactory.java:596) 
11-23 13:14:02.159: I/dalvikvm(32489): at android.graphics.BitmapFactory.decodeResourceStream(BitmapFactory.java:444) 
11-23 13:14:02.159: I/dalvikvm(32489): at android.graphics.drawable.Drawable.createFromResourceStream(Drawable.java:832) 
11-23 13:14:02.159: I/dalvikvm(32489): at android.content.res.Resources.loadDrawable(Resources.java:2988) 
11-23 13:14:02.159: I/dalvikvm(32489): at android.content.res.Resources.getDrawable(Resources.java:1558) 
11-23 13:14:02.164: I/dalvikvm(32489): at android.widget.ImageView.resolveUri(ImageView.java:646) 
11-23 13:14:02.164: I/dalvikvm(32489): at android.widget.ImageView.setImageResource(ImageView.java:375) 
11-23 13:14:02.164: I/dalvikvm(32489): at com.example.foodu.CuisineRecommendation.buidAndroidComponentsFromRandom(CuisineRecommendation.java:108) 
11-23 13:14:02.164: I/dalvikvm(32489): at com.example.foodu.CuisineRecommendation.onCreateOptionsMenu(CuisineRecommendation.java:53) 
11-23 13:14:02.164: I/dalvikvm(32489): at android.app.Activity.onCreatePanelMenu(Activity.java:2578) 
11-23 13:14:02.164: I/dalvikvm(32489): at com.android.internal.policy.impl.PhoneWindow.preparePanel(PhoneWindow.java:507) 
11-23 13:14:02.164: I/dalvikvm(32489): at com.android.internal.policy.impl.PhoneWindow.doInvalidatePanelMenu(PhoneWindow.java:934) 
11-23 13:14:02.164: I/dalvikvm(32489): at com.android.internal.policy.impl.PhoneWindow$1.run(PhoneWindow.java:292) 
11-23 13:14:02.169: I/dalvikvm(32489): at android.view.Choreographer$CallbackRecord.run(Choreographer.java:791) 
11-23 13:14:02.169: I/dalvikvm(32489): at android.view.Choreographer.doCallbacks(Choreographer.java:591) 
11-23 13:14:02.169: I/dalvikvm(32489): at android.view.Choreographer.doFrame(Choreographer.java:560) 
11-23 13:14:02.169: I/dalvikvm(32489): at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:777) 
11-23 13:14:02.169: I/dalvikvm(32489): at android.os.Handler.handleCallback(Handler.java:730) 
11-23 13:14:02.169: I/dalvikvm(32489): at android.os.Handler.dispatchMessage(Handler.java:92) 
11-23 13:14:02.169: I/dalvikvm(32489): at android.os.Looper.loop(Looper.java:176) 
11-23 13:14:02.169: I/dalvikvm(32489): at android.app.ActivityThread.main(ActivityThread.java:5419) 
11-23 13:14:02.169: I/dalvikvm(32489): at java.lang.reflect.Method.invokeNative(Native Method) 
11-23 13:14:02.169: I/dalvikvm(32489): at java.lang.reflect.Method.invoke(Method.java:525) 
11-23 13:14:02.169: I/dalvikvm(32489): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1046) 
11-23 13:14:02.169: I/dalvikvm(32489): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:862) 
11-23 13:14:02.169: I/dalvikvm(32489): at dalvik.system.NativeStart.main(Native Method) 
11-23 13:14:02.169: D/skia(32489): --- allocation failed for scaled bitmap 
11-23 13:14:02.174: D/AndroidRuntime(32489): Shutting down VM 
11-23 13:14:02.174: W/dalvikvm(32489): threadid=1: thread exiting with uncaught exception (group=0x41b68700) 
11-23 13:14:02.179: E/AndroidRuntime(32489): FATAL EXCEPTION: main 
11-23 13:14:02.179: E/AndroidRuntime(32489): java.lang.OutOfMemoryError 
11-23 13:14:02.179: E/AndroidRuntime(32489): at android.graphics.BitmapFactory.nativeDecodeAsset(Native Method) 
11-23 13:14:02.179: E/AndroidRuntime(32489): at android.graphics.BitmapFactory.decodeStream(BitmapFactory.java:596) 
11-23 13:14:02.179: E/AndroidRuntime(32489): at android.graphics.BitmapFactory.decodeResourceStream(BitmapFactory.java:444) 
11-23 13:14:02.179: E/AndroidRuntime(32489): at android.graphics.drawable.Drawable.createFromResourceStream(Drawable.java:832) 
11-23 13:14:02.179: E/AndroidRuntime(32489): at android.content.res.Resources.loadDrawable(Resources.java:2988) 
11-23 13:14:02.179: E/AndroidRuntime(32489): at android.content.res.Resources.getDrawable(Resources.java:1558) 
11-23 13:14:02.179: E/AndroidRuntime(32489): at android.widget.ImageView.resolveUri(ImageView.java:646) 
11-23 13:14:02.179: E/AndroidRuntime(32489): at android.widget.ImageView.setImageResource(ImageView.java:375) 
11-23 13:14:02.179: E/AndroidRuntime(32489): at com.example.foodu.CuisineRecommendation.buidAndroidComponentsFromRandom(CuisineRecommendation.java:108) 
11-23 13:14:02.179: E/AndroidRuntime(32489): at com.example.foodu.CuisineRecommendation.onCreateOptionsMenu(CuisineRecommendation.java:53) 
11-23 13:14:02.179: E/AndroidRuntime(32489): at android.app.Activity.onCreatePanelMenu(Activity.java:2578) 
11-23 13:14:02.179: E/AndroidRuntime(32489): at com.android.internal.policy.impl.PhoneWindow.preparePanel(PhoneWindow.java:507) 
11-23 13:14:02.179: E/AndroidRuntime(32489): at com.android.internal.policy.impl.PhoneWindow.doInvalidatePanelMenu(PhoneWindow.java:934) 
11-23 13:14:02.179: E/AndroidRuntime(32489): at com.android.internal.policy.impl.PhoneWindow$1.run(PhoneWindow.java:292) 
11-23 13:14:02.179: E/AndroidRuntime(32489): at android.view.Choreographer$CallbackRecord.run(Choreographer.java:791) 
11-23 13:14:02.179: E/AndroidRuntime(32489): at android.view.Choreographer.doCallbacks(Choreographer.java:591) 
11-23 13:14:02.179: E/AndroidRuntime(32489): at android.view.Choreographer.doFrame(Choreographer.java:560) 
11-23 13:14:02.179: E/AndroidRuntime(32489): at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:777) 
11-23 13:14:02.179: E/AndroidRuntime(32489): at android.os.Handler.handleCallback(Handler.java:730) 
11-23 13:14:02.179: E/AndroidRuntime(32489): at android.os.Handler.dispatchMessage(Handler.java:92) 
11-23 13:14:02.179: E/AndroidRuntime(32489): at android.os.Looper.loop(Looper.java:176) 
11-23 13:14:02.179: E/AndroidRuntime(32489): at android.app.ActivityThread.main(ActivityThread.java:5419) 
11-23 13:14:02.179: E/AndroidRuntime(32489): at java.lang.reflect.Method.invokeNative(Native Method) 
11-23 13:14:02.179: E/AndroidRuntime(32489): at java.lang.reflect.Method.invoke(Method.java:525) 
11-23 13:14:02.179: E/AndroidRuntime(32489): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1046) 
11-23 13:14:02.179: E/AndroidRuntime(32489): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:862) 
11-23 13:14:02.179: E/AndroidRuntime(32489): at dalvik.system.NativeStart.main(Native Method) 
+0

图像有多大? – Simon 2014-11-23 19:22:04

回答

1

您正试图分配37552400字节= 35.8MB。那就是太大太大了,相当于一张800万像素的照片。在许多设备上,这比您的整个应用程序具有更多的内存,更不用说多少可用于单个映像。

将图像资源的分辨率降低到更合理的值。

+0

谢谢你的回复。但是,当我不以编程方式设置图像,[只需在布局中设置图像],它工作得很好。图像的大小也不是很大,以千字节为单位。仍然想知道为什么会发生这种情况 – Var 2014-11-23 19:25:57

+1

我不确定。但是您在案件中缺少break语句。它可能是一个问题?因为在这段代码运行时,程序试图在每种情况下设置图像... – 2014-11-23 19:28:51

+0

@Var:“图像的大小也不是太大,它以千字节为单位” - 这是磁盘上*的大小。 PNG和JPEG文件以压缩格式存储在磁盘上。在内存中,它们会消耗比磁盘上多得多的空间。此外,操作系统需要35.8MB来加载你的图片,无论你喜欢与否。 Receme的观点也是有效的,因为你不必要地多次调用'setImageResource()',导致更多的内存消耗。 – CommonsWare 2014-11-23 19:29:43

相关问题