2014-11-23 89 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

相關問題