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

답변

1

37552400 바이트 = 35.8MB를 할당하려고합니다. 즉 이 너무 커서, 약 8MP 사진과 동일합니다. 많은 기기에서 전체 이미지가 하나의 이미지에 사용할 수있는 양보다 많은 메모리를 사용합니다.

이미지 자원의 해상도를 좀 더 합리적으로 줄입니다.

+0

답장을 보내 주셔서 감사합니다. 그러나 이미지를 프로그래밍 방식으로 설정하지 않으면 레이아웃에서 이미지를 설정하면 완벽하게 작동합니다. 또한 이미지의 크기가 너무 크지 않아서 킬로바이트 단위입니다. 아직도 이것이 왜 일어나고 있는지 궁금하다. – Var

+1

나는 실제로 확신하지 못한다. 경우에 따라 break 문이 누락되었습니다. 그것은 문제가 될 수 있습니까 ?? 프로그램이이 코드가 실행될 때 각각의 경우에 이미지를 설정하려고하기 때문에 ... –

+0

@Var : "이미지의 크기가 너무 크지 않습니다. 이는 킬로바이트 단위입니다."즉 디스크 크기 *입니다. PNG 및 JPEG 파일은 압축 된 형식으로 디스크에 저장됩니다. 메모리에서, 그들은 디스크보다 훨씬 더 많은 공간을 소비 할 것입니다. 또한 OS가 마음에 드는지 여부에 관계없이 이미지에 35.8MB가 필요합니다. Recem의 요점은'setImageResource()'를 불필요하게 여러 번 호출한다는 점에서 유효하다. 그래서 더 많은 메모리를 소비하게된다. – CommonsWare