在布局上有一个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" />
大家给予的任何建议都非常感谢!
footerimage = (ImageView)findViewById(R.id.im);
改成:
footerimage = (ImageView)footerView.findViewById(R.id.im);