问题描述
当我尝试在 ListView
中扩展布局时发生以下错误:
The following error occurs when I attempt to inflate a layout within a ListView
:
requestLayout() improperly called by android.widget.TextView{...} during layout: running second layout pass
我正在尝试在 ListView
中扩充布局,如下所示:
I am attempting to inflate a layout within a ListView
as follows:
@Override
public View getView(int position, View convertView, ViewGroup parent) {
if(convertView == null){
LayoutInflater inflater = (LayoutInflater) musicActivity.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
convertView = inflater.inflate(R.layout.list_item, parent, false);
...
}else{...}
}
被膨胀的布局看起来很简单,但仍然会产生错误
The layout being inflated can look as simple as the following, and will still produce the error
<TextView
android:id="@+id/txt"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="@dimen/txt_size"/>
我研究过类似的问题,但似乎没有找到可行的解决方案 问题 1,问题 2,问题 3.
I have looked into similar questions, and no solutions found seem to work Question 1, Question 2, Question 3.
有谁知道是什么导致了这种类型的错误?任何故障排除建议?对于更多上下文,此 ListView
显示在 ViewPager
Does anyone know what causes this type of error? Any troubleshooting advice? For more context, this ListView
is displayed within a Fragment
within a ViewPager
这里是完整的 XML 布局(减去一堆属性),仍然导致问题
Here is the full XML Layout (minus a bunch of attributes), that still results in the problem
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:id="@+id/txt1"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<TextView
android:id="@+id/txt2"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<TextView
android:id="@+id/txt3"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<TextView
android:id="@+id/txt4"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
</RelativeLayout>
基于此,我认为 XML 本身不是问题,除非它与我使用 ViewPager 和 Fragments 的事实有关
Based on this, I would think the XML itself is not a problem, unless it has to do with the fact that I am using a ViewPager and Fragments
推荐答案
这个问题好像是android实现的一个bug,请看:https://code.google.com/p/android/issues/detail?id=75516
This issue seems to be a bug in the android implementation, please see: https://code.google.com/p/android/issues/detail?id=75516
通过 ListView.setFastScrollEnabled(true)
在您的代码中激活 ListView
的快速滚动功能将触发此错误,您将开始看到
Activating the fast scroll feature of a ListView
in your code via ListView.setFastScrollEnabled(true)
will trigger this bug and you'll start seeing the
requestLayout() 被 android.widget.TextView{...} 错误调用布局期间:运行第二个布局过程
控制台中的消息.
这个错误一定是在 KitKat (4.4.x) 更新之一中引入的,因为我没有在最初的 KitKat (4.4.0) 版本中看到它.除了丑陋的控制台垃圾邮件和上面的调试消息外,似乎没有其他影响(在某些情况下可能是性能,我还没有测试过).
This bug must have been introduced in one of the KitKat (4.4.x) updates, as I've not seen it with the initial KitKat (4.4.0) release. Apart from the ugly console spamming with the debug message from above, there seem to be no other impacts (maybe performance in some cases, which I haven't tested).
干杯
PS:这不是第一次快速滚动功能被窃听,例如https://code.google.com/p/android/issues/detail?id=63545, 63545 已在 KitKat 4.4.3 中修复,但此后又弹出 75516 --> 似乎是 google 的一个恼人的主题 ;-)
PS: it's not the first time that the fast scroll feature is bugged, e.g. https://code.google.com/p/android/issues/detail?id=63545, 63545 was fixed in KitKat 4.4.3 but 75516 poped up thereafter --> seems to be a vexed subject for google ;-)
编辑 2015 年 5 月 12 日:
几分钟前我将我的 Nexus 7 更新到了 Android 5.1(之前运行的是 5.0)并且在这个新版本中不再看到这个问题.由于 FastScroll 指示器的外观在 5.1 中也发生了变化,我认为 google 解决了这个问题,或者至少注释掉了那些在控制台中发送垃圾邮件的丑陋行......
I updated my Nexus 7 to Android 5.1 some minutes ago (was Running 5.0 before) and stopped seeing this issue in this new version. As the appearance of the FastScroll indicator also changed in 5.1, I assume that google fixed this issue or at least commented out those ugly lines that spammed the console...
75516 &82461 仍然未解决",但我想那些是指同样的问题,现在在 5.1 中解决了.
75516 & 82461 are still 'unresolved', but I guess that those refer to the same issue, that's now resolved in 5.1.
这篇关于Android:requestLayout() 调用不当的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!