gpt4 book ai didi

android - 外部分配对于这个过程来说太大了,错误膨胀类,位图大小超过 VM 预算

转载 作者:塔克拉玛干 更新时间:2023-11-02 23:08:38 26 4
gpt4 key购买 nike

发布了我的问题 here too.

一个初学者..我正在做一个学校 map 应用程序。

首先,我的背景有 4 个按钮(level3、level4、level5、level6)。

我尝试点击所有的按钮,通常在点击3个之后,下一个会崩溃。(每个级别按钮进入一个级别 map ,点击房间图像按钮,我将进入gallery-imageswitcher)

我在每个关卡 map 中至少有 5 个 ImageSwitcher,因此总共有大约 20 多个 ImageSwitcher 画廊。

我使用 Drawable 而不是 Bitmap 来完成我所有的工作。我有很多图片(因为它是学校 map ,显示我学校的所有照片,以及所有这些背景和 ImageButton)

我做了一些研究,有问题的是那些使用位图的。一些可以解决的方法是 system.gc();bitmap.recycle(虽然不太确定如何使用它)

在我的主后台-starttour.java

 public class StartTour extends Activity {  
private SoundManager mSoundManager;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.ttour);

mSoundManager = new SoundManager();
mSoundManager.initSounds(getBaseContext());
mSoundManager.addSound(1, R.raw.beep);

//play music
final MediaPlayer mPlayer = MediaPlayer.create(this, R.raw.jazz);
mPlayer.start();

final Button button = (Button) findViewById(R.id.back);

button.setOnClickListener(new Button.OnClickListener() {

// Implement the OnClickListener callback
public void onClick(View v) {
// do something when the button is clicked
startActivity(new Intent("android.com.Android"));
mSoundManager.playSound(1);
mPlayer.stop();
}
});

final Button button1 = (Button) findViewById(R.id.level3);
button1.setOnClickListener(new Button.OnClickListener() {
public void onClick(View v) {
// Perform action on click
startActivity(new Intent("android.com.Lvl3"));
mSoundManager.playSound(1);
mPlayer.stop();

}
});

final Button button2 = (Button) findViewById(R.id.level4);
button2.setOnClickListener(new Button.OnClickListener() {
public void onClick(View v) {
// Perform action on click
startActivity(new Intent("android.com.Lvl4"));
mSoundManager.playSound(1);
mPlayer.stop();

}
});


final Button button3 = (Button) findViewById(R.id.level5);
button3.setOnClickListener(new Button.OnClickListener() {
public void onClick(View v) {
// Perform action on click
startActivity(new Intent("android.com.Lvl5"));
mSoundManager.playSound(1);
mPlayer.stop();

}
});

final Button button4 = (Button) findViewById(R.id.level6);
button4.setOnClickListener(new Button.OnClickListener() {
public void onClick(View v) {
// Perform action on click
startActivity(new Intent("android.com.Lvl6"));
mSoundManager.playSound(1);
mPlayer.stop();

}
});
}
}





<ImageView android:id="@+id/ilevel4" android:layout_width="wrap_content"
android:layout_height="fill_parent" android:src="@drawable/level4map"
android:layout_gravity="center"/>

<Button
android:id="@+id/m412button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="92.5px"
android:layout_marginTop="95px"
android:background="@drawable/m412_button" />

<Button
android:id="@+id/m411button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="92.5px"
android:layout_marginTop="248.8px"
android:background="@drawable/m411_button" />

<Button
android:id="@+id/m410button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="92.5px"
android:layout_marginTop="334.8px"
android:background="@drawable/m410_button" />


<Button
android:id="@+id/m405button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="6px"
android:layout_marginTop="246.5px"
android:background="@drawable/m405_button" />

<Button
android:id="@+id/m406button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="6px"
android:layout_marginTop="335px"
android:background="@drawable/m406_button" />

<Button
android:id="@+id/m419button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="260px"
android:layout_marginTop="59.5px"
android:background="@drawable/m419_button" />

