【问题标题】:Need help in understanding this ANR report在理解此 ANR 报告时需要帮助
【发布时间】:2018-09-23 21:26:02
【问题描述】:

我有一些使用静态布局的自定义绘图逻辑。有一些自动调整大小的逻辑可以创建静态布局的新实例,以便找到要显示的文本的最佳大小。然而,我很少看到 ANR 并试图了解它们发生时究竟是什么原因造成的。它是这样写的

    "main" prio=5 tid=1 Runnable
  | group="main" sCount=0 dsCount=0 flags=0 obj=0x738bfc20 self=0x7e282c4a00
  | sysTid=31483 nice=-10 cgrp=default sched=0/0 handle=0x7e2d5109c8
  | state=R schedstat=( 22803957927 398883091 2986 ) utm=2163 stm=117 core=3 HZ=100
  | stack=0x7fff593000-0x7fff595000 stackSize=8MB
  | held mutexes= "mutator lock"(shared held)
  #00  pc 00000000003973ec  /system/lib64/libart.so (_ZN3art15DumpNativeStackERNSt3__113basic_ostreamIcNS0_11char_traitsIcEEEEiP12BacktraceMapPKcPNS_9ArtMethodEPv+212)
  #01  pc 000000000045dba0  /system/lib64/libart.so (_ZNK3art6Thread9DumpStackERNSt3__113basic_ostreamIcNS1_11char_traitsIcEEEEbP12BacktraceMapb+348)
  #02  pc 00000000004752bc  /system/lib64/libart.so (_ZN3art14DumpCheckpoint3RunEPNS_6ThreadE+880)
  #03  pc 000000000045e9b0  /system/lib64/libart.so (_ZN3art6Thread21RunCheckpointFunctionEv+416)
  #04  pc 00000000004e20a0  /system/lib64/libart.so (_ZN3art16JniMethodFastEndEjPNS_6ThreadE+92)
  #05  pc 0000000000556360  /system/framework/arm64/boot-framework.oat (Java_android_graphics_Paint_nGetFontMetricsInt__JJLandroid_graphics_Paint_00024FontMetricsInt_2+208)
  at android.graphics.Paint.nGetFontMetricsInt (Paint.java)
  at android.graphics.Paint.getFontMetricsInt (Paint.java:2193)
  at android.text.MeasuredText.addStyleRun (MeasuredText.java:199)
  at android.text.StaticLayout.generate (StaticLayout.java:754)
  at android.text.StaticLayout.<init> (StaticLayout.java:534)
  at android.text.StaticLayout.<init> (StaticLayout.java:479)
  at android.text.StaticLayout.<init> (StaticLayout.java:457)
  at android.text.StaticLayout.<init> (StaticLayout.java:437)

【问题讨论】:

    标签: android performance android-layout android-anr-dialog


    【解决方案1】:

    在 UI 线程上重复创建新布局肯定是昂贵的,它会阻塞线程直到工作完成,这不会发生,因为只要前一个完成就创建新布局.因此,它会导致 UI 阻塞几秒钟,并且由于您在 UI 上运行的繁重处理(用户感觉),Android 会将您的应用程序标记为无响应,并向用户显示 ANR 对话框,允许他/她停止应用程序并拉动摆脱痛苦。

    如果你的算法是这样工作的,你应该做的是在后台线程上移动你的工作并使用new Handler(Looper.getMainLooper()) 到达 UI,或者你可以尝试以某种方式更改你的算法以在没有 UI 阻塞和用户注意的情况下完成它。

    【讨论】:

    • 奇怪的是,它并不是这么简单。作为一个实验,我在一个 while 循环中运行了这个 StaticLayout 初始化 1000 次,但我无法重现崩溃。因此试图了解在什么情况下调用此 ANR。感谢您抽出宝贵时间回复!
    • 根据Android docs,在大约 5 秒的 UI 阻塞后,这当然因设备而异。
    猜你喜欢
    • 2010-11-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-08-06
    • 1970-01-01
    • 2011-10-26
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多