setContentView中的OutOfMemoryError

时间:2014-11-10 19:22:20

标签: android android-layout

我的应用程序中存在一个很难捕获的错误,因为它很少发生(可能每500次我在真实设备上运行应用程序一次)。我在与活动关联的布局中有几个自定义视图。在活动的setContentView()方法中,我有时遇到OOME,我不明白为什么?

它似乎试图让那个视图膨胀,然后从那里下山。因为问题很少发生,有没有办法可以处理这个错误或者我该如何避免呢?我不明白在这种情况下内存是如何用完的。这是堆栈跟踪

 Process: com.android.wallfly, PID: 31817
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.android.wallfly/com.android.wallfly.Tabs.CreateEvent}: android.view.InflateException: Binary XML file line #738: Error inflating class <unknown>
        at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2305)
        at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2363)
        at android.app.ActivityThread.access$900(ActivityThread.java:161)
        at java.lang.reflect.Method.invoke(Method.java:515)
        at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1265)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1081)
        at dalvik.system.NativeStart.main(Native Method)
 Caused by: android.view.InflateException: Binary XML file line #738: Error inflating class <unknown>
        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 com.android.internal.policy.impl.PhoneWindow.setContentView(PhoneWindow.java:359)
        at android.app.Activity.setContentView(Activity.java:2015)
        at com.android.wallfly.Tabs.CreateEvent.onCreate(CreateEvent.java:337)
        at android.app.Activity.performCreate(Activity.java:5431)
        at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1105)
        at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2269)
    Caused by: java.lang.reflect.InvocationTargetException
        at java.lang.reflect.Constructor.constructNative(Native Method)
        at java.lang.reflect.Constructor.newInstance(Constructor.java:423)
        at android.view.LayoutInflater.createView(LayoutInflater.java:600)
Caused by: java.lang.OutOfMemoryError
        at android.graphics.BitmapFactory.nativeDecodeAsset(Native Method)
        at android.graphics.BitmapFactory.decodeStream(BitmapFactory.java:677)
        at android.graphics.BitmapFactory.decodeResourceStream(BitmapFactory.java:507)
        at android.graphics.drawable.Drawable.createFromResourceStream(Drawable.java:872)
        at android.content.res.Resources.loadDrawable(Resources.java:3024)
        at android.content.res.Resources.getDrawable(Resources.java:1586)
        at android.graphics.drawable.StateListDrawable.inflate(StateListDrawable.java:176)
        at android.graphics.drawable.Drawable.createFromXmlInner(Drawable.java:973)
        at android.graphics.drawable.Drawable.createFromXml(Drawable.java:913)
        at android.content.res.Resources.loadDrawable(Resources.java:3006)
        at android.content.res.TypedArray.getDrawable(TypedArray.java:602)
        at android.view.View.<init>(View.java:3694)

这是布局

<ViewFlipper
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/layoutsVF">

<RelativeLayout
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:layout_gravity="center"
    android:background="#ff3a393c"
    android:id="@+id/first"
    android:visibility="visible">
<ImageView
    android:layout_width="fill_parent"
    android:layout_height="350dp"
    android:layout_gravity="center"
    android:id="@+id/eventPhoto"
    android:layout_margin="5dp"
    android:layout_centerInParent="true"
    android:src="@drawable/galleryicon"
    android:background="@drawable/rect_border" />

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="50dp"
        android:textAppearance="?android:attr/textAppearanceLarge"
        android:text="Chose Photo"
        android:id="@+id/textView"
        android:layout_alignParentTop="true"
        android:layout_alignParentStart="true"
        android:textSize="25dp"
        android:textColor="@color/white"
        android:background="#4C1995"
        android:gravity="center|left"
        android:paddingLeft="20dp"
        android:layout_alignParentEnd="true" />

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="0.5dp"
        android:text="New Text"
        android:id="@+id/postedTimeTV"
        android:layout_alignParentStart="true"
        android:layout_below="@+id/textView"
        android:layout_alignParentEnd="true"
        android:background="#ff311760" />

    <ImageView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/confirmPhotoButton"
        android:layout_alignParentTop="true"
        android:layout_alignParentEnd="true"
        android:src="@drawable/check_button_ab"
        android:background="@drawable/ab_confirm_bckd"
        android:layout_alignBottom="@+id/textView"
        android:visibility="visible" />

    <ImageView
        android:layout_width="35dp"
        android:layout_height="35dp"
        android:id="@+id/whiteBackButton"
        android:layout_alignParentBottom="true"
        android:layout_alignParentStart="true"
        android:layout_marginBottom="35dp"
        android:layout_marginLeft="15dp"
        android:background="#fffbf8ff" />

    <ImageView
        android:layout_width="35dp"
        android:layout_height="35dp"
        android:id="@+id/blackBackButton"
        android:background="#ff000000"
        android:layout_alignTop="@+id/whiteBackButton"
        android:layout_toRightOf="@+id/whiteBackButton"
        android:layout_marginLeft="5dp" />

    <ImageView
        android:layout_width="35dp"
        android:layout_height="35dp"
        android:id="@+id/clearBackButton"
        android:background="@drawable/rect_border"
        android:layout_alignTop="@+id/blackBackButton"
        android:layout_toRightOf="@+id/blackBackButton"
        android:layout_marginLeft="5dp" />

