Android - Seemingly harmless layout causes app to crash after short use without any errors

三世轮回 提交于 2019-12-22 10:38:15

问题


After 2 days of fruitless testing, I've decided to post my issue here, in the hopes that I'm missing something obvious.

I've boiled down a seemingly innocuous xml-layout to a random collection of images, layouts, and a scrollview. Here it is:

<?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="match_parent"
    android:layout_height="match_parent"
    android:background="@drawable/wood_texture"
    tools:context="com.testlayout.example.testlayout.MainActivity">

    <ImageView
        android:layout_width="150dp"
        android:layout_height="150dp"
        android:src="@drawable/image_one"
        android:id="@+id/imageOne"
        android:adjustViewBounds="true"
        android:layout_gravity="center_vertical"
        android:visibility="visible"/>

    <ImageView
        android:layout_width="300dp"
        android:layout_height="150dp"
        android:background="@drawable/dark_wood_texture"
        android:id="@+id/darkWood"
        android:layout_alignParentBottom="true"
        android:layout_centerHorizontal="true"
        android:layout_marginLeft="20dp"
        android:layout_marginRight="20dp"
        android:layout_marginBottom="20dp"
        android:visibility="visible"/>

    <HorizontalScrollView
        android:layout_width="300dp"
        android:layout_height="150dp"
        android:id="@+id/handScrollView"
        android:scrollbars="none"
        android:layout_centerHorizontal="true"
        android:layout_alignParentBottom="true"
        android:paddingTop="10dp"
        android:paddingBottom="10dp"
        android:layout_marginLeft="20dp"
        android:layout_marginRight="20dp"
        android:layout_marginBottom="20dp"
        android:visibility="visible">

        <LinearLayout
            android:orientation="horizontal"
            android:layout_width="wrap_content"
            android:layout_height="fill_parent"
            android:id="@+id/handLayout">

            <ImageView
                android:layout_width="wrap_content"
                android:layout_height="match_parent"
                android:src="@drawable/image_three"
                android:id="@+id/imageView"
                android:adjustViewBounds="true"
                android:layout_marginLeft="20dp"
                android:layout_marginRight="20dp"/>

            <ImageView
                android:layout_width="wrap_content"
                android:layout_height="match_parent"
                android:src="@drawable/image_three"
                android:id="@+id/imageView2"
                android:adjustViewBounds="true"
                android:layout_marginLeft="20dp"
                android:layout_marginRight="20dp"/>

            <ImageView
                android:layout_width="wrap_content"
                android:layout_height="match_parent"
                android:src="@drawable/image_three"
                android:id="@+id/imageView6"
                android:adjustViewBounds="true"
                android:layout_marginLeft="20dp"
                android:layout_marginRight="20dp"/>

        </LinearLayout>

    </HorizontalScrollView>

    <ImageView
        android:id="@+id/imageTwo"
        android:layout_width="200dp"
        android:layout_height="200dp"
        android:layout_alignParentTop="true"
        android:layout_alignParentRight="true"
        android:src="@drawable/image_two_tiled"
        android:scaleType="fitXY"/>
</RelativeLayout>

I can load this layout onto my phone just fine, but after swiping around in the scrollview for as little as 5 seconds, the app will suddenly crash to the home screen - without reporting that the app has stopped working. I've been testing on a Samsung Galaxy s6. I've also tested this on a Samsung Galaxy Tab 2, where this problem does not occur (both my real app and this test app run just fine on the tablet).

I've created a new Android Studio project; the only changes I've made are to the res/layout/activity_main.xml, as outlined above. I've also included the drawables I'm using. I'm hoping someone with better debugging skills than I can either pull down that project, or simply create a new one of their own using the above layout (that's all there is to it, though you'll need my drawables regardless).

I'd like to clarify that I'm not looking for a way to fix this issue, I'm trying to understand the issue. The thing is, after 2 days of testing, I've found half a dozen ways to seemingly "fix" the issue. But none of them make any sense, and they all seem unrelated. If I don't know why my solution fixes the issue, I won't know how to avoid it during future development.

Observations

As I mentioned above, I've found several ways to "fix" the issue (i.e. make it no longer crash), but none of them seem to be related. Here are a few:

  • Set the visibility of almost any of the elements to gone.
  • Remove the background from the root element.
  • Remove the line android:tileMode="repeat" from drawable/image_two_tiled.xml
    • Note that changing the tileMode to something else - for instance, clamp - does not fix the issue. Only removing it (or setting it to disabled).
  • Set the src of the ImageView with the id "@+id/imageTwo" to image_two_smaller_tiled.
    • This is the exact same image as image_two_tiled, just a smaller resolution.

This is by no means an exhaustive list of, but it gives you an idea of how disjointed these fixes are (especially the tileMode one).

As near as I can tell, it looks like some kind of memory issue. I'm fairly new to android development, so I'm not quite familiar with the memory constraints I need to work with, but I would be horrifically concerned if I was causing the heap to explode with so few images, and of fairly small size (at the very least, certainly not big).

If anyone could tell me exactly what is causing my app to crash, I would be. . . Well, I would be extremely grateful. 'Cause after 2 days of trying to debug this myself, I'm about ready to quit android development : (

Edit

The app itself does not throw an exception, however logcat does consistently show this error at the time of the crash when I'm not filtering log messages from my app alone.

E/Resources: RunTimeException
android.content.res.Resources$NotFoundException: Resource ID #0x7f0202cf
    at android.content.res.Resources.getValue(Resources.java:2558)
    at android.content.res.Resources.startRC(Resources.java:1116)
    at android.app.ActivityThread$mRunnable.run(ActivityThread.java:3056)
    at java.lang.Thread.run(Thread.java:818)

e2: I'm starting to wonder if this is an issue with my phone, and not the app, since I can run it just fine on my tablet. I'd really like to know if anyone else is experiencing this issue if they try to run this on their Galaxy s6. Specifically the S6 - that would tell me if it's an issue with my phone or all S6's in general.


回答1:


This previous SO post answer solved my issue.

In short, it was the BitmapDrawable that was causing the entire issue. While unfortunately I cannot say why this fixes the issue, setting the view that uses the bitmap with layerType="software" will prevent the crashes.

  • In a layout: android:layerType="software"
  • In code: view.setLayerType(View.LAYER_TYPE_SOFTWARE, null);


来源:https://stackoverflow.com/questions/39585642/android-seemingly-harmless-layout-causes-app-to-crash-after-short-use-without

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!