<Button
android:id="@+id/m420button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="260px"
android:layout_marginTop="168.5px"
android:background="@drawable/m420_button" />

<Button
android:id="@+id/m421button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="260px"
android:layout_marginTop="240.5px"
android:background="@drawable/m421_button" />

<Button
android:id="@+id/m422button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="260px"
android:layout_marginTop="335px"
android:background="@drawable/m422_button" />
</RelativeLayout>

在我的关卡 4.xml 中

     <ImageView android:id="@+id/ilevel4" android:layout_width="wrap_content"  
android:layout_height="fill_parent" android:src="@drawable/level4map"
android:layout_gravity="center"/>

<Button
android:id="@+id/m412button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="92.5px"
android:layout_marginTop="95px"
android:background="@drawable/m412_button" />

<Button
android:id="@+id/m411button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="92.5px"
android:layout_marginTop="248.8px"
android:background="@drawable/m411_button" />

<Button
android:id="@+id/m410button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="92.5px"
android:layout_marginTop="334.8px"
android:background="@drawable/m410_button" />


<Button
android:id="@+id/m405button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="6px"
android:layout_marginTop="246.5px"
android:background="@drawable/m405_button" />

<Button
android:id="@+id/m406button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="6px"
android:layout_marginTop="335px"
android:background="@drawable/m406_button" />

<Button
android:id="@+id/m419button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="260px"
android:layout_marginTop="59.5px"
android:background="@drawable/m419_button" />

<Button
android:id="@+id/m420button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="260px"
android:layout_marginTop="168.5px"
android:background="@drawable/m420_button" />

<Button
android:id="@+id/m421button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="260px"
android:layout_marginTop="240.5px"
android:background="@drawable/m421_button" />

<Button
android:id="@+id/m422button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="260px"
android:layout_marginTop="335px"
android:background="@drawable/m422_button" />
</RelativeLayout>

在level4.java中

  public class Level4 extends Activity {  
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.tlevel4);

final Button button1 = (Button) findViewById(R.id.m405button);
button1.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
// Perform action on click
startActivity(new Intent("android.com.M405"));
}
});

final Button button2 = (Button) findViewById(R.id.m406button);
button2.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
// Perform action on click
startActivity(new Intent("android.com.M406"));
}
});
}
}

在 logcat 中