</RelativeLayout>

<ScrollView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:id="@+id/second"
    tools:context="com.android.wallfly.Tabs.CreateEvent"
    android:visibility="visible">
<RelativeLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:id="@+id/firstLayout" >




<RelativeLayout
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:layout_alignParentTop="true"
    android:id="@+id/relativeLayout">

    <com.android.wallfly.UI_utils.CircularProgressButton
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Submit"
        android:id="@+id/submit"
        android:layout_below="@+id/mapOne"
        app:cpb_paddingProgress="5dp"
        app:cpb_textComplete="@string/Complete"
        app:cpb_textError="@string/Error"
        app:cpb_textIdle="Post"
        app:cpb_cornerRadius="5dp"
        android:textColor="@android:color/white"
        android:layout_marginTop="5dp"
        android:layout_toRightOf="@+id/blueBadgedTV"
        android:layout_alignRight="@+id/toButtonTime"
        android:layout_alignEnd="@+id/toButtonTime"
        android:layout_marginBottom="5dp" />

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Who"
        android:id="@+id/textView9"
        android:layout_alignParentTop="true"
        android:layout_alignParentLeft="true"
        android:layout_alignParentStart="true"
        android:textSize="15dp"
        android:layout_marginTop="25dp"
        android:layout_marginLeft="13dp" />

    <ImageView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/divider4"
        android:background="@drawable/divider"
        android:layout_alignLeft="@+id/eventNameTV"
        android:layout_alignStart="@+id/eventNameTV"
        android:layout_below="@+id/textView9"
        android:layout_alignRight="@+id/eventNameTV"
        android:layout_alignEnd="@+id/eventNameTV"
        android:layout_marginRight="10dp"
        android:layout_marginLeft="10dp"/>

    <EditText
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/eventNameTV"
        android:layout_below="@+id/linedivider"
        android:layout_alignParentLeft="true"
        android:layout_alignParentStart="true"
        android:layout_alignParentRight="true"
        android:layout_alignParentEnd="true"
        android:hint="Name your event..."
        android:layout_marginLeft="7dp"
        android:layout_marginRight="7dp"
        android:textSize="10dp"
        android:layout_marginTop="7dp"
        android:gravity="center|center_vertical|center_horizontal"
        android:singleLine="true"
        android:imeOptions="actionDone"/>

    <EditText
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/eventDescription"
        android:layout_below="@+id/eventNameTV"
        android:layout_alignParentLeft="true"
        android:layout_alignParentStart="true"
        android:layout_alignParentRight="true"
        android:layout_alignParentEnd="true"
        android:hint="Description (Optional)"
        android:layout_marginLeft="7dp"
        android:layout_marginRight="7dp"
        android:textSize="10dp"
        android:layout_marginTop="7dp"
        android:gravity="center|center_vertical|center_horizontal" />

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="30dp"
        android:textAppearance="?android:attr/textAppearanceLarge"
        android:text="F :"
        android:id="@+id/blackBadgedTV"
        android:layout_below="@+id/divider2"
        android:layout_alignParentLeft="true"
        android:layout_alignParentStart="true"
        android:gravity="center_vertical"
        android:layout_marginLeft="13dp"
        android:layout_marginTop="10dp"
        android:textSize="10dp" />

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="30dp"
        android:textAppearance="?android:attr/textAppearanceLarge"
        android:text="T :"
        android:id="@+id/blueBadgedTV"
        android:layout_below="@+id/blackBadgedTV"
        android:layout_alignParentLeft="true"
        android:layout_alignParentStart="true"
        android:layout_marginTop="10dp"
        android:gravity="center_vertical"
        android:layout_marginLeft="13dp"
        android:textSize="10dp" />

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/fromButtonDate"
        android:layout_toRightOf="@+id/blackBadgedTV"
        android:layout_above="@+id/blueBadgedTV"
        android:textSize="10dp"
        android:background="@drawable/setbutton"
        android:layout_alignTop="@+id/blackBadgedTV"
        android:layout_toLeftOf="@+id/space"
        android:layout_marginLeft="10dp"
        android:text="333333"
        android:gravity="center_vertical|center_horizontal"
        android:singleLine="true"
        android:textAlignment="center"
        android:textAppearance="@android:style/TextAppearance.Holo.Small"
        android:paddingTop="10dp" />

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/toButtonDate"
        android:layout_alignBottom="@+id/blueBadgedTV"
        android:layout_toRightOf="@+id/blackBadgedTV"
        android:layout_alignTop="@+id/blueBadgedTV"
        android:textSize="10dp"
        android:layout_alignRight="@+id/fromButtonDate"
        android:layout_alignEnd="@+id/fromButtonDate"
        android:background="@drawable/greenbutton"
        android:layout_marginLeft="10dp"
        android:text="333"
        android:gravity="center"
        android:paddingTop="10dp" />

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/fromButtonTime"
        android:layout_alignParentRight="true"
        android:layout_alignParentEnd="true"
        android:layout_alignBottom="@+id/fromButtonDate"
        android:layout_toRightOf="@+id/space"
        android:layout_marginRight="25dp"
        android:layout_marginLeft="3dp"
        android:textSize="10dp"
        android:layout_alignTop="@+id/fromButtonDate"
        android:text="333"
        android:gravity="center_vertical|center_horizontal"
        android:paddingTop="10dp"
        android:background="@drawable/setbutton" />



    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="When"
        android:id="@+id/planToAttendTV"
        android:textSize="15dp"
        android:layout_below="@+id/eventDescription"
        android:layout_alignLeft="@+id/eventNameTV"
        android:layout_alignStart="@+id/eventNameTV"
        android:layout_marginLeft="5dp" />

    <ImageView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/divider2"
        android:layout_toLeftOf="@+id/space"
        android:background="@drawable/divider"
        android:layout_alignParentEnd="true"
        android:layout_alignParentStart="true"
        android:layout_below="@id/planToAttendTV"
        android:layout_marginLeft="15dp"
        android:layout_marginTop="4dp"
        android:layout_marginRight="15dp" />

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Where"
        android:id="@+id/addToCalendarButton"
        android:textSize="15dp"
        android:layout_below="@+id/toButtonTime"
        android:layout_alignLeft="@+id/eventNameTV"
        android:layout_alignStart="@+id/eventNameTV"
        android:layout_marginLeft="5dp"
        android:layout_marginTop="15dp" />

    <ImageView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/divider3"
        android:layout_toLeftOf="@+id/space"
        android:background="@drawable/divider"
        android:layout_alignParentEnd="true"
        android:layout_alignParentStart="true"
        android:layout_below="@id/addToCalendarButton"
        android:layout_marginLeft="15dp"
        android:layout_marginTop="4dp"
        android:layout_marginRight="15dp" />

    <ViewFlipper
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignRight="@+id/divider3"
        android:layout_alignLeft="@+id/linedivider"
        android:layout_alignStart="@+id/linedivider"
        android:layout_below="@+id/divider3"
        android:layout_marginTop="10dp"
        android:id="@+id/locationVF">



    <EditText
        android:layout_width="fill_parent"
        android:layout_height="30dp"
        android:textAppearance="?android:attr/textAppearanceMedium"
        android:id="@+id/address"
        android:textColor="@android:color/black"
        android:textSize="14dp"
        android:singleLine="true"
        android:background="@android:color/transparent"
        android:gravity="center|center_vertical|center_horizontal"
        android:hint="Address or address alias"/>

        <TextView
            android:layout_width="fill_parent"
            android:layout_height="30dp"
            android:textAppearance="?android:attr/textAppearanceMedium"
            android:id="@+id/confirmedLocationTV"
            android:textColor="@android:color/white"
            android:textSize="14dp"
            android:singleLine="true"
            android:background="#ff00c004"
            android:gravity="center|center_vertical|center_horizontal"
            android:text="Confirmed!" />

    </ViewFlipper>

    <ViewFlipper
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:id="@+id/eventProfileVF"
        android:layout_alignRight="@+id/fromButtonTime"
        android:layout_alignEnd="@+id/fromButtonTime"
        android:layout_toRightOf="@+id/blackBadgedTV"
        android:layout_below="@+id/divider4"
        android:layout_marginTop="10dp">

        <Button
            android:layout_width="match_parent"
            android:layout_height="30dp"
            android:id="@+id/profileButton"
            android:layout_below="@+id/divider4"
            android:gravity="center|center_vertical|center_horizontal"
            android:text="Public"
            android:textColor="@color/white"
            android:background="@drawable/publicbadgebutton" />

        <Button
            android:layout_width="match_parent"
            android:layout_height="30dp"
            android:id="@+id/blackBadgeButton"
            android:layout_below="@+id/divider4"
            android:background="@drawable/blackbutton"
            android:gravity="center|center_vertical|center_horizontal"
            android:text="Badge: Black"
            android:textColor="@color/white" />

        <Button
            android:layout_width="match_parent"
            android:layout_height="30dp"
            android:id="@+id/blueBadgeButton"
            android:layout_below="@+id/divider4"
            android:background="@drawable/badgebluebutton"
            android:gravity="center|center_vertical|center_horizontal"
            android:text="Badge: Blue"
            android:textColor="@color/white" />

        <Button
            android:layout_width="match_parent"
            android:layout_height="30dp"
            android:id="@+id/greenBadgeButton"
            android:layout_below="@+id/divider4"
            android:background="@drawable/greenbadgebutton"
            android:gravity="center|center_vertical|center_horizontal"
            android:text="Badge: Green"
            android:textColor="@color/white" />

        <Button
            android:layout_width="match_parent"
            android:layout_height="30dp"
            android:id="@+id/purpleBadgeButton"
            android:layout_below="@+id/divider4"
            android:background="@drawable/purplebadgebutton"
            android:gravity="center|center_vertical|center_horizontal"
            android:text="Badge: Purple"
            android:textColor="@color/white" />

        <Button
            android:layout_width="match_parent"
            android:layout_height="30dp"
            android:id="@+id/redBadgeButton"
            android:layout_below="@+id/divider4"
            android:background="@drawable/redbadgebutton"
            android:gravity="center|center_vertical|center_horizontal"
            android:text="Badge: Red"
            android:textColor="@color/white" />

        <Button
            android:layout_width="match_parent"
            android:layout_height="30dp"
            android:id="@+id/privateBadgeButton"
            android:layout_below="@+id/divider4"
            android:background="@drawable/publicbadgebutton"
            android:gravity="center|center_vertical|center_horizontal"
            android:text="Private"
            android:textColor="@color/white" />



    </ViewFlipper>

    <Space
        android:layout_width="20px"
        android:layout_height="20px"
        android:layout_above="@+id/submit"
        android:layout_centerHorizontal="true"
        android:layout_marginBottom="67dp"
        android:id="@+id/space3" />

