ListView.addHeaderView抛出的异常及解决方法

本文详细解析了Android开发中使用ListView时遇到的错误代码,重点在于将布局头添加到ListView的问题,通过实例展示了正确的代码实现方式,并解释了错误代码中关于inflate方法使用的不当之处。
   错误代码:  
   LVbook=(ListView)findViewById(R.id.booklistID);
    try { LVbook.addHeaderView(getLayoutInflater().inflate(R.id.booklist_headid, null)); } catch (Exception e) { e.printStackTrace(); }
  正确代码:
     try {
              LVbook.addHeaderView(getLayoutInflater().inflate(R.layout.booklist_head, null));
        } catch (Exception e) {
             e.printStackTrace();
        }
相关API信息:
public View inflate(int resource, ViewGroup root)

Inflate a new view hierarchy from the specified xml resource. Throws InflateException if there is an error.

Parameters:
resource - ID for an XML layout resource to load (e.g., R.layout.main_page)------>问题出现在这里,这里需要的是一个Layout布局ID号,
                                             上面写的是View ID号,犯了个低级错误
root - Optional view to be the parent of the generated hierarchy.

Returns:
The root View of the inflated hierarchy. If root was supplied, this is the root View; otherwise it is the root of the
inflated XML file.
错误截图:
 
 

转载于:https://www.cnblogs.com/lyyh-victory/p/3802357.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值