简体   繁体   English

加载Android Activity用户界面时出现Java内存不足错误

[英]Java Out of Memory Error while loading the UI of an Android Activity

I have been trying to load a very basic activity with on ImageButton, 2 textViews, a textField and an ImageView. 我一直在尝试使用ImageButton,2个textViews,一个textField和一个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" />

And in MainActivity.java's OnCreate function I am simply calling setContentView(R.layout.activity_main); 在MainActivity.java的OnCreate函数中,我只是调用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). 但是,我的应用程序的内存使用情况图显示我正在使用约44MB(尽管应用程序中所有图像的总大小都小于1MB,用于ImageButton的图像大小为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. 请帮助我找出问题所在以及如何解决此问题。

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: 当图片很大并且将它们放置在drawable(ldpi / mdpi ...)文件夹中时,可能会发生这种情况,因此android操作系统倾向于根据需要调整它们的大小,但是在您的情况下,您说它们很小,请仍然尝试放置这些图片内:

res/drawable-nodpi

folder as it avoids them from rescaling and to be used as it is. 文件夹,因为这样可以避免它们重新缩放并按原样使用。

It might require more memory space after the images are decoded to bitmap, you can use bitmap options, modify the parameters to resize it; 将图像解码为位图后,可能需要更多的存储空间,您可以使用位图选项,修改参数以调整其大小;

You might want to clean and refresh your project; 您可能需要cleanrefresh您的项目。

Check the res folder as people said; 如人们所说,检查res文件夹;

Refer this to study more: https://developer.android.com/training/displaying-bitmaps/manage-memory.html 请参考以进行更多研究: https : //developer.android.com/training/displaying-bitmaps/manage-memory.html

It is highly probable that the drawables you are using cause this problem. 您正在使用的可绘制对象很可能导致此问题。 You may want to refer to his question Android Outofmemory drawable 您可能要参考他的问题: Android Outofmemory drawable

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM