我在使用适配器实现自定义视图时遇到问题,代码没有显示任何错误,但是当我运行它时,它给了我一个空点异常。
01-08 12:21:43.949: E/AndroidRuntime(12646): FATAL EXCEPTION: main
01-08 12:21:43.949: E/AndroidRuntime(12646): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.jp.blueoceans2/friends.ListFriendsMain}: java.lang.NullPointerException
01-08 12:21:43.949: E/AndroidRuntime(12646): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1816)
01-08 12:21:43.949: E/AndroidRuntime(12646): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1837)
01-08 12:21:43.949: E/AndroidRuntime(12646): at android.app.ActivityThread.access$1500(ActivityThread.java:132)
01-08 12:21:43.949: E/AndroidRuntime(12646): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1033)
01-08 12:21:43.949: E/AndroidRuntime(12646): at android.os.Handler.dispatchMessage(Handler.java:99)
01-08 12:21:43.949: E/AndroidRuntime(12646): at android.os.Looper.loop(Looper.java:143)
01-08 12:21:43.949: E/AndroidRuntime(12646): at android.app.ActivityThread.main(ActivityThread.java:4196)
01-08 12:21:43.949: E/AndroidRuntime(12646): at java.lang.reflect.Method.invokeNative(Native Method)
01-08 12:21:43.949: E/AndroidRuntime(12646): at java.lang.reflect.Method.invoke(Method.java:507)
01-08 12:21:43.949: E/AndroidRuntime(12646): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)
01-08 12:21:43.949: E/AndroidRuntime(12646): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
01-08 12:21:43.949: E/AndroidRuntime(12646): at dalvik.system.NativeStart.main(Native Method)
01-08 12:21:43.949: E/AndroidRuntime(12646): Caused by: java.lang.NullPointerException
01-08 12:21:43.949: E/AndroidRuntime(12646): at friends.ListFriendsMain.onCreate(ListFriendsMain.java:95)
01-08 12:21:43.949: E/AndroidRuntime(12646): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1093)
01-08 12:21:43.949: E/AndroidRuntime(12646): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1780)
01-08 12:21:43.949: E/AndroidRuntime(12646): ... 11 more
适配器的代码如下:
public class FriendsAdapter extends BaseAdapter{
private Activity activity;
ArrayList<HashMap<String, String>> data;
private static LayoutInflater inflater=null;
public ImageLoader imageLoader;
public FriendsAdapter (Activity a, ArrayList<HashMap<String, String>> d) {
activity = a;
data=d;
inflater = (LayoutInflater)activity.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
imageLoader=new ImageLoader(activity.getApplicationContext());
}
public int getCount() {
return data.size();
}
public Object getItem(int position) {
return position;
}
public long getItemId(int position) {
return position;
}
public View getView(int position, View convertView, ViewGroup parent) {
View vi=convertView;
if(convertView==null)
vi = inflater.inflate(R.layout.row_friend_cell, null);
TextView surname_friend = (TextView)vi.findViewById(R.id.surname_friend);
TextView name_friend = (TextView)vi.findViewById(R.id.name_friend);
TextView relation = (TextView)vi.findViewById(R.id.relation);
ImageView avatar = (ImageView)vi.findViewById(R.id.avatar);
HashMap<String, String> friends = new HashMap<String, String>();
friends = data.get(position);
// Setting all values in listview
surname_friend.setText(friends.get(ListFriendsMain.KEY_SURNAME));
name_friend.setText(friends.get(ListFriendsMain.KEY_NAME));
relation.setText(friends.get(ListFriendsMain.KEY_RELATION));
imageLoader.DisplayImage(friends.get(ListFriendsMain.KEY_AVATAR_URL), avatar);
return vi;
}
}
关于我为什么会收到该错误的任何想法?
编辑
主要活动代码指出了错误和我使用的信息。
XMLParser parser = new XMLParser();
//HashMap<String, String> map = parser.getXmlFromUrl(URL, userid);
String xml = parser.getXmlFromUrl(URL, userid);
//Log.v("pictures", "string xml " + xml);
Document doc = parser.getDomElement(xml); // getting DOM element
NodeList nl = doc.getElementsByTagName(KEY_FRIEND);
for (int i = 0; i < nl.getLength(); i++){
HashMap<String, String> map = new HashMap<String, String>();
Element e = (Element) nl.item(i);
// adding each child node to HashMap key value
map.put(KEY_ID, parser.getValue(e, KEY_ID));
map.put(KEY_SURNAME, parser.getValue(e, KEY_SURNAME));
map.put(KEY_NAME, parser.getValue(e, KEY_NAME));
map.put(KEY_RELATION, parser.getValue(e, KEY_RELATION));
map.put(KEY_AVATAR_URL, parser.getValue(e, KEY_AVATAR_URL));
// adding HashList to ArrayList
info.add(map);
}
list=(ListView)findViewById(R.id.list);
adapter = new FriendsAdapter(this, info);
list.setAdapter(adapter);
// Click event for single list row
list.setOnItemClickListener(new OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
}
});
第 95 行具体指的是这一行:
list.setAdapter(adapter);