SLaks 2013-07-29 08:00 采纳率: 0%
浏览 4602

在imageview.setBackgroundResource上的空指针

在布局上有一个imageview,名称是 footer.xml,实际上是给 listview 添加的一个 footer。我需要在 listview 的末尾动态的显示图像。我在java 文件中设置的 imageview 如下:

ImageView footerimage;
    View footerView = getLayoutInflater().inflate(R.layout.footer, null);//This is the footer layout which i have added to the listview
    footerimage = (ImageView)findViewById(R.id.im);
    footerimage.setBackgroundResource(images[i]);//images is an integer array which has drawable resources and i value is not null.
    lv.addFooterView(footerView, null, false);

在上面代码中的第四行获得一个空指针异常,我也把 images[i] 改成 R.drawable.someimage,但是 NPE 还是存在的,下面是 footer Layout:

<?xml version="1.0" encoding="utf-8"?>
<ImageView
    android:id="@+id/im"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_centerHorizontal="true"        
    android:contentDescription="@string/app_name" />

大家给予的任何建议都非常感谢!

  • 写回答

1条回答

  • JaveZh 2013-07-29 08:09
    关注
    footerimage = (ImageView)findViewById(R.id.im);
    

    改成:

    footerimage = (ImageView)footerView.findViewById(R.id.im);
    
    评论

报告相同问题?

悬赏问题

  • ¥15 #MATLAB仿真#车辆换道路径规划
  • ¥15 java 操作 elasticsearch 8.1 实现 索引的重建
  • ¥15 数据可视化Python
  • ¥15 要给毕业设计添加扫码登录的功能!!有偿
  • ¥15 kafka 分区副本增加会导致消息丢失或者不可用吗?
  • ¥15 微信公众号自制会员卡没有收款渠道啊
  • ¥15 stable diffusion
  • ¥100 Jenkins自动化部署—悬赏100元
  • ¥15 关于#python#的问题:求帮写python代码
  • ¥20 MATLAB画图图形出现上下震荡的线条