子项的ExpandableListView中的NullPointerException

时间:2016-04-25 11:11:40

标签: android expandablelistview

我得到NullPointerException当我点击父项目时。你可以通过查看我的代码来建议这个原因。我没有得到任何下一步我必须采取的这个错误。试着建议我看看我的代码。我也发布了logcat的屏幕截图。

先谢谢。

enter image description here

public class ExpandListAdapter extends BaseExpandableListAdapter {

private Context context;
private ArrayList<Group> groups;

ImageLoader imageLoader = MyApplication.getInstance().getImageLoader();

public ExpandListAdapter(Context context, ArrayList<Group> groups) {
    this.context = context;
    this.groups = groups;
}

@Override
public Object getChild(int groupPosition, int childPosition) {
    ArrayList<Child> chList = groups.get(groupPosition).getItems();
    return chList.get(childPosition);
}

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

@Override
public View getChildView(int groupPosition, int childPosition,
                         boolean isLastChild, View convertView, ViewGroup parent) {

    Child child = (Child) getChild(groupPosition, childPosition);
    if (convertView == null) {
        LayoutInflater infalInflater = (LayoutInflater) context
                .getSystemService(context.LAYOUT_INFLATER_SERVICE);
        convertView = infalInflater.inflate(R.layout.child_item, null);
    }

    if (imageLoader == null)
        imageLoader = MyApplication.getInstance().getImageLoader();

    TextView tv = (TextView) convertView.findViewById(R.id.country_name);
    NetworkImageView iv = (NetworkImageView) convertView
            .findViewById(R.id.flag);

    tv.setText(child.getName().toString());
    iv.setImageUrl(String.valueOf(child.getImage()), imageLoader);

    return convertView;
}

@Override
public int getChildrenCount(int groupPosition) {
    ArrayList<Child> chList = groups.get(groupPosition).getItems();
    return chList.size();
}

@Override
public Object getGroup(int groupPosition) {
    return groups.get(groupPosition);
}

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

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

@Override
public View getGroupView(int groupPosition, boolean isExpanded,
                         View convertView, ViewGroup parent) {
    Group group = (Group) getGroup(groupPosition);
    if (convertView == null) {
        LayoutInflater inf = (LayoutInflater) context
                .getSystemService(context.LAYOUT_INFLATER_SERVICE);
        convertView = inf.inflate(R.layout.group_item, null);
    }
    TextView tv = (TextView) convertView.findViewById(R.id.group_name);
    tv.setText(group.getName());
    return convertView;
}

@Override
public boolean hasStableIds() {
    return true;
}

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

}

XML文件:

<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingTop="4dp"
android:paddingBottom="4dp"
android:layout_marginLeft="50dp">

<TextView
    android:id="@+id/name"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignBottom="@+id/flag"
    android:layout_marginBottom="14dp"
    android:layout_marginLeft="29dp"
    android:layout_toRightOf="@+id/flag"
    android:text="TextView"
    android:textSize="20dp" />

<com.android.volley.toolbox.NetworkImageView
    android:id="@+id/flag"
    android:layout_width="70dp"
    android:layout_height="40dp"
    android:layout_alignParentLeft="true"
    android:layout_alignParentTop="true"
    android:layout_marginLeft="27dp"
    android:src="@drawable/ic_launcher" />

1 个答案:

答案 0 :(得分:1)

您似乎在getChildView方法

中使用了不同的textview
TextView tv = (TextView) convertView.findViewById(R.id.country_name);

<TextView
    android:id="@+id/name"

这会产生nullpointerexception。因此将其改为

TextView tv = (TextView) convertView.findViewById(R.id.name);