Android-看似无害的布局会导致应用在短时间使用后崩溃而没有任何错误 [英] Android - Seemingly harmless layout causes app to crash after short use without any errors

查看:80
本文介绍了Android-看似无害的布局会导致应用在短时间使用后崩溃而没有任何错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

经过2天没有结果的测试后,我决定将我的问题发布在这里,以希望我遗漏了一些明显的内容.

我已经将看似无害的xml布局简化为图像,布局和滚动视图的随机集合.在这里:

<?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>

我可以很好地将这种布局加载到手机上,但是在滚动视图中滑动约5秒钟后,该应用程序突然崩溃到主屏幕-没有报告该应用程序已停止工作.我已经在三星Galaxy s6上进行过测试. 我还在三星Galaxy Tab 2上进行了测试,在该问题上不会发生(我的真实应用程序和该测试应用程序在平板电脑上都运行得很好).

我创建了一个新的Android Studio项目;如上所述,我所做的唯一更改是对res/layout/activity_main.xml的更改.我还包括了我正在使用的可绘制对象.我希望调试能力比我高的人能够下拉该项目,或者使用上述布局简单地创建自己的新项目(这就是全部,尽管无论如何都需要我的可绘制对象)./p>

我想澄清的是,我不是在寻找解决此问题的方法,而是试图理解.问题是,经过2天的测试,我发现了六种看似修复"问题的方法.问题.但是它们都没有任何意义,而且它们似乎都不相关.如果我不知道为什么为什么解决了该问题,那么我将不知道如何在将来的开发过程中避免该问题.

观察

如上所述,我发现了几种修复"程序的方法.问题(即使其不再崩溃),但似乎都没有关系.这里有一些:

  • 将几乎所有元素的visibility设置为gone.
  • 从根元素中删除background.
  • drawable/image_two_tiled.xml删除行android:tileMode="repeat"
    • 请注意,将tileMode更改为其他内容(例如,clamp)不能解决该问题. 仅将其删除(或将其设置为disabled).
  • 设置id为"@ + id/imageTwo"的ImageView的src.到image_two_smaller_tiled.
    • 这是与image_two_tiled完全相同的图像,只是分辨率较小.

这绝不是一个详尽的清单,但是它使您了解这些修补程序的脱节程度(尤其是tileMode修补程序).

据我所知,这似乎是某种内存问题.我对android开发相当陌生,所以我对需要使用的内存限制不太熟悉,但是如果我让堆爆炸时只有很少的图像并且尺寸很小,我会非常担心(至少,肯定不大).

如果任何人都可以准确地告诉我是什么导致我的应用崩溃,那么我会的. . .好吧,我将非常感谢. 由于尝试自己调试两天后,我准备退出android开发:(

编辑

应用程序本身不会引发异常,但是当我不仅仅从应用程序中过滤日志消息时,logcat确实在崩溃时始终显示此错误.

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:我开始怀疑这是我的手机而不是应用程序存在的问题,因为我可以在平板电脑上正常运行它.我真的很想知道是否其他人尝试在他们的Galaxy s6上运行此问题. 特别是S6 -它会告诉我是 my 手机还是所有S6的问题.

解决方案

此先前的SO帖子解答解决了我的问题.

简而言之,是导致整个问题的BitmapDrawable.不幸的是,我不能说出为什么可以解决此问题,但是将使用位图的视图设置为layerType="software"可以防止崩溃.

  • 在布局中:android:layerType="software"
  • 在代码中:view.setLayerType(View.LAYER_TYPE_SOFTWARE, null);

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.

解决方案

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);

这篇关于Android-看似无害的布局会导致应用在短时间使用后崩溃而没有任何错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