Android 尝试在SimpleCorsOrAdapter上绑定时出现NullPointerException
我以前问过我在哪里发现了我的错误。然而,现在我面临着另一个问题。我已检查了在StackOverflow上询问的所有类似错误,但未成功。欢迎提供任何帮助 这里的想法是,我从DB获取图像名称,因此根据这些名称,Android 尝试在SimpleCorsOrAdapter上绑定时出现NullPointerException,android,Android,我以前问过我在哪里发现了我的错误。然而,现在我面临着另一个问题。我已检查了在StackOverflow上询问的所有类似错误,但未成功。欢迎提供任何帮助 这里的想法是,我从DB获取图像名称,因此根据这些名称,Drawable文件夹中的图像将与说明一起显示在listView中,但我在setViewValue处得到一个NullPointException错误 以下是代码片段: private void populateListView() { ListView customListView =
Drawable
文件夹中的图像将与说明一起显示在listView
中,但我在setViewValue
处得到一个NullPointException
错误
以下是代码片段:
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);
}
private void populateListView(){
ListView customListView=(ListView)findViewById(R.id.lvCustom);
Cursor Cursor=DBhelper.getAllimages();
开始管理游标(游标);
字符串[]from={DBhelper.COLUMN_PIC_URL,DBhelper.COLUMN_PIC_DESC};
int[]to={R.id.ivImg,R.id.tvTitle};
SimpleCursorAdapter cursorAdapter=新的SimpleCursorAdapter(此,R.layout.custom_listview_行,光标,从,到,0);
cursorAdapter.setViewBinder(新的ViewBinder(){
@凌驾
公共布尔setViewValue(视图、光标、int-columnIndex){
ImageView=(ImageView)findViewById(R.id.ivImg);
String[]imgNames=新字符串[cursor.getCount()];
int[]imgResourceIds=new int[cursor.getCount()];
对于(int i=0;i可能您没有在布局文件的ivImg中初始化ImageView。请检查您是否在custom_listview_行中定义了ImageView R.id.ivImg不清楚ImageView
的实际位置。但是从这行判断
int[] to = {R.id.ivImg, R.id.tvTitle};
它看起来像是每个ListView
s项目的一部分。因此,您应该在项目内部找到该视图
试试这句话,看看它是怎么说的:
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;
}
});
我还是不明白为什么你要使用ViewBinder,而CursorAdapter可以自己设置ImageView。好的,伙计们,问题出在return
语句上。我忽略的一点是setViewValue
需要验证关于return
语句
在setViewValue
中启动任何操作之前,将布尔值初始化为false,如果操作成功,则将该值指定为true`并返回该值:
boolean binded = false;
if(view instanceof ImageView){
//your actions
binded = true;
}
return binded;
setViewValue()中的哪一行是有问题的空指针?@JaySnayder我记录了所有参数,没有人返回空值,但应用程序在imageImageView.setImageResource(imgreSourceId[I])中断
当然,其中一个返回null。这就是为什么错误被作为null指针抛出。您只需找出是imageName没有正确返回导致它的原因,还是getIdentifier出了问题,或者是完全由于安装程序的其他原因导致setImageResource()返回null值@JaySnayderimageName
进展顺利(它在DB上以.jpg扩展名存储,但我删除了它)imgResourceIds
也不为空……但由于它在循环中,所以只显示了数组的第一个元素(在LogCat中)在第一个参数之后,循环就被打破了,就在它获得imgResourceIds
之后。问题似乎是当我试图setImageResource
@JaySnayder时,我编辑了这个问题。你能检查一下吗,也许你抓住了我遗漏的点了吗?是的,我重复检查了一遍,并且它被定义为,但在同一行有相同的NPE。 R.id.ivImg
是在自定义列表视图行
布局中定义的图像视图
(这是列表视图行的布局
)我正在循环setImageResource
来设置数组中的每个图像。是否有其他方法来设置多个图像资源ID?@hrskrs但是您将它们都设置为同一个ImageView,我不明白这有什么意义。我如何设置不同的图像?我不明白您的观点。ImageView是ListView的一项。我正在尝试实现的是eve是我把所有的图像从DB放到aListView@hrskrs更新了答案,但我怀疑它是否有效。不过,希望您至少可以了解一下ViewBinder应该如何工作。