Java的内存不足错误加载一个Android活动的UI [英] Java Out of Memory Error while loading the UI of an Android Activity

查看:156
本文介绍了Java的内存不足错误加载一个Android活动的UI的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在试图加载一个非常基本活动上的ImageButton,2 textViews,一个文本框和一个ImageView的。

I have been trying to load a very basic activity with on ImageButton, 2 textViews, a textField and an ImageView.

    <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent"
android:layout_height="match_parent"
android:clickable="false"
android:id="@+id/namescreenactivity">

<TextView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:textAppearance="?android:attr/textAppearanceLarge"
    android:text="Welcome to your Pet House"
    android:id="@+id/Welcome"
    android:textColor="#ff000000"
    android:textStyle="bold"
    android:layout_marginTop="94dp"
    android:layout_alignParentTop="true"
    android:layout_centerHorizontal="true" />

<EditText android:id="@+id/edit_message"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:hint="@string/edit_message"
    android:layout_marginTop="36dp"
    android:textColor="#ff000000"
    android:layout_below="@+id/Welcome"
    android:layout_alignEnd="@+id/Welcome" />

<TextView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:textAppearance="?android:attr/textAppearanceMedium"
    android:text="Name your pet"
    android:id="@+id/NameLabel"
    android:layout_alignTop="@+id/edit_message"
    android:layout_alignStart="@+id/Welcome" />

<ImageView
    android:layout_width="100dp"
    android:layout_height="100dp"
    android:src="@drawable/sun"
    android:layout_alignBottom="@+id/Welcome"
    android:layout_alignParentEnd="true"
    android:layout_marginBottom="39dp"
    android:id="@+id/sun" />

<ImageButton
    android:layout_width="250dp"
    android:layout_height="250dp"
    android:id="@+id/imageButton"
    android:src="@drawable/camel"
    android:clickable="true"
    android:scaleType="fitXY"
    android:onClick="sendMessage"
    android:layout_alignParentBottom="true"
    android:layout_alignEnd="@+id/edit_message"
    android:layout_marginBottom="31dp" />

和在MainActivity.java的OnCreate函数,我简单的调用的setContentView(R.layout.activity_main);
但是,我的应用程序的内存使用情况图显示,我44MB左右使用(虽然我的所有图像应用中的总规模小于1MB,并用于ImageButton的是193KB的)。

And in MainActivity.java's OnCreate function I am simply calling setContentView(R.layout.activity_main); However, my application's memory usage graph shows that I am utilizing around 44MB (Although the total size of all my images in the app is less than 1MB and the one used for the ImageButton is 193KB).

我认为,这是什么原因我的应用程序崩溃并引发内存不足的错误

I believe that this is the reason my app crashes and throws an Out of Memory error

Caused by: java.lang.OutOfMemoryError: Failed to allocate a 138018828 byte allocation with 4194304 free bytes and 35MB until OOM
        at dalvik.system.VMRuntime.newNonMovableArray(Native Method)
        at android.graphics.BitmapFactory.nativeDecodeAsset(Native Method)
        at android.graphics.BitmapFactory.decodeStream(BitmapFactory.java:609)
        at android.graphics.BitmapFactory.decodeResourceStream(BitmapFactory.java:444)
        at android.graphics.drawable.Drawable.createFromResourceStream(Drawable.java:988)
        at android.content.res.Resources.loadDrawableForCookie(Resources.java:2474)
        at android.content.res.Resources.loadDrawable(Resources.java:2381)
        at android.content.res.TypedArray.getDrawable(TypedArray.java:749)
at android.widget.ImageView.<init>(ImageView.java:146)
at android.widget.ImageButton.<init>(ImageButton.java:86)
at android.widget.ImageButton.<init>(ImageButton.java:82)
at android.widget.ImageButton.<init>(ImageButton.java:78)
at java.lang.reflect.Constructor.newInstance(Native Method)
at java.lang.reflect.Constructor.newInstance(Constructor.java:288)
at android.view.LayoutInflater.createView(LayoutInflater.java:607)
at com.android.internal.policy.impl.PhoneLayoutInflater.onCreateView(PhoneLayoutInflater.java:55)
at android.view.LayoutInflater.onCreateView(LayoutInflater.java:682)
at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:741)
at android.view.LayoutInflater.rInflate(LayoutInflater.java:806)
at android.view.LayoutInflater.inflate(LayoutInflater.java:504)
at android.view.LayoutInflater.inflate(LayoutInflater.java:414)
at android.view.LayoutInflater.inflate(LayoutInflater.java:365)
at com.android.internal.policy.impl.PhoneWindow.setContentView(PhoneWindow.java:378)
at android.app.Activity.setContentView(Activity.java:2145)
at com.example.healthypetdhr.MainActivity.onCreate(MainActivity.java:37)
at android.app.Activity.performCreate(Activity.java:5990)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1106)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2278)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2390)
at android.app.ActivityThread.access$800(ActivityThread.java:151)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1303)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:135)
at android.app.ActivityThread.main(ActivityThread.java:5257)
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(Method.java:372) 
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:903)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:698)'

请帮我找出什么是错的,我怎么能解决这个问题。

Please help me figure out what's wrong and how I can resolve this issue.

推荐答案

有可能发生时,图像大,当他们被安置在绘(LDPI / MDPI ...)文件夹,以便Android操作系统往往会调整它们的大小根据需要,但在你的情况下,你说他们是非常小的,还是尽量把这些图片里:

it may happen when Images are big and when they are placed in drawable(ldpi/mdpi...) folder so android OS tends to resize them according to need, but in your case you said they are very small, still try putting those images inside:

res/drawable-nodpi

夹,因为它避免它们从重新缩放,也可以使用,因为它是

folder as it avoids them from rescaling and to be used as it is.

这篇关于Java的内存不足错误加载一个Android活动的UI的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