gpt4 book ai didi

java - Android:java.lang.OutOfMemoryError,当我尝试以编程方式设置 ImageView 时

转载 作者:行者123 更新时间:2023-12-01 12:14:31 32 4
gpt4 key购买 nike

我有下面的代码,它设置一些文本并加载图像。

代码:

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)

最佳答案

您正在尝试分配 37552400 字节 = 35.8MB。这太大了,大​​约相当于一张 8MP 照片。在许多设备上,这比整个应用程序的内存还要多,更不用说单个图像可以使用多少内存了。

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

关于java - Android:java.lang.OutOfMemoryError,当我尝试以编程方式设置 ImageView 时,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27093205/

32 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com