android - imageview.setBackgroundResource 上的空指针

标签 android

我在名为 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/

相关文章:

android - 检查数据库中是否已存在记录,如果没有插入则增加计数字段

Android 和 iPhone UI 组件比较

android - 以下 android CustomAdapter 不起作用?

android - 增加android中 ListView 中项目之间的间距

android - 拖放 ImageView

android - sqlite中select语句的使用

android broadcastreceiver 与听众

选项菜单上的android开发

安卓服务问题?

安卓教程 'HelloTabWidget'