android - OutOfMemory异常android

标签 android exception out-of-memory

我在 android playstore 上发布了一个应用程序,很多人都没有问题地尝试过它,但是在 android 手机(Galaxy note III)上应用程序卡住了,我在我的开发者控制台上的 android 帐户中收到了这条消息。

 java.lang.RuntimeException: Unable to start activity ComponentInfo{org.cade.codigos/org.cade.codigos.ui.activities.RequestDataForm}: android.view.InflateException: Binary XML file line #11: Error inflating class android.widget.RelativeLayout
    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2294)
    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2348)
    at android.app.ActivityThread.access$700(ActivityThread.java:159)
    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1316)
    at android.os.Handler.dispatchMessage(Handler.java:99)
    at android.os.Looper.loop(Looper.java:137)
    at android.app.ActivityThread.main(ActivityThread.java:5414)
    at java.lang.reflect.Method.invokeNative(Native Method)
    at java.lang.reflect.Method.invoke(Method.java:525)
    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1187)
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1003)
    at dalvik.system.NativeStart.main(Native Method)
Caused by: android.view.InflateException: Binary XML file line #11: Error inflating class android.widget.RelativeLayout
    at android.view.LayoutInflater.createView(LayoutInflater.java:626)
    at com.android.internal.policy.impl.PhoneLayoutInflater.onCreateView(PhoneLayoutInflater.java:56)
    at android.view.LayoutInflater.onCreateView(LayoutInflater.java:675)
    at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:700)
    at android.view.LayoutInflater.rInflate(LayoutInflater.java:761)
    at android.view.LayoutInflater.rInflate(LayoutInflater.java:769)
    at android.view.LayoutInflater.inflate(LayoutInflater.java:498)
    at android.view.LayoutInflater.inflate(LayoutInflater.java:398)
    at android.view.LayoutInflater.inflate(LayoutInflater.java:354)
    at com.android.internal.policy.impl.PhoneWindow.setContentView(PhoneWindow.java:361)
    at android.app.Activity.setContentView(Activity.java:1956)
    at org.cade.codigos.ui.activities.RequestDataForm.onCreate(Unknown Source)
    at android.app.Activity.performCreate(Activity.java:5369)
    at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1104)
    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2257)
    ... 11 more
Caused by: java.lang.reflect.InvocationTargetException
    at java.lang.reflect.Constructor.constructNative(Native Method)
    at java.lang.reflect.Constructor.newInstance(Constructor.java:417)
    at android.view.LayoutInflater.createView(LayoutInflater.java:600)
    ... 25 more
Caused by: java.lang.OutOfMemoryError
    at android.graphics.BitmapFactory.nativeDecodeAsset(Native Method)
    at android.graphics.BitmapFactory.decodeStream(BitmapFactory.java:596)
    at android.graphics.BitmapFactory.decodeResourceStream(BitmapFactory.java:444)
    at android.graphics.drawable.Drawable.createFromResourceStream(Drawable.java:832)
    at android.content.res.Resources.loadDrawable(Resources.java:2988)
    at android.content.res.TypedArray.getDrawable(TypedArray.java:602)
    at android.view.View.<init>(View.java:3563)
    at android.view.View.<init>(View.java:3492)
    at android.view.ViewGroup.<init>(ViewGroup.java:469)
    at android.widget.RelativeLayout.<init>(RelativeLayout.java:242)
    ... 28 more

这是我的布局

<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/ScrollView01"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:fillViewport="true"
    android:scrollbars="none" >

<LinearLayout android:layout_width="match_parent"
    android:layout_height="match_parent">
