当我尝试打开我的聊天类时,有时它运行正常,有时它会在 setContentView 行出现 OutOfMemoryError。我不明白问题的原因是什么?
这是我的 xml 代码:

<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="#ffffff"
    android:orientation="vertical">

    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:background="#00000000"
        android:orientation="vertical">

        <android.support.v7.widget.Toolbar
            android:id="@+id/toolbarmesajlasma"
            android:layout_width="match_parent"
            android:layout_height="50dp"
            android:background="@color/ColorPrimary"
            android:elevation="1dp">

            <RelativeLayout
                android:id="@+id/layoutmesajlasma"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:background="#00000000">

                <ImageButton
                    android:id="@+id/button6"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_marginLeft="0dp"
                    android:layout_centerVertical="true"
                    android:background="#00000000"
                    android:longClickable="false"
                    android:src="@mipmap/geribas" />

                <ImageButton
                    android:id="@+id/imageButton"
                    android:layout_width="43dp"
                    android:layout_height="43dp"
                    android:layout_marginLeft="35dp"
                    android:background="#00000000"
                    android:layout_centerVertical="true" />

                <TextView
                    android:id="@+id/textView2"
                    android:layout_width="135dp"
                    android:layout_height="wrap_content"
                    android:layout_gravity="center"
                    android:layout_marginLeft="85dp"
                    android:text="Abdurrahman"
                    android:textAppearance="?android:attr/textAppearanceLarge"
                    android:layout_centerVertical="true"
                    android:textColor="#ffffff"
                    android:textIsSelectable="false"
                    android:textSize="15dp"
                    android:textStyle="bold" />

            </RelativeLayout>
        </android.support.v7.widget.Toolbar>

        <RelativeLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_centerHorizontal="true"
            android:orientation="vertical"
            android:layout_below="@+id/toolbarmesajlasma"
            android:background="@mipmap/maybehi">

            <ListView
                android:id="@+id/listView2"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:divider="#00000000"
                android:dividerHeight="4dp"
                android:layout_alignParentTop="true"
                android:layout_above="@+id/linearLayout" />

            <LinearLayout
                android:orientation="horizontal"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_alignParentBottom="true"
                android:layout_centerHorizontal="true"
                android:background="@mipmap/bisiler_yaz_altplan"
                android:id="@+id/linearLayout">

                <EditText
                    android:id="@+id/editText2"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:background="@drawable/bisiler_yaz"
                    android:hint="Bir mesaj yaz..."
                    android:imeOptions="actionNone"
                    android:textSize="14dp"
                    android:textStyle="bold"
                    android:layout_marginRight="5dp"
                    android:layout_marginLeft="7dp"
                    android:layout_marginTop="5dp"
                    android:capitalize="sentences"
                    android:allowUndo="true"
                    android:width="295dp"
                    android:inputType="textCapSentences" />

                <ImageButton
                    android:id="@+id/button4"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_alignParentBottom="false"
                    android:layout_alignParentEnd="true"
                    android:layout_alignParentRight="true"
                    android:layout_centerVertical="true"
                    android:layout_gravity="bottom"
                    android:background="#00000000"
                    android:src="@mipmap/gonder"
                    android:text="Gonder"
                    android:layout_marginBottom="10dp" />

            </LinearLayout>

        </RelativeLayout>

    </RelativeLayout>

    <ImageView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/ivYatay"
        android:src="@drawable/horizontalrope"
        android:layout_marginTop="56dp"
        android:layout_marginLeft="118dp"/>
    <ImageView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/ivCapraz"
        android:src="@drawable/diagonalrope"
        android:layout_marginTop="50dp"
        android:layout_marginLeft="92dp"/>


    <ImageView
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:visibility="invisible"
        android:id="@+id/imageView6"
        android:background="@mipmap/gri"
        android:layout_marginTop="45dp" />
</FrameLayout>

这是错误:
java.lang.OutOfMemoryError
  at android.graphics.BitmapFactory.nativeDecodeAsset(Native Method)
  at android.graphics.BitmapFactory.decodeStream(BitmapFactory.java:683)
  at android.graphics.BitmapFactory.decodeResourceStream(BitmapFactory.java:513)
  at android.graphics.drawable.Drawable.createFromResourceStream(Drawable.java:889)
  at android.content.res.Resources.loadDrawable(Resources.java:3436)
  at android.content.res.TypedArray.getDrawable(TypedArray.java:602)
  at android.view.View.<init>(View.java:3708)
  at android.widget.ImageView.<init>(ImageView.java:127)
  at android.support.v7.widget.AppCompatImageView.<init>(AppCompatImageView.java:57)
  at android.support.v7.widget.AppCompatImageView.<init>(AppCompatImageView.java:53)
  at android.support.v7.app.AppCompatViewInflater.createView(AppCompatViewInflater.java:106)
  at android.support.v7.app.AppCompatDelegateImplV7.createView(AppCompatDelegateImplV7.java:963)
  at android.support.v7.app.AppCompatDelegateImplV7.onCreateView(AppCompatDelegateImplV7.java:1022)
  at android.support.v4.view.LayoutInflaterCompatHC$FactoryWrapperHC.onCreateView(LayoutInflaterCompatHC.java:44)
  at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:690)
  at android.view.LayoutInflater.rInflate(LayoutInflater.java:761)
  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 android.support.v7.app.AppCompatDelegateImplV7.setContentView(AppCompatDelegateImplV7.java:267)
  at android.support.v7.app.AppCompatActivity.setContentView(AppCompatActivity.java:129)
  at droxoft.armin.com.shappy.Mesajlasma.onCreate(Mesajlasma.java:223)
  at android.app.Activity.performCreate(Activity.java:5451)
  at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1093)
  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2377)
  at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2471)
  at android.app.ActivityThread.access$900(ActivityThread.java:175)
  at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1308)
  at android.os.Handler.dispatchMessage(Handler.java:102)
  at android.os.Looper.loop(Looper.java:146)
  at android.app.ActivityThread.main(ActivityThread.java:5602)
  at java.lang.reflect.Method.invokeNative(Native Method)
  at java.lang.reflect.Method.invoke(Method.java:515)
  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1283)
  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1099)
  at dalvik.system.NativeStart.main(Native Method)

最佳答案

正如我在日志跟踪中看到的那样,您的 OOM 错误与您正在加载的图像有关。

请参阅 this 以更有效地加载大型位图:

一个 400 KB 的图像文件很容易占用 5-10 MB 的 RAM。

为了请求动态增加堆大小,使用 android:largeHeap="true"在 manifest.xml 中。

或/和只是使用适当的功能来解码...

关于SetContentView 行中的 Android OutOfMemoryError,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/37072102/

10-12 01:20