Reputation: 4480
I have asked a question before where i found my mistake. However now i am facing with another problem. I have checked all the similar errors asked on StackOverflow but without success.Any help is appriciated.
The idea here is that i am getting image names from DB so depending on those names images from Drawable
folder will be shown in a listView
together with a description but im getting an error of NullPointException
at setViewValue
.
Here is the code snippet:
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);
}
Here is the Error from LogCat
:
I have tried to log the output of imgNames[i]
where it returns the url pic from the DB correctly and imgResourceIds[i]
where it return the image resource id correctly also(it does not return NULL but something like: 295731). But it stops at imageImageView.setImageResource(imgResourceIds[i]);
To see from where that NullPointerException
is coming, i commented out imageImageView.setImageResource(imgResourceIds[i]);
. This time imageNames(those with a TAG) and imgResourceIds(those system printed out) came correctly but doubled, when i removed cursor.MoveToNext()
last row were doubled. Here is the screen shot of that:
I have tried all the suggestions on stack about gettin a NullException but without success. Any idea where i am doing mistake?
Upvotes: 0
Views: 134
Reputation: 4480
Ok guys the problem was at return
statement. One thing that i was ignoring was that setViewValue
requires a validation
about return
statement.
Before starting any action in setViewValue
initialise a boolean
value to falseand if the action is successful assign the value to
true` and return that value:
boolean binded = false;
if(view instanceof ImageView){
//your actions
binded = true;
}
return binded;
Upvotes: 0
Reputation: 44118
It is unclear where the ImageView
is actually located. But judging from this line
int[] to = {R.id.ivImg, R.id.tvTitle};
It looks like it's a part of each ListView
s item. So you should be finding the view inside of the item.
Try this line and see how it goes:
ImageView imageImageView = (ImageView) view.findViewById(R.id.ivImg);
Also, I find it weird that you are looping setImageResource
on the ImageView
.
This is what your ViewBinder should look like:
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;
}
});
Then again I don't understand why you are using a ViewBinder when the CursorAdapter can handle setting an ImageView by itself.
Upvotes: 1
Reputation: 144
It maybe you did not initialize the ImageView in ivImg in layout file. Check if you have defined ImageView R.id.ivImg in custom_listview_row
Upvotes: 0