11-03 12:35:55.194: ERROR/dalvikvm-heap(708): 483406-byte external allocation too large for this process.
11-03 12:35:55.194: ERROR/(708): VM won't let us allocate 483406 bytes
11-03 12:35:55.204: DEBUG/AndroidRuntime(708): Shutting down VM
11-03 12:35:55.213: WARN/dalvikvm(708): threadid=3: thread exiting with uncaught exception (group=0x4000fe70)
11-03 12:35:55.213: ERROR/AndroidRuntime(708): Uncaught handler: thread main exiting due to uncaught exception
11-03 12:35:55.254: ERROR/AndroidRuntime(708): java.lang.RuntimeException: Unable to start activity ComponentInfo{android.com/android.com.Level3}: android.view.InflateException: Binary XML file line #7: Error inflating class java.lang.reflect.Constructor
11-03 12:35:55.254: ERROR/AndroidRuntime(708): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2268)
11-03 12:35:55.254: ERROR/AndroidRuntime(708): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2284)
11-03 12:35:55.254: ERROR/AndroidRuntime(708): at android.app.ActivityThread.access$1800(ActivityThread.java:112)
11-03 12:35:55.254: ERROR/AndroidRuntime(708): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1692)
11-03 12:35:55.254: ERROR/AndroidRuntime(708): at android.os.Handler.dispatchMessage(Handler.java:99)
11-03 12:35:55.254: ERROR/AndroidRuntime(708): at android.os.Looper.loop(Looper.java:123)
11-03 12:35:55.254: ERROR/AndroidRuntime(708): at android.app.ActivityThread.main(ActivityThread.java:3948)
11-03 12:35:55.254: ERROR/AndroidRuntime(708): at java.lang.reflect.Method.invokeNative(Native Method)
11-03 12:35:55.254: ERROR/AndroidRuntime(708): at java.lang.reflect.Method.invoke(Method.java:521)
11-03 12:35:55.254: ERROR/AndroidRuntime(708): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:782)
11-03 12:35:55.254: ERROR/AndroidRuntime(708): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:540)
11-03 12:35:55.254: ERROR/AndroidRuntime(708): at dalvik.system.NativeStart.main(Native Method)
11-03 12:35:55.254: ERROR/AndroidRuntime(708): Caused by: android.view.InflateException: Binary XML file line #7: Error inflating class java.lang.reflect.Constructor
11-03 12:35:55.254: ERROR/AndroidRuntime(708): at android.view.LayoutInflater.createView(LayoutInflater.java:512)
11-03 12:35:55.254: ERROR/AndroidRuntime(708): at com.android.internal.policy.impl.PhoneLayoutInflater.onCreateView(PhoneLayoutInflater.java:56)
11-03 12:35:55.254: ERROR/AndroidRuntime(708): at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:562)
11-03 12:35:55.254: ERROR/AndroidRuntime(708): at android.view.LayoutInflater.rInflate(LayoutInflater.java:617)
11-03 12:35:55.254: ERROR/AndroidRuntime(708): at android.view.LayoutInflater.inflate(LayoutInflater.java:407)
11-03 12:35:55.254: ERROR/AndroidRuntime(708): at android.view.LayoutInflater.inflate(LayoutInflater.java:320)
11-03 12:35:55.254: ERROR/AndroidRuntime(708): at android.view.LayoutInflater.inflate(LayoutInflater.java:276)
11-03 12:35:55.254: ERROR/AndroidRuntime(708): at com.android.internal.policy.impl.PhoneWindow.setContentView(PhoneWindow.java:309)
11-03 12:35:55.254: ERROR/AndroidRuntime(708): at android.app.Activity.setContentView(Activity.java:1626)
11-03 12:35:55.254: ERROR/AndroidRuntime(708): at android.com.Level3.onCreate(Level3.java:19)
11-03 12:35:55.254: ERROR/AndroidRuntime(708): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1123)
11-03 12:35:55.254: ERROR/AndroidRuntime(708): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2231)
11-03 12:35:55.254: ERROR/AndroidRuntime(708): ... 11 more
11-03 12:35:55.254: ERROR/AndroidRuntime(708): Caused by: java.lang.reflect.InvocationTargetException
11-03 12:35:55.254: ERROR/AndroidRuntime(708): at android.widget.ImageView.<init>(ImageView.java:103)
11-03 12:35:55.254: ERROR/AndroidRuntime(708): at java.lang.reflect.Constructor.constructNative(Native Method)
11-03 12:35:55.254: ERROR/AndroidRuntime(708): at java.lang.reflect.Constructor.newInstance(Constructor.java:446)
11-03 12:35:55.254: ERROR/AndroidRuntime(708): at android.view.LayoutInflater.createView(LayoutInflater.java:499)
11-03 12:35:55.254: ERROR/AndroidRuntime(708): ... 22 more
11-03 12:35:55.254: ERROR/AndroidRuntime(708): Caused by: java.lang.OutOfMemoryError: bitmap size exceeds VM budget
11-03 12:35:55.254: ERROR/AndroidRuntime(708): at android.graphics.BitmapFactory.nativeDecodeAsset(Native Method)
11-03 12:35:55.254: ERROR/AndroidRuntime(708): at android.graphics.BitmapFactory.decodeStream(BitmapFactory.java:363)
11-03 12:35:55.254: ERROR/AndroidRuntime(708): at android.graphics.BitmapFactory.decodeStream(BitmapFactory.java:212)
11-03 12:35:55.254: ERROR/AndroidRuntime(708): at android.graphics.drawable.Drawable.createFromResourceStream(Drawable.java:663)
11-03 12:35:55.254: ERROR/AndroidRuntime(708): at android.content.res.Resources.loadDrawable(Resources.java:1637)
11-03 12:35:55.254: ERROR/AndroidRuntime(708): at android.content.res.TypedArray.getDrawable(TypedArray.java:548)
11-03 12:35:55.254: ERROR/AndroidRuntime(708): at android.widget.ImageView.<init>(ImageView.java:113)
11-03 12:35:55.254: ERROR/AndroidRuntime(708): ... 26 more

