我在名为 footer.xml 的布局中有一个 ImageView ,它实际上是作为 ListView 的页脚添加的。我需要在 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 仍然存在。 下面是我的页脚布局
<?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) footerView.findViewById(R.id.im);
关于android - imageview.setBackgroundResource 上的空指针,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10310015/