<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:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    android:paddingBottom="@dimen/activity_vertical_margin"
    tools:context="org.cade.codigos.ui.activities.RequestDataForm"
    android:background="@drawable/background">

    <TextView
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:textAppearance="?android:attr/textAppearanceMedium"
        android:text="NOMBRE*"
        android:id="@+id/lblNombre"
        android:layout_marginTop="80dp"
        android:layout_alignParentTop="true"
        android:layout_alignParentRight="true"
        android:textSize="15dp"
        android:textColor="#e7f5fe"
        android:layout_marginBottom="5dp"
        android:layout_alignParentLeft="true"
        android:layout_marginLeft="10dp" />


    <EditText
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:inputType="textPersonName"
        android:ems="10"
        android:id="@+id/txtNombre"

        android:hint="@string/hint_nombre"
        android:layout_below="@+id/lblNombre"
        android:layout_alignParentRight="true" />


    <TextView
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:textAppearance="?android:attr/textAppearanceMedium"
        android:text="APELLIDO*"
        android:id="@+id/lblApellido"
        android:layout_below="@+id/txtNombre"
        android:layout_alignParentRight="true"
        android:textSize="15dp"
        android:textColor="#E6E6E6"
        android:layout_marginBottom="5dp"
        android:layout_marginLeft="10dp"
        android:layout_marginTop="3dp" />

    <EditText
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:inputType="textPersonName"
        android:ems="10"
        android:id="@+id/txtApellido"
        android:hint="@string/hint_apellido"
        android:layout_below="@+id/lblApellido"
        android:layout_alignParentRight="true" />

    <Button
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text="ENVIAR"
        android:textColor="#e7f5fe"
        android:id="@+id/btnEnviar"
        android:background="@drawable/button_form"

        android:minHeight="41dp"
        android:layout_below="@+id/txtCelularRecomendar2"
        android:layout_alignParentLeft="true"
        android:layout_marginTop="10dp" />



    <TextView
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:textAppearance="?android:attr/textAppearanceMedium"
        android:text="MAIL*"
        android:id="@+id/lblMail"
        android:textSize="15dp"
        android:textColor="#E6E6E6"
        android:layout_below="@+id/txtEmpresa"
        android:layout_alignRight="@+id/txtEmpresa"
        android:layout_marginBottom="5dp"
        android:layout_marginLeft="10dp"
        android:layout_marginTop="3dp" />/>


    <EditText
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:inputType="textEmailAddress"
        android:ems="10"
        android:id="@+id/txtMail"
        android:hint="@string/hint_mail"
        android:layout_below="@+id/lblMail"
        android:layout_alignRight="@+id/lblMensajeRecomendar" />

    <TextView
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:textAppearance="?android:attr/textAppearanceMedium"
        android:text="CELULAR*"
        android:id="@+id/lblCelular"
        android:layout_below="@+id/txtMail"
        android:layout_alignParentLeft="true"
        android:textSize="15dp"
        android:textColor="#E6E6E6"
        android:layout_marginBottom="5dp"
        android:layout_marginLeft="10dp"
        android:layout_marginTop="3dp"
        />

    <EditText
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:inputType="textEmailAddress"
        android:ems="10"
        android:id="@+id/txtCelular"
        android:hint="@string/hint_celular_recomendar"
        android:layout_below="@+id/lblCelular"
        android:layout_alignParentLeft="true" />

    <ImageView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/imageView"
        android:layout_alignParentTop="true"
        android:layout_centerHorizontal="true"
        android:background="@drawable/logo" />

    <TextView
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:textAppearance="?android:attr/textAppearanceMedium"
        android:text="PROFESIÓN*"
        android:id="@+id/lblProfesion"
        android:textSize="15dp"
        android:textColor="#E6E6E6"
        android:layout_below="@+id/txtApellido"
        android:layout_alignParentLeft="true"
        android:layout_marginBottom="5dp"
        android:layout_marginLeft="10dp"
        android:layout_marginTop="3dp" />/>

    <EditText
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:inputType="textPersonName"
        android:ems="10"
        android:id="@+id/txtProfesion"
        android:hint="@string/hint_profesion"
        android:layout_below="@+id/lblProfesion"
        android:layout_alignParentLeft="true" />

    <TextView
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:textAppearance="?android:attr/textAppearanceMedium"
        android:text="EMPRESA"
        android:id="@+id/lblEmpresa"
        android:textSize="15dp"
        android:textColor="#E6E6E6"
        android:layout_below="@+id/txtProfesion"
        android:layout_alignParentLeft="true"
        android:layout_marginBottom="5dp"
        android:layout_marginLeft="10dp"
        android:layout_marginTop="3dp" />/>

    <EditText
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:inputType="textPersonName"
        android:ems="10"
        android:id="@+id/txtEmpresa"
        android:hint="@string/hint_empresa"
        android:layout_below="@+id/lblEmpresa"
        android:layout_alignParentLeft="true" />

    <TextView
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:textAppearance="?android:attr/textAppearanceMedium"
        android:text="NÚMERO DE CLIENTE"
        android:id="@+id/lblNumeroCliente"
        android:textSize="15dp"
        android:textColor="#E6E6E6"
        android:layout_below="@+id/txtCelular"
        android:layout_alignParentLeft="true"
        android:layout_marginBottom="5dp"
        android:layout_marginLeft="10dp"
        android:layout_marginTop="3dp" />/>

    <EditText
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:inputType="textEmailAddress"
        android:ems="10"
        android:id="@+id/txtNumeroCliente"
        android:hint="@string/hint_numero_cliente"
        android:layout_below="@+id/lblNumeroCliente"
        android:layout_alignParentLeft="true" />

    <TextView
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:textAppearance="?android:attr/textAppearanceMedium"
        android:text="RECOMIENDE AMIGOS PARA REGALARLE LA APLICACIÓN &quot;CADE-CODIGOS&quot;  Y PARTICIPE DEL SORTEO DE UN &quot;CURSO-CADE&quot; A SU ELECCIÓN(TENDRÁ UNA CHANCE POR CADA AMIGO RECOMENDADO)"
        android:id="@+id/lblMensajeRecomendar"
        android:textSize="15dp"
        android:textColor="#e7f5fe"
        android:layout_below="@+id/txtNumeroCliente"
        android:layout_alignParentLeft="true"
        android:layout_marginBottom="5dp"
        android:layout_marginTop="3dp"
        android:layout_marginLeft="1dp" />

    <EditText
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:inputType="textPersonName"
        android:ems="10"
        android:id="@+id/txtMailRecomendar1"
        android:hint="@string/hint_email_recomendar"
        android:layout_below="@+id/lblMailRecomendar1"
        android:layout_alignParentLeft="true" />

    <TextView
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:textAppearance="?android:attr/textAppearanceMedium"
        android:text="MAIL "
        android:id="@+id/lblMailRecomendar1"
        android:textSize="15dp"
        android:textColor="#E6E6E6"
        android:layout_below="@+id/txtApellidoRecomendar1"
        android:layout_alignParentLeft="true"
        android:layout_marginBottom="5dp"
        android:layout_marginLeft="10dp"
        android:layout_marginTop="3dp"/>

    <TextView
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:textAppearance="?android:attr/textAppearanceMedium"
        android:text="CELULAR"
        android:id="@+id/lblCelularRecomendar1"
        android:textSize="15dp"
        android:textColor="#E6E6E6"
        android:layout_below="@+id/txtMailRecomendar1"
        android:layout_alignParentLeft="true"
        android:layout_marginBottom="5dp"
        android:layout_marginLeft="10dp"
        android:layout_marginTop="3dp" />

    <EditText
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:inputType="textPersonName"
        android:ems="10"
        android:id="@+id/txtCelularRecomendar1"
        android:hint="@string/hint_celular_recomendar"
        android:layout_below="@+id/lblCelularRecomendar1"
        android:layout_alignParentLeft="true" />

    <TextView
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:textAppearance="?android:attr/textAppearanceMedium"
        android:text="NOMBRE"
        android:id="@+id/lblNombreRecomendar1"
        android:textSize="15dp"
        android:textColor="#E6E6E6"
        android:layout_below="@+id/lblAmigo1"
        android:layout_alignParentRight="true"
        android:layout_marginTop="5dp"
        android:layout_marginBottom="5dp"
        android:layout_marginLeft="10dp" />

    <EditText
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:inputType="textPersonName"
        android:ems="10"
        android:id="@+id/txtNombreRecomendar1"
        android:hint="@string/hint_nombre"
        android:layout_below="@+id/lblNombreRecomendar1"
        android:layout_alignParentLeft="true" />

    <TextView
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:textAppearance="?android:attr/textAppearanceMedium"
        android:text="APELLIDO"
        android:id="@+id/lblApellidoRecomendar1"
        android:textSize="15dp"
        android:textColor="#E6E6E6"
        android:layout_below="@+id/txtNombreRecomendar1"
        android:layout_alignParentLeft="true"
        android:layout_marginBottom="5dp"
        android:layout_marginLeft="10dp"
        android:layout_marginTop="3dp"/>

    <EditText
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:inputType="textPersonName"
        android:ems="10"
        android:id="@+id/txtApellidoRecomendar1"
        android:hint="@string/hint_apellido"
        android:layout_below="@+id/lblApellidoRecomendar1"
        android:layout_alignParentLeft="true" />

    <TextView
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:textAppearance="?android:attr/textAppearanceMedium"
        android:text="AMIGO 1"
        android:id="@+id/lblAmigo1"
        android:textSize="15dp"
        android:textColor="#E6E6E6"
        android:layout_below="@+id/lblMensajeRecomendar"
        android:layout_alignParentLeft="true"
        android:layout_marginTop="5dp" />

    <TextView
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:textAppearance="?android:attr/textAppearanceMedium"
        android:text="AMIGO 2"
        android:id="@+id/lblAmigo2"
        android:textSize="15dp"
        android:textColor="#E6E6E6"
        android:layout_below="@+id/txtCelularRecomendar1"
        android:layout_alignRight="@+id/lblMensajeRecomendar"
        android:layout_marginTop="5dp" />

    <TextView
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:textAppearance="?android:attr/textAppearanceMedium"
        android:text="NOMBRE"
        android:id="@+id/lblNombreRecomendar2"
        android:textSize="15dp"
        android:textColor="#E6E6E6"
        android:layout_below="@+id/lblAmigo2"
        android:layout_alignParentLeft="true"
        android:layout_marginTop="5dp"
        android:layout_marginBottom="5dp"
        android:layout_marginLeft="10dp"/>

    <EditText
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:inputType="textPersonName"
        android:ems="10"
        android:id="@+id/txtNombreRecomendar2"
        android:hint="@string/hint_nombre"
        android:layout_below="@+id/lblNombreRecomendar2"
        android:layout_alignParentLeft="true" />

    <TextView
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:textAppearance="?android:attr/textAppearanceMedium"
        android:text="APELLIDO"
        android:id="@+id/lblApellidoRecomendar2"
        android:textSize="15dp"
        android:textColor="#E6E6E6"
        android:layout_below="@+id/txtNombreRecomendar2"
        android:layout_alignParentLeft="true"
        android:layout_marginTop="5dp"
        android:layout_marginBottom="5dp"
        android:layout_marginLeft="10dp"/>

    <EditText
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:inputType="textPersonName"
        android:ems="10"
        android:id="@+id/txtApellidoRecomendar2"
        android:hint="@string/hint_apellido"
        android:layout_below="@+id/lblApellidoRecomendar2"
        android:layout_alignParentLeft="true" />

    <TextView
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:textAppearance="?android:attr/textAppearanceMedium"
        android:text="MAIL "
        android:id="@+id/lblMailRecomendar2"
        android:textSize="15dp"
        android:textColor="#E6E6E6"
        android:layout_below="@+id/txtApellidoRecomendar2"
        android:layout_alignParentLeft="true"
        android:layout_marginTop="5dp"
        android:layout_marginBottom="5dp"
        android:layout_marginLeft="10dp"/>

    <EditText
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:inputType="textPersonName"
        android:ems="10"
        android:id="@+id/txtMailRecomendar2"
        android:hint="@string/hint_email_recomendar"
        android:layout_below="@+id/lblMailRecomendar2"
        android:layout_alignParentLeft="true" />

    <TextView
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:textAppearance="?android:attr/textAppearanceMedium"
        android:text="CELULAR"
        android:id="@+id/lblCelularRecomendar2"
        android:textSize="15dp"
        android:textColor="#E6E6E6"
        android:layout_below="@+id/txtMailRecomendar2"
        android:layout_alignParentLeft="true"
        android:layout_marginTop="5dp"
        android:layout_marginBottom="5dp"
        android:layout_marginLeft="10dp"/>

    <EditText
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:inputType="textPersonName"
        android:ems="10"
        android:id="@+id/txtCelularRecomendar2"
        android:hint="@string/hint_celular_recomendar"
        android:layout_below="@+id/lblCelularRecomendar2"
        android:layout_alignParentLeft="true"
        android:layout_marginBottom="10dp" />


