Issue
I am getting an error in my app then it crashes when I reach at the end of the listview. Not sure what is causing this problem and how to solve this issue. I use a cursor to populate my listview. This is my code that I used to populate the listview from:
How to assing an Image to Imageview based on a query results?
Error:
11-15 12:45:22.446: E/AndroidRuntime(276): FATAL EXCEPTION: main
11-15 12:45:22.446: E/AndroidRuntime(276): java.lang.NullPointerException
11-15 12:45:22.446: E/AndroidRuntime(276): at android.content.res.AssetManager.getResourceIdentifier(Native Method)
11-15 12:45:22.446: E/AndroidRuntime(276): at android.content.res.Resources.getIdentifier(Resources.java:1422)
11-15 12:45:22.446: E/AndroidRuntime(276): at com.example.yao.zCustomUsersAdapter.getView(zCustomUsersAdapter.java:62)
11-15 12:45:22.446: E/AndroidRuntime(276): at android.widget.AbsListView.obtainView(AbsListView.java:1294)
11-15 12:45:22.446: E/AndroidRuntime(276): at android.widget.ListView.makeAndAddView(ListView.java:1727)
11-15 12:45:22.446: E/AndroidRuntime(276): at android.widget.ListView.fillDown(ListView.java:652)
11-15 12:45:22.446: E/AndroidRuntime(276): at android.widget.ListView.fillGap(ListView.java:623)
11-15 12:45:22.446: E/AndroidRuntime(276): at android.widget.AbsListView.trackMotionScroll(AbsListView.java:2944)
11-15 12:45:22.446: E/AndroidRuntime(276): at android.widget.AbsListView$FlingRunnable.run(AbsListView.java:2485)
11-15 12:45:22.446: E/AndroidRuntime(276): at android.os.Handler.handleCallback(Handler.java:587)
11-15 12:45:22.446: E/AndroidRuntime(276): at android.os.Handler.dispatchMessage(Handler.java:92)
11-15 12:45:22.446: E/AndroidRuntime(276): at android.os.Looper.loop(Looper.java:123)
11-15 12:45:22.446: E/AndroidRuntime(276): at android.app.ActivityThread.main(ActivityThread.java:4627)
11-15 12:45:22.446: E/AndroidRuntime(276): at java.lang.reflect.Method.invokeNative(Native Method)
11-15 12:45:22.446: E/AndroidRuntime(276): at java.lang.reflect.Method.invoke(Method.java:521)
11-15 12:45:22.446: E/AndroidRuntime(276): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868)
11-15 12:45:22.446: E/AndroidRuntime(276): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)
11-15 12:45:22.446: E/AndroidRuntime(276): at dalvik.system.NativeStart.main(Native Method)
Solution
Put NullPointer check on String.valueOf method
Like:-
package com.example.yao;
import java.util.ArrayList;
import java.util.List;
import android.content.Context;
import android.graphics.drawable.Drawable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ImageView;
import android.widget.TextView;
public class zCustomUsersAdapter extends ArrayAdapter<YAOYVD> {
public zCustomUsersAdapter(Context context, List<YAOYVD> users) {
super(context, 0, users);
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
// Get the data item for this position
// User user = getItem(position);
YAOYVD user = getItem(position);
// Check if an existing view is being reused, otherwise inflate the view
if (convertView == null) {
convertView = LayoutInflater.from(getContext()).inflate(R.layout.zitem_user, parent, false);
}
// Lookup view for data population
TextView tvName = (TextView) convertView.findViewById(R.id.tvName);
TextView tvHome = (TextView) convertView.findViewById(R.id.tvHometown);
ImageView tvImage = (ImageView) convertView.findViewById(R.id.ivUserIcon);
// Populate the data into the template view using the data object
tvName.setText(user.getID_YAO()!=null ? String.valueOf(user.getID_YAO()): null);
//.name);
tvHome.setText(user.getID_YAO() != null ? String.valueOf(user.getID_YAO()): null);
//.hometown);
if(user.getSPIC_YAO() != null{
tvImage.setBackgroundResource(user.getSPIC_YAO());
}
return convertView;
}
}
The problem can be either you string value are getting null or background resource id is wrong. It's better if you put proper check on values.
Answered By - VikasGoyal
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.