尝试在SimpleCursorAdapter上绑定时出现NullPointerException

时间:2014-08-22 16:58:47

标签: android

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

这里的想法是我从数据库中获取图像名称,因此根据这些名称,来自Drawable文件夹的图像将与listView一起显示在描述中,但我得到的错误是{{ 1}} NullPointException

以下是代码段:

setViewValue

以下是来自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); } 错误

enter image description here

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

要查看imageImageView.setImageResource(imgResourceIds[i]);即将到来的地方,我评论了NullPointerException。这次 imageNames(带有TAG的那些)和imgResourceIds(打印出来的那些系统)正确但加倍,当我删除imageImageView.setImageResource(imgResourceIds[i]);时,最后一行加倍。这是屏幕截图: enter image description here

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

3 个答案:

答案 0 :(得分:1)

目前还不清楚ImageView实际位于何处。但从这条线来看

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

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

试试这一行,看看情况如何:

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

另外,我发现你在setImageResource上循环ImageView很奇怪。

这就是你的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的原因。

答案 1 :(得分:0)

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

答案 2 :(得分:0)

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

setViewValue开始执行任何操作之前,将boolean值初始化为false and if the action is successful assign the value to true`并返回该值:

boolean binded = false;

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

return binded;