即使这么忙,还是要记录下这个AndroidStudio的坑:Binary XML file line #20: You must supply a layout_width attribute.

本文讲述了作者在使用Android Studio时遇到的一个问题:在尝试实现嵌套滑动效果时,遇到了'Binary XML file line #20: You must supply a layout_width attribute.'的错误。经过一系列排查,发现错误并非在当前布局文件的第20行,而是在一个被包含的布局文件的第20行,由于Android Studio没有明确提示,导致了排查时间的浪费。作者提醒开发者,遇到此类错误应检查所有包含的布局文件。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

使用的是AS 2.1.1版本。没有升级的原因的是怕比较坑。


但是... 还是被坑了。


项目中要使用嵌套滑动的效果.很多个人详情页(知乎,微博等等)目前主流都需要这种效果.. 

使用Android Design Support 库中  CoordinatorLayout  配合collapsingtoolbarlayout appbarlayout配合可以实现。

自己实现NestedScrollingParent  NestedScrollingChild 接口也可以实现。


但是我们项目中使用的是自定义的Listview,而Listview并没有实现NestedScrollingChild ,只是在API>=21的时候 才有enable nestedScrolling的属性。

评估了一下,切换成RecyclerView代价太大,大量的adpater,自定义下拉刷新,上拉加载 的效果都需要重新写。

自定义listview不能换。

毫无疑问,要支持listview,得自己动手。

项目紧,时间急,想赶紧上车,没太多时间研究Nestedscroll如何实现,就各种找了能支持Listview 嵌套滑动的开源控件。


各种调试了大半天,头晕脑胀之后...   开始掉坑里了... 


使用了https://github.com/ta893115871/StickyNavLayout 来实现嵌套滑动。 各种布局设置好后。run~~~  很自信的点开activity

Caused by: java.lang.RuntimeException: Binary XML file line #20: You must supply a layout_width attribute.

at android.app.Activity.setContentView(Activity.java:2184)

OK。。20行。 少一个layout_width 

我瞟了一眼。 反复看了3遍, xml 20 行,

android:layout_width="match_parent"
android:layout_height="200dp"
android:background="#fee"
android:orientation="vertical"


妥妥的没问题。 instant run的问题吧。我clean 。继续run 点开activity。


Caused by: java.lang.RuntimeException: Binary XML file line #20: You must supply a layout_width attribute.

at android.app.Activity.setContentView(Activity.java:2184)

又给我蹦了。


Interesting.我调整了布局,让20行那一片区域都没有布局代码,并且反复检查了一般每一个Layout和view 。都有layout_width、 

再clean ,再run。 


Caused by: java.lang.RuntimeException: Binary XML file line #20: You must supply a layout_width attribute.

at android.app.Activity.setContentView(Activity.java:2184)

厉害了。我20行都没有代码了,还给我报20行缺少width属性。我怀疑是不是代码里面 哪里把某个view取出来,然后把layout_width置空了??  Ok 把代码都注释了。


Caused by: java.lang.RuntimeException: Binary XML file line #20: You must supply a layout_width attribute.

at android.app.Activity.setContentView(Activity.java:2184)


可以的。 那只能一个一个注释布局了。 布局注释到只剩根布局的时候,错误终于消除了。


有点头晕。 这点小问题,折腾2小时..  下班回去 睡了一觉。。


第二天早上。


无意间点开布局其中的一个include文件



竟然是这样的。刚好是第20行。


 Rendering Problems NOTE: One or more layouts are missing the layout_width or layout_height attributes. These are required in most layouts.


我去.这样androidstudio 在文件名这都不给我来个红线。一样可以build 可以run 。

太牛逼了。


总结一下Caused by: java.lang.RuntimeException: Binary XML file line #20: You must supply a layout_width attribute.

报20行错误,这个20行不一定是当前布局文件下的,也可能是任何Include的布局文件下的第20行。。。


这个道理早点知道,也就知道早点去各个Include 布局里面的第20行找了。

浪费2个多小时。。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值