如果我先点击任何按钮并进入关卡 map ,它会在我查看 imageswitcher 图片时崩溃。

在日志中

11-03 09:17:34.308: INFO/ActivityManager(564): Starting activity: Intent { action=android.com.M615 comp={android.com/android.com.M615_gallery} }
11-03 09:17:34.638: ERROR/dalvikvm-heap(712): 212800-byte external allocation too large for this process.
11-03 09:17:34.638: ERROR/(712): VM won't let us allocate 212800 bytes
11-03 09:17:34.638: DEBUG/AndroidRuntime(712): Shutting down VM
11-03 09:17:34.648: WARN/dalvikvm(712): threadid=3: thread exiting with uncaught exception (group=0x4000fe70)
11-03 09:17:34.648: ERROR/AndroidRuntime(712): Uncaught handler: thread main exiting due to uncaught exception
11-03 09:17:34.677: ERROR/AndroidRuntime(712): java.lang.OutOfMemoryError: bitmap size exceeds VM budget
11-03 09:17:34.677: ERROR/AndroidRuntime(712): at android.graphics.BitmapFactory.nativeDecodeAsset(Native Method)
11-03 09:17:34.677: ERROR/AndroidRuntime(712): at android.graphics.BitmapFactory.decodeStream(BitmapFactory.java:363)
11-03 09:17:34.677: ERROR/AndroidRuntime(712): at android.graphics.BitmapFactory.decodeStream(BitmapFactory.java:212)
11-03 09:17:34.677: ERROR/AndroidRuntime(712): at android.graphics.drawable.Drawable.createFromResourceStream(Drawable.java:663)
11-03 09:17:34.677: ERROR/AndroidRuntime(712): at android.content.res.Resources.loadDrawable(Resources.java:1637)
11-03 09:17:34.677: ERROR/AndroidRuntime(712): at android.content.res.Resources.getDrawable(Resources.java:535)
11-03 09:17:34.677: ERROR/AndroidRuntime(712): at android.widget.ImageView.resolveUri(ImageView.java:482)
11-03 09:17:34.677: ERROR/AndroidRuntime(712): at android.widget.ImageView.setImageResource(ImageView.java:268)
11-03 09:17:34.677: ERROR/AndroidRuntime(712): at android.com.M615_gallery$AddImgAdp.getView(M615_gallery.java:71)
11-03 09:17:34.677: ERROR/AndroidRuntime(712): at android.widget.Gallery.makeAndAddView(Gallery.java:754)
11-03 09:17:34.677: ERROR/AndroidRuntime(712): at android.widget.Gallery.fillToGalleryRight(Gallery.java:703)
11-03 09:17:34.677: ERROR/AndroidRuntime(712): at android.widget.Gallery.layout(Gallery.java:634)
11-03 09:17:34.677: ERROR/AndroidRuntime(712): at android.widget.Gallery.onLayout(Gallery.java:339)
11-03 09:17:34.677: ERROR/AndroidRuntime(712): at android.view.View.layout(View.java:6133)
11-03 09:17:34.677: ERROR/AndroidRuntime(712): at android.widget.LinearLayout.setChildFrame(LinearLayout.java:1119)
11-03 09:17:34.677: ERROR/AndroidRuntime(712): at android.widget.LinearLayout.layoutVertical(LinearLayout.java:998)
11-03 09:17:34.677: ERROR/AndroidRuntime(712): at android.widget.LinearLayout.onLayout(LinearLayout.java:918)
11-03 09:17:34.677: ERROR/AndroidRuntime(712): at android.view.View.layout(View.java:6133)
11-03 09:17:34.677: ERROR/AndroidRuntime(712): at android.widget.FrameLayout.onLayout(FrameLayout.java:333)
11-03 09:17:34.677: ERROR/AndroidRuntime(712): at android.view.View.layout(View.java:6133)
11-03 09:17:34.677: ERROR/AndroidRuntime(712): at android.widget.LinearLayout.setChildFrame(LinearLayout.java:1119)
11-03 09:17:34.677: ERROR/AndroidRuntime(712): at android.widget.LinearLayout.layoutVertical(LinearLayout.java:998)
11-03 09:17:34.677: ERROR/AndroidRuntime(712): at android.widget.LinearLayout.onLayout(LinearLayout.java:918)
11-03 09:17:34.677: ERROR/AndroidRuntime(712): at android.view.View.layout(View.java:6133)
11-03 09:17:34.677: ERROR/AndroidRuntime(712): at android.widget.FrameLayout.onLayout(FrameLayout.java:333)
11-03 09:17:34.677: ERROR/AndroidRuntime(712): at android.view.View.layout(View.java:6133)
11-03 09:17:34.677: ERROR/AndroidRuntime(712): at android.view.ViewRoot.performTraversals(ViewRoot.java:929)
11-03 09:17:34.677: ERROR/AndroidRuntime(712): at android.view.ViewRoot.handleMessage(ViewRoot.java:1482)
11-03 09:17:34.677: ERROR/AndroidRuntime(712): at android.os.Handler.dispatchMessage(Handler.java:99)
11-03 09:17:34.677: ERROR/AndroidRuntime(712): at android.os.Looper.loop(Looper.java:123)
11-03 09:17:34.677: ERROR/AndroidRuntime(712): at android.app.ActivityThread.main(ActivityThread.java:3948)
11-03 09:17:34.677: ERROR/AndroidRuntime(712): at java.lang.reflect.Method.invokeNative(Native Method)
11-03 09:17:34.677: ERROR/AndroidRuntime(712): at java.lang.reflect.Method.invoke(Method.java:521)
11-03 09:17:34.677: ERROR/AndroidRuntime(712): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:782)
11-03 09:17:34.677: ERROR/AndroidRuntime(712): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:540)
11-03 09:17:34.677: ERROR/AndroidRuntime(712): at dalvik.system.NativeStart.main(Native Method)