</RelativeLayout>

</LinearLayout>
    </ScrollView>

最佳答案

是什么原因?

主要原因可能是您的应用程序中存在一处或多处内存泄漏,或者您只是试图在此布局中分配过多的内存。它可能位于此布局中,其中包含您尝试显示的图像的数量/大小,或者它可能位于先前的布局中,该布局仍保留对正在占用内存的位图的引用。这会在任何设备上发生,但三星 Galaxy III 的内存比新手机少,这可能是它在这款手机上经常发生的原因。

如何找到解决方案?

只有一种方法可以检测您的问题。

  1. 打开导致应用崩溃的activity,此时进行内存转储。

  2. 打开另一个 Activity ,并在此处创建内存转储。

  3. 分析内存转储并尝试查找内存泄漏。

  4. 只有你知道内存泄漏可能在哪里(它可以是一个大图像,或者你使用了很多静态对象,可能还没有关闭流等等),内存转储可以帮助你这次旅行。

记住,只有你能解决这个问题,当你解决了它你就不会再犯同样的错误!!

祝你好运!

另见

  1. Investigating Your RAM Usage .
  2. Memory Analysis for Android Applications .

附言

我刚刚分析了你的错误堆栈,发现 OutOfMemory 导致你的 drawable,它已经从资源中加载,如果它是一个图像,你可以为它降低分辨率,也许问题会消失。

