【问题标题】:NullPointerException while trying to bind at SimpleCursorAdapter尝试在 SimpleCursorAdapter 处绑定时出现 NullPointerException
【发布时间】:2014-08-24 23:11:28
【问题描述】:

在我发现我的错误之前,我已经问过a question。但是现在我面临另一个问题。我已经检查了 StackOverflow 上询问的所有类似错误,但没有成功。任何帮助都可以得到。

这里的想法是我从 DB 获取图像名称,因此取决于这些名称,来自 Drawable 文件夹的图像将显示在 listView 以及描述中,但我在 @987654327 收到错误 NullPointException @。

这里是sn-p的代码:

private void populateListView() {
    ListView customListView = (ListView)findViewById(R.id.lvCustom);

    Cursor cursor = DBhelper.getAllimages();

    startManagingCursor(cursor);

    String[] from = { DBhelper.COLUMN_PIC_URL, DBhelper.COLUMN_PIC_DESC};
    int[] to = {R.id.ivImg, R.id.tvTitle};
    SimpleCursorAdapter cursorAdapter = new SimpleCursorAdapter(this, R.layout.custom_listview_row, cursor, from, to, 0);

    cursorAdapter.setViewBinder(new ViewBinder() {
        @Override
        public boolean setViewValue(View view, Cursor cursor, int columnIndex) {
            ImageView imageImageView = (ImageView)findViewById(R.id.ivImg);
            String[] imgNames = new String[cursor.getCount()];
            int[] imgResourceIds = new int[cursor.getCount()]; 
            for(int i=0; i<cursor.getCount(); i++){
                imgNames[i] = cursor.getString(cursor.getColumnIndex(DBhelper.COLUMN_PIC_URL));
                imgResourceIds[i] = getResources().getIdentifier(imgNames[i], "drawable", getPackageName());
                imageImageView.setImageResource(imgResourceIds[i]);
                cursor.moveToNext();
            }

            return true;
        }
    });

    customListView.setAdapter(cursorAdapter);

}

这是来自LogCat错误

我试图记录imgNames[i] 的输出,它正确地从数据库返回url pic,imgResourceIds[i] 也正确地返回图像资源ID(它不返回NULL,而是类似:295731)。但它停在imageImageView.setImageResource(imgResourceIds[i]);

要查看NullPointerException 的来源,我注释掉了imageImageView.setImageResource(imgResourceIds[i]);。这次 imageNames(带有 TAG 的那些)和 imgResourceIds(那些系统打印出来的) 正确但翻了一番,当我删除 cursor.MoveToNext() 时,最后一行翻了一番。这是它的屏幕截图:

我已经尝试了堆栈上有关获取 NullException 的所有建议,但没有成功。知道我在哪里做错了吗?

【问题讨论】:

  • setViewValue() 中的哪一行是有问题的 NullPointer?
  • @JaySnayder 我记录了所有参数,没有人返回 null,但应用程序在 imageImageView.setImageResource(imgResourceIds[i]); 处中断
  • 当然其中一个返回null。这就是将错误作为 NullPointer 抛出的原因。您只需要弄清楚是不是 imageName 没有正确返回导致它,还是 getIdentifier 是问题所在,或者完全与设置有关的其他原因导致 setImageResource() 返回 null 值
  • @JaySnayder imageName 进展顺利(它以 .jpg 扩展名存储在数据库中,但我删除了它)imgResourceIds 也不是空的......但因为它在循环内只有第一个元素数组的显示(在 LogCat 中)并且循环在第一个参数之后被打破,就在它获得 imgResourceIds 之后。看起来问题出在我尝试setImageResource
  • @JaySnayder 我编辑了这个问题。你能检查一下,也许你明白我错过的点了吗?

标签: android


【解决方案1】:

目前尚不清楚ImageView 的实际位置。但是从这一行来看

int[] to = {R.id.ivImg, R.id.tvTitle};

它看起来像是每个 ListViews 项目的一部分。所以你应该在项目内部找到视图。

试试这条线,看看效果如何:

ImageView imageImageView = (ImageView) view.findViewById(R.id.ivImg);

另外,我觉得你在 ImageView 上循环 setImageResource 很奇怪。

这就是你的 ViewBinder 的样子:

cursorAdapter.setViewBinder(new SimpleCursorAdapter.ViewBinder() {
    @Override
    public boolean setViewValue(View view, Cursor cursor, int columnIndex) {
        ImageView imageImageView = (ImageView) view.findViewById(R.id.ivImg);
        String name = cursor.getString(cursor.getColumnIndex(DBhelper.COLUMN_PIC_URL));
        int resourceId = getResources().getIdentifier(name, "drawable",
                getPackageName());
        imageImageView.setImageResource(resourceId);

        return true;
    }
});

再一次,我不明白为什么当 CursorAdapter 可以自己处理设置 ImageView 时,您使用 ViewBinder。

【讨论】:

  • 在同一行尝试过相同的 NPE。 R.id.ivImg 它是在 custom_listview_row 布局中定义的 ImageView(这是 ListView 的一行的布局)我正在循环 setImageResource 以设置数组中的每个图像。有没有其他方法可以设置多个图片资源ID?
  • @hrskrs 但您将它们全部设置为同一个 ImageView,我不明白这有什么意义。
  • 如何设置不同的?我没明白你的意思。 ImageView 是 ListView 的一项。我想要实现的是我将所有图像从 DB 放到 ListView
  • @hrskrs 更新了答案,但我怀疑它会起作用。但是希望您至少可以了解 ViewBinder 应该如何工作。
【解决方案2】:

可能你没有在布局文件中初始化 ivImg 中的 ImageView。检查是否在 custom_listview_row 中定义了 ImageView R.id.ivImg

【讨论】:

  • 是的,我仔细检查并定义了
【解决方案3】:

好的,问题出在return 声明中。我忽略的一件事是 setViewValue 需要 validation 关于 return 声明。

setViewValue 中开始任何操作之前,将 boolean 值初始化为 falseand if the action is successful assign the value totrue` 并返回该值:

boolean binded = false;

if(view instanceof ImageView){
//your actions
binded = true;
}

return binded;

【讨论】:

    猜你喜欢
    • 2023-03-31
    • 1970-01-01
    • 1970-01-01
    • 2017-08-13
    • 2010-12-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-07-22
    相关资源
    最近更新 更多