启动时Android应用程序崩溃,图像资源相关

时间:2017-11-16 18:59:37

标签: android

我有一个Android应用程序崩溃三星S4,S5和Note3设备。 该应用程序前面有一个LinearLayout,并呈现7行“类别”选项。

常见错误是:

java.lang.OutOfMemoryError: 
           at dalvik.system.VMRuntime.newNonMovableArray (Native Method)
           at android.graphics.BitmapFactory.nativeDecodeAsset (Native Method)
           at android.graphics.BitmapFactory.decodeStream (BitmapFactory.java:856)

      at android.graphics.BitmapFactory.decodeResourceStream (BitmapFactory.java:675)

      at android.graphics.drawable.Drawable.createFromResourceStream (Drawable.java:2228)

      at android.content.res.Resources.loadDrawableForCookie (Resources.java:4211)

      at android.content.res.Resources.loadDrawable (Resources.java:4085)

      at android.content.res.Resources.getDrawable (Resources.java:2005)

      at android.content.res.Resources.getDrawable (Resources.java:1987)

      at android.content.Context.getDrawable (Context.java:464)

      at android.support.v4.c.b.a (Unknown Source)

      at android.support.v4.c.a.a (Unknown Source)

      at android.support.v7.widget.m.a (Unknown Source)

      at android.support.v7.widget.m.a (Unknown Source)

      at android.support.v7.b.a.b.b (Unknown Source)

      at android.support.v7.view.menu.j.getIcon (Unknown Source)

      at android.support.design.internal.NavigationMenuItemView.a (Unknown Source)

      at android.support.design.internal.i$b.a (Unknown Source)

      at android.support.design.internal.i$b.a (Unknown Source)
      at android.support.v7.widget.RecyclerView$a.a (Unknown Source)
           at android.support.v7.widget.RecyclerView$a.b (Unknown Source)
           at android.support.v7.widget.RecyclerView$o.a (Unknown Source)
           at android.support.v7.widget.RecyclerView$o.a (Unknown Source)
           at android.support.v7.widget.RecyclerView$o.a (Unknown Source)
           at android.support.v7.widget.RecyclerView$o.c (Unknown Source)
           at android.support.v7.widget.LinearLayoutManager$c.a (Unknown Source)
           at android.support.v7.widget.LinearLayoutManager.a (Unknown Source)     
      at android.support.v7.widget.LinearLayoutManager.a (Unknown Source)

      at android.support.v7.widget.LinearLayoutManager.c (Unknown Source)

      at android.support.v7.widget.RecyclerView.N (Unknown Source)

      at android.support.v7.widget.RecyclerView.p (Unknown Source)

      at android.support.v7.widget.RecyclerView.onLayout (Unknown Source)

      at android.view.View.layout (View.java:17938)

      at android.view.ViewGroup.layout (ViewGroup.java:5814)

      at android.widget.FrameLayout.layoutChildren (FrameLayout.java:344)

      at android.widget.FrameLayout.onLayout (FrameLayout.java:281)

      at android.view.View.layout (View.java:17938)

      at android.view.ViewGroup.layout (ViewGroup.java:5814)

      at android.support.v4.widget.DrawerLayout.onLayout (Unknown Source)

      at android.view.View.layout (View.java:17938)

      at android.view.ViewGroup.layout (ViewGroup.java:5814)

      at android.widget.FrameLayout.layoutChildren (FrameLayout.java:344)

      at android.widget.FrameLayout.onLayout (FrameLayout.java:281)

      at android.view.View.layout (View.java:17938)

      at android.view.ViewGroup.layout (ViewGroup.java:5814)

      at android.widget.LinearLayout.setChildFrame (LinearLayout.java:1742)

      at android.widget.LinearLayout.layoutVertical (LinearLayout.java:1585)

      at android.widget.LinearLayout.onLayout (LinearLayout.java:1494)

      at android.view.View.layout (View.java:17938)

      at android.view.ViewGroup.layout (ViewGroup.java:5814)

      at android.widget.FrameLayout.layoutChildren (FrameLayout.java:344)

      at android.widget.FrameLayout.onLayout (FrameLayout.java:281)

      at android.view.View.layout (View.java:17938)
     ...

我在MainActivity中使用ListView,其中每一行都是单个图像资源。像这样:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="fill_parent"
    android:layout_height="100dp"
    android:layout_margin="0dp"
    android:padding="0dp">

    <LinearLayout
        android:id="@+id/categoryViewContainer"
        android:layout_width="match_parent"
        android:layout_height="100dp"
        android:layout_centerHorizontal="true"
        android:layout_centerVertical="true"
        android:background="@drawable/category_logo_transport"
        android:orientation="vertical"
        tools:ignore="UselessParent">

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="100dp"
            android:background="@drawable/gradient_bg"
            android:gravity="bottom"
            android:orientation="vertical"
            android:paddingBottom="10dp"
            android:paddingLeft="10dp"
            tools:ignore="RtlHardcoded,RtlSymmetry">

            <TextView
                android:id="@+id/categoryTitle"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:shadowColor="@color/Black"
                android:shadowDx="-5"
                android:shadowDy="-1"
                android:shadowRadius="10"
                android:text="@string/categorytitle"
                android:textColor="@color/White"
                android:textSize="28sp" />

            <TextView
                android:id="@+id/categoryDesc"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:maxLines="2"
                android:text="@string/categorydesc"
                android:textColor="@color/windowBackground"
                android:textSize="11sp"
                tools:ignore="SmallSp" />

        </LinearLayout>

    </LinearLayout>

</RelativeLayout>

我认为这么多图像使用的内存量可能存在问题,但我已经将每个图像压缩到35kB。

1 个答案:

答案 0 :(得分:1)

尝试在ImageView中设置图片,而不是LinearLayout

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="fill_parent"
    android:layout_height="100dp"
    android:layout_margin="0dp"
    android:padding="0dp">

    <ImageView
        android:layout_width="match_parent"
        android:layout_height="100dp"
        android:src="@drawable/category_logo_transport"/>

    <LinearLayout
        android:id="@+id/categoryViewContainer"
        android:layout_width="match_parent"
        android:layout_height="100dp"
        android:layout_centerHorizontal="true"
        android:layout_centerVertical="true"
        android:orientation="vertical"
        tools:ignore="UselessParent">

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="100dp"
            android:background="@drawable/gradient_bg"
            android:gravity="bottom"
            android:orientation="vertical"
            android:paddingBottom="10dp"
            android:paddingLeft="10dp"
            tools:ignore="RtlHardcoded,RtlSymmetry">

            <TextView
                android:id="@+id/categoryTitle"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:shadowColor="@color/Black"
                android:shadowDx="-5"
                android:shadowDy="-1"
                android:shadowRadius="10"
                android:text="@string/categorytitle"
                android:textColor="@color/White"
                android:textSize="28sp" />

            <TextView
                android:id="@+id/categoryDesc"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:maxLines="2"
                android:text="@string/categorydesc"
                android:textColor="@color/windowBackground"
                android:textSize="11sp"
                tools:ignore="SmallSp" />

        </LinearLayout>

    </LinearLayout>

</RelativeLayout>