我一直在研究可能会Native Android App遇到一些虫子的地方Android 2.3.3 versions and below Android 3.0 version。
Native Android App
Android 2.3.3 versions and below Android 3.0 version
我没有到达代码的确切位置,因为在Logcat中编译了每一行代码,但最后却得到了非常奇怪的错误,在下面描述日志:
java.lang.StackOverflowError at java.util.concurrent.locks.ReentrantLock$NonfairSync.tryAcquire(ReentrantLock.java:189) at java.util.concurrent.locks.AbstractQueuedSynchronizer.acquire(AbstractQueuedSynchronizer.java:1171) at java.util.concurrent.locks.ReentrantLock$NonfairSync.lock(ReentrantLock.java:185) at java.util.concurrent.locks.ReentrantLock.lock(ReentrantLock.java:261) at java.util.concurrent.CopyOnWriteArrayList.removeRange(CopyOnWriteArrayList.java:569) at java.util.concurrent.CopyOnWriteArrayList.remove(CopyOnWriteArrayList.java:366) at java.util.concurrent.CopyOnWriteArrayList.remove(CopyOnWriteArrayList.java:376) at android.view.ViewTreeObserver.removeOnPreDrawListener(ViewTreeObserver.java:377) at android.widget.TextView.onDraw(TextView.java:4085) at android.view.View.draw(View.java:6986) at android.view.ViewGroup.drawChild(ViewGroup.java:1739) at android.view.ViewGroup.dispatchDraw(ViewGroup.java:1466) at android.view.ViewGroup.drawChild(ViewGroup.java:1737) at android.view.ViewGroup.dispatchDraw(ViewGroup.java:1466) at android.view.ViewGroup.drawChild(ViewGroup.java:1737) at android.view.ViewGroup.dispatchDraw(ViewGroup.java:1466) at android.view.View.draw(View.java:6989) at android.view.ViewGroup.drawChild(ViewGroup.java:1739) at android.view.ViewGroup.dispatchDraw(ViewGroup.java:1466) at android.view.View.draw(View.java:6989) at android.view.ViewGroup.drawChild(ViewGroup.java:1739) at android.view.ViewGroup.dispatchDraw(ViewGroup.java:1466) at android.view.ViewGroup.drawChild(ViewGroup.java:1737) at android.view.ViewGroup.dispatchDraw(ViewGroup.java:1466) at android.view.ViewGroup.drawChild(ViewGroup.java:1737) at android.view.ViewGroup.dispatchDraw(ViewGroup.java:1466) at android.view.View.draw(View.java:6989) at android.view.ViewGroup.drawChild(ViewGroup.java:1739) at android.view.ViewGroup.dispatchDraw(ViewGroup.java:1466) at android.view.View.draw(View.java:6989) at android.view.ViewGroup.drawChild(ViewGroup.java:1739) at android.view.ViewGroup.dispatchDraw(ViewGroup.java:1466) at android.view.ViewGroup.drawChild(ViewGroup.java:1737) at android.view.ViewGroup.dispatchDraw(ViewGroup.java:1466) at android.view.ViewGroup.drawChild(ViewGroup.java:1737) at android.view.ViewGroup.dispatchDraw(ViewGroup.java:1466) at android.view.View.draw(View.java:6989) at android.widget.FrameLayout.draw(FrameLayout.java:361) at android.view.ViewGroup.drawChild(ViewGroup.java:1739) at android.view.ViewGroup.dispatchDraw(ViewGroup.java:1466) at android.view.ViewGroup.drawChild(ViewGroup.java:1737) at android.view.ViewGroup.dispatchDraw(ViewGroup.java:1466) at android.view.View.draw(View.java:6989) at android.view.ViewGroup.drawChild(ViewGroup.java:1739) at android.view.ViewGroup.dispatchDraw(ViewGroup.java:1466) at android.view.View.draw(View.java:6989) at android.view.ViewGroup.drawChild(ViewGroup.java:1739) at android.view.ViewGroup.dispatchDraw(ViewGroup.java:1466) at android.view.View.draw(View.java:7093) at android.widget.FrameLayout.draw(FrameLayout.java:361) at android.widget.ScrollView.draw(ScrollView.java:1421) at android.view.ViewGroup.drawChild(ViewGroup.java:1739) at android.view.ViewGroup.dispatchDraw(ViewGroup.java:1466) at android.view.ViewGroup.drawChild(ViewGroup.java:1737) at android.view.ViewGroup.dispatchDraw(ViewGroup.java:1466) at android.view.View.draw(View.java:6989) at android.widget.FrameLayout.draw(FrameLayout.java:361) at android.view.ViewGroup.drawChild(ViewGroup.java:1739) at android.view.ViewGroup.dispatchDraw(ViewGroup.java:1466) at android.view.ViewGroup.d
当我检查代码时,没有发现stackoverflow的痕迹。当我在android 3.0或更高版本上运行相同的代码时,它可以有效运行并正确运行,但在android 2.3.3或3.0以下版本上却无法运行。请让我知道是否有人知道这个问题吗?
[编辑]我正在使用Tab活动,因此主布局为Main.xml,在该子活动中包含名为firstTab_Results.xml的视图,它具有一个表布局,该表布局实际上执行inflate_table_firstTab.xml布局的最多20个视图。
[编辑2]无论如何我都不会递归它,它会将最多20个视图(inflate_table_firstTab.xml)膨胀为表格布局(firstTab_Results.xml)。
Note : Stackoverflow happens on single or dual core processor devices & without fullscreen mode .
您的视图层次结构太深。以递归方式绘制层次结构时,堆栈空间用完了,无法深入层次结构,如堆栈跟踪中所观察到的那样。
之所以仅在较早的设备中发生这种情况,是因为UI线程堆栈大小在Android2.3设备中仅为12kB,而在较新的OS版本中为16kB。
如何减少视图层次结构深度?只需尽可能避免嵌套布局。您发布的布局太复杂了,无法在此Stack Overflow模型中为您解决这些布局(需要花费一些时间才能正确完成,并且可能太具体以至于无法帮助其他人),但是这里有一些一般性准则:
删除不必要的布局。例如,在firsttab_results.xml中,只需要TableLayout,RelativeLayout和LinearLayout顶部几乎没有用。在您的ScrollView布局中,至少有6个嵌套布局,一个或最多两个可能就足够了。
TableLayout
RelativeLayout
LinearLayout
ScrollView
布局中的一个孩子是代码气味。在大多数情况下,可以将子级移动到其父级布局并使用边距调整布局。
通常几乎不需要将布局放在内RelativeLayout。相对布局在使用相对位置和子基线对齐来布置其子项方面功能强大。
如果您仅需要一个嵌套布局background,请尝试将背景移至例如View与嵌套布局具有相同大小的,然后在其顶部布局其他元素。
background
View
请注意Android Lint警告。该工具在检测可以简化的视图层次结构复杂性方面已经变得非常出色。