getChildView ExpandableListView上的NullPointerException

时间:2014-03-22 23:51:00

标签: android listview arraylist hashmap expandablelistview

我在我的ExpandableListView中的getChildView上获得了NullPointerException,但不确定原因。组列表显示很好,但是当我点击一个组来显示孩子时,我得到了NullPointerException。谁能告诉我我失踪了什么?

public class LazyAdapter extends BaseExpandableListAdapter {

private Activity activity;    
private ArrayList<HashMap<String, String>> groups;
private ArrayList<HashMap<String, String>> child;

private static LayoutInflater inflater = null;
public ImageLoader imageLoader;
private DatabaseHandler dbh;
int flag;

static class RowHolder {
    TextView name;
    TextView id;
    TextView reference;
    TextView vicinity;
    TextView lastcheck;
    TextView number_checkins;
    TextView user;
    ImageView user_image;
    ImageView checkin;
}

public LazyAdapter(Activity a, ArrayList<HashMap<String, String>> d, 
ArrayList<HashMap<String, String>> e) {
    activity = a;
    groups = d;
    child = e;
    inflater = (LayoutInflater) 
activity.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    imageLoader = new ImageLoader(activity.getApplicationContext());
    dbh = new DatabaseHandler(activity);

}

public void clearData() {
    // clear the data
    groups.clear();
}

public Object getChild(int groupPosition, int childPosition) {
    return ((HashMap<String, String>)child.get(groupPosition)).get(childPosition);
}

public long getChildId(int groupPosition, int childPosition) {
    return childPosition;
}


public int getChildrenCount(int groupPosition) {
    return ((HashMap<String, String>)child.get(groupPosition)).size();
}

public View getChildView(int groupPosition, int childPosition, boolean isLastChild,View  
convertView, ViewGroup parent) {
    View vi = convertView;
    RowHolder holder = null;
    if(convertView == null) {
        vi = inflater.inflate(R.layout.child_view, null);
        holder = new RowHolder();
        holder.user_image = (ImageView) convertView.findViewById(R.id.user_image);
        holder.user = (TextView) convertView.findViewById(R.id.user);
        vi.setTag(holder);
    } else {
        holder = (RowHolder) vi.getTag();
    }

    HashMap<String, String> userList;
    userList = child.get(groupPosition);
    imageLoader.DisplayImage(userList.get(MainActivity.KEY_USERIMAGE),    
 holder.user_image);
    holder.user.setText(userList.get(MainActivity.KEY_USER));
    return convertView;
}
public Object getGroup(int groupPosition) {
    return groups.get(groupPosition);
}

public int getGroupCount() {
    return groups.size();
}

public long getGroupId(int groupPosition) {
    return groupPosition;
}

public View getGroupView(int groupPosition, boolean isExpanded, View                

convertView,ViewGroup parent) {
    View vi = convertView;
    RowHolder holder = null;
    if (convertView == null) {
        vi = inflater.inflate(R.layout.group_view, null);
        holder = new RowHolder();
        holder.number_checkins = (TextView) vi.findViewById(R.id.number_checkins);
        holder.id = (TextView) vi.findViewById(R.id.id);
        holder.name = (TextView) vi.findViewById(R.id.name);
        holder.vicinity = (TextView) vi.findViewById(R.id.vicinity);
        holder.lastcheck = (TextView) vi.findViewById(R.id.lastcheck);
        holder.checkin = (ImageView) vi.findViewById(R.id.check_in);

        vi.setTag(holder);
    } else {
        holder = (RowHolder) vi.getTag();
    }

    HashMap<String, String> placeList;
    placeList = groups.get(groupPosition);
    holder.id.setText(placeList.get(MainActivity.KEY_ID));
    holder.name.setText(placeList.get(MainActivity.KEY_NAME));
    holder.vicinity.setText(placeList.get(MainActivity.KEY_VICINITY));
    holder.lastcheck.setText(placeList.get(MainActivity.KEY_LASTCHECK));
    holder.number_checkins.setText(placeList.get(MainActivity.KEY_NUMBERCHECKINS));       

    return vi;

}

public boolean isChildSelectable(int groupPosition, int childPosition) {
    return true;
}

public boolean hasStableIds() {
    return true;
}
}

日志:

FATAL EXCEPTION: main java.lang.NullPointerException at     
lazylist.LazyAdapter.getChildView(LazyAdapter.java:85)
at android.widget.ExpandableListConnector.getView(ExpandableListConnector.java:450)
        at android.widget.AbsListView.obtainView(AbsListView.java:2046)
        at android.widget.ListView.measureHeightOfChildren(ListView.java:1292)
        at android.widget.ListView.onMeasure(ListView.java:1155)
        at android.view.View.measure(View.java:13011)
        at android.widget.RelativeLayout.measureChild(RelativeLayout.java:579)
        at android.widget.RelativeLayout.onMeasure(RelativeLayout.java:392)
        at android.view.View.measure(View.java:13011)
        at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:4706)
        at android.widget.FrameLayout.onMeasure(FrameLayout.java:293)
        at android.view.View.measure(View.java:13011)
        at android.widget.LinearLayout.measureVertical(LinearLayout.java:812)
        at android.widget.LinearLayout.onMeasure(LinearLayout.java:553)
        at android.view.View.measure(View.java:13011)
        at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:4706)
        at android.widget.FrameLayout.onMeasure(FrameLayout.java:293)
        at 
com.android.internal.policy.impl.PhoneWindow$DecorView.onMeasure(PhoneWindow.java:2162)
        at android.view.View.measure(View.java:13011)
        at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:1091)
        at android.view.ViewRootImpl.handleMessage(ViewRootImpl.java:2505)
        at android.os.Handler.dispatchMessage(Handler.java:99)
        at android.os.Looper.loop(Looper.java:154)
        at android.app.ActivityThread.main(ActivityThread.java:4945)
        at java.lang.reflect.Method.invokeNative(Native Method)
        at java.lang.reflect.Method.invoke(Method.java:511)
        at 
com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:784)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551)
        at dalvik.system.NativeStart.main(Native Method)

1 个答案:

答案 0 :(得分:2)

问题是您检查convertView == null,但初始化vi,然后继续使用convertView

我会放弃vi并初始化convertView

注意: 如果您认为这样做:

  

vi = convertView;

     

vi = something;

convertView现在alao引用something,那么你的理解是错误的,应该查看java引用。