Caused by: java.lang.OutOfMemoryError
    at android.graphics.BitmapFactory.nativeDecodeAsset(Native Method)
    at android.graphics.BitmapFactory.decodeStream(BitmapFactory.java:596)
    at android.graphics.BitmapFactory.decodeResourceStream(BitmapFactory.java:444)
    at android.graphics.drawable.Drawable.createFromResourceStream(Drawable.java:832)
    at android.content.res.Resources.loadDrawable(Resources.java:2988)
    at android.content.res.TypedArray.getDrawable(TypedArray.java:602) 

我希望,this topic会帮助你!

附言

看起来您的logo 图像占用大量内存(它太大-高分辨率),它可能会导致您的问题,进行内存转储并分析它有多少字节被拍了

关于android - OutOfMemory异常android,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32828339/

相关文章:

更改类后的 java.lang.NoSuchFieldError

Android应用程序运行内存大小限制

android - BluetoothGattCallBack 函数 onCharacteristicRead 仅调用一次

c# - 如何在 C# (.NET 2.0) 中获取给定 TableAdapter 和 SqlDataAdapter 的 Sql 命令对象

android - FFmpeg可执行文件Chmod权限

python - 异常处理

android - 比较 b/w bitmap.recycle() 和 bitmap=null

java - 将 3GB 文件转换为字节数组

android - 如何使用 FirebaseRecyclerViewAdapter 在扁平结构 Firebase 中导航

java - 获取 LayoutInflater 的最快方法