如果有人能提供帮助,我真的很感激..谢谢

PS:我的应用程序中有大量图片。我在每个级别中至少有 5 个 ImageSwitcher 画廊,所以我的应用程序中总计有 20 多个图像切换器。

最佳答案

我必须在这里做一些猜测,因为您的命名和行号并不完全匹配。

你有这一行,例如:

startActivity(new Intent("android.com.Lvl4"));

但是您的 Activity 名为“android.com.Level4”。当您将布局称为“level4.xml”时,您正在膨胀布局“tlevel4”。另外,您粘贴的错误是针对 Level3 的。也许它们相似,但行号很重要,所以最好发布相同的错误和类。

我并不是要挑剔,这些细节在尝试破译您不熟悉的代码时会产生很大的不同。

无论如何,在这种情况下,我认为很清楚发生了什么。

错误 1 ​​和 2这两个错误都是内存不足异常。在这两种情况下,您都要求操作系统为您提供加载一些大型资源的内存,而操作系统只是告诉您您已经受够了。如果您发现它一直在发生,那么几乎可以肯定您在某处泄漏了内存。您提到广泛使用可绘制对象。您要非常小心地处理这些,因为当您将可绘制对象添加到 View 时,它会保留对该 View 的引用……它具有对上下文的引用。我在你发布的代码中没有看到任何明显的东西,但我敢打赌你有某种上下文泄漏。阅读this article看看他们谈论的内容是否响起。

此外,我注意到您的包名称是 android.com(即 android.com.Lvl3)。也许您只是为了发布而编辑了它,但它确实应该以 com 开头,即 com.yourProjectName.Lvl3。

哦,请不要这样做:

startActivity(new Intent("android.com.Android"));

只需调用 this.finish();

关于android - 外部分配对于这个过程来说太大了,错误膨胀类,位图大小超过 VM 预算,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4087367/

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