</RelativeLayout>




</RelativeLayout>
</ScrollView>

<RelativeLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:id="@+id/thirdLayout"
    android:orientation="vertical"
    android:visibility="visible">

    <fragment xmlns:android="http://schemas.android.com/apk/res/android"
        android:id="@+id/mapTwo"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        class="com.google.android.gms.maps.MapFragment"
        android:layout_alignParentTop="true"
        android:layout_alignParentLeft="true"
        android:layout_alignParentStart="true" />

            <Space
                android:layout_width="50dp"
                android:layout_height="20px"
                android:layout_alignTop="@+id/clearText"
                android:layout_alignParentRight="true"
                android:layout_alignParentEnd="true"
                android:id="@+id/space100" />

        </RelativeLayout>


        <Space
            android:layout_width="20px"
            android:layout_height="20px" />

    </ViewFlipper>


    <ViewFlipper
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/searchHintVF"
        android:layout_alignParentLeft="true"
        android:layout_alignParentStart="true"
        android:layout_below="@+id/searchVF">

        <Space
            android:layout_width="20px"
            android:layout_height="20px" />

        <RelativeLayout
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:id="@+id/searcgHintLayout">

            <TextView
                android:id="@+id/searchhintTV"
                android:layout_width="fill_parent"
                android:layout_height="35dp"
                android:background="#c79cb9ff"
                android:paddingLeft="10dp"
                android:layout_marginLeft="10dp"
                android:layout_alignParentTop="true"
                android:layout_marginTop="8dp"
                android:textSize="13dp"
                android:layout_marginRight="10dp"
                android:gravity="center_vertical"
                android:textColor="#ff4f4c52"
                android:textColorHint="#ff4f4c52" />

            <Button
                style="?android:attr/buttonStyleSmall"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="X"
                android:id="@+id/cancelHint"
                android:background="@android:color/transparent"
                android:textStyle="bold"
                android:layout_alignTop="@+id/searchhintTV"
                android:paddingTop="2dp"
                android:textColor="#ff383838"
                android:layout_alignRight="@+id/searchhintTV"
                android:layout_alignEnd="@+id/searchhintTV"
                android:layout_alignBottom="@+id/searchhintTV" />

        </RelativeLayout>

    </ViewFlipper>

    <ImageButton
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerHorizontal="true" />



    <ViewFlipper
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/switchShapeViewFlipper"
        android:layout_alignTop="@+id/radiusLayoutViewFlipper"
        android:layout_alignParentLeft="true"
        android:layout_alignParentStart="true"
        android:layout_marginTop="25dp"
        android:layout_marginLeft="5dp">

        <Button
            style="?android:attr/buttonStyleSmall"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:id="@+id/switch_shape_button"
            android:layout_alignTop="@+id/radiusLayout"
            android:layout_alignParentLeft="true"
            android:layout_alignParentStart="true"
            android:scaleX="0.4"
            android:scaleY="0.4" />



        <Space
            android:layout_width="20px"
            android:layout_height="20px" />

    </ViewFlipper>

    <Button
        style="?android:attr/buttonStyleSmall"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/orientation_button"
        android:layout_below="@+id/switchShapeViewFlipper"
        android:layout_alignParentLeft="true"
        android:layout_alignParentStart="true"
        android:visibility="invisible"
        android:enabled="true"
        android:layout_marginLeft="5dp"
        android:scaleX="0.3"
        android:scaleY="0.3"
        android:background="@drawable/rotate_button"
        android:layout_alignRight="@+id/switchShapeViewFlipper"
        android:layout_alignEnd="@+id/switchShapeViewFlipper"
        android:layout_above="@+id/seekBarViewFlipper"
        android:alpha="70" />

      ....

它说错误发生在布局的第738行,这只是一个常规按钮小部件。

更新

尝试充气自定义视图时不会出错。尝试充气常规视图时出错。

2 个答案:

答案 0 :(得分:3)

当您尝试处理OutOfMemoryError时,似乎会导致Bitmap,从以下几行可以看出:

Caused by: java.lang.OutOfMemoryError
        at android.graphics.BitmapFactory.nativeDecodeAsset(Native Method)
        at android.graphics.BitmapFactory.decodeStream(BitmapFactory.java:677)
        at android.graphics.BitmapFactory.decodeResourceStream(BitmapFactory.java:507)

我强烈建议您阅读有关displaying bitmaps的文章,特别是关于loading large bitmaps efficiently,并检查您的代码是否会这样做。

此外,您可以尝试使用Square的Picasso库,这在使用Bitmaps时会有很大帮助。

更新: 在你的情况下,问题可能是你在布局中使用一些大图像,只是尝试使用较小的图像,看看它是否仍然存在。查看与您类似的this answer

答案 1 :(得分:0)

尝试在清单的应用标记中添加 android:largeHeap =&#34; true&#34;