2015-10-18 1 views
0

Je crée un tiroir de navigation avec une liste ExpandableListView, mais je n'arrive pas à comprendre comment gérer les clics de groupe vides. Chaque fois que j'essaie de cliquer sur un groupe vide, j'obtiens une exception NullPointerException qui indique "Tentative d'invocation de la méthode interface 'int java.util.List.size()' sur une référence d'objet null" et qui pointe vers getChildrenCount() méthode.Gestion des clics de groupes vides avec ExpandableListView

Ceci est mon ExpandableListAdapter personnalisé:

ExpandableListAdapter.java

package co.eshg.drawertest; 

import java.util.HashMap; 
import java.util.List; 

import android.content.Context; 
import android.view.LayoutInflater; 
import android.view.View; 
import android.view.ViewGroup; 
import android.widget.BaseExpandableListAdapter; 
import android.widget.TextView; 

public class ExpandableListAdapter extends BaseExpandableListAdapter { 

private Context context; 
private List<String> listDataItem; // header titles 
// child data in format of header title, child title 
private HashMap<String, List<String>> listDataChild; 

public ExpandableListAdapter(Context context, List<String> listDataItem, 
          HashMap<String, List<String>> listChildData) { 
    this.context = context; 
    this.listDataItem = listDataItem; 
    this.listDataChild = listChildData; 
} 

@Override 
public Object getChild(int groupPosition, int childPosititon) { 
    return this.listDataChild.get(this.listDataItem.get(groupPosition)) 
      .get(childPosititon); 
} 

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

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

    final String childText = (String) getChild(groupPosition, childPosition); 

    if (convertView == null) { 
     LayoutInflater inflater = (LayoutInflater) this.context 
       .getSystemService(Context.LAYOUT_INFLATER_SERVICE); 
     convertView = inflater.inflate(R.layout.drawer_child_item, null); 
    } 

    TextView txtListChild = (TextView) convertView 
      .findViewById(R.id.tvChildItem); 

    txtListChild.setText(childText); 
    return convertView; 
} 

@Override 
public int getChildrenCount(int groupPosition) { 
    if (this.listDataChild.get(this.listDataItem.get(groupPosition)).size() != 0) { 
     return this.listDataChild.get(this.listDataItem.get(groupPosition)).size(); 
    } 
    return 1; 
} 

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

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

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

@Override 
public View getGroupView(int groupPosition, boolean isExpanded, 
         View convertView, ViewGroup parent) { 
    String headerTitle = (String) getGroup(groupPosition); 
    if (convertView == null) { 
     LayoutInflater inflater = (LayoutInflater) this.context 
       .getSystemService(Context.LAYOUT_INFLATER_SERVICE); 
     convertView = inflater.inflate(R.layout.drawer_list_item, null); 
    } 

    TextView lblListHeader = (TextView) convertView 
      .findViewById(R.id.tvListItem); 
    lblListHeader.setText(headerTitle); 

    return convertView; 
} 

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

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

Toute aide serait appréciée.

Répondre

2

La liste que vous appelez size() est nulle, donc vous devez d'abord vérifier cela.

Essayez ceci:

@Override 
public int getChildrenCount(int groupPosition) { 
    List childList = listDataChild.get(listDataItem.get(groupPosition)); 
    if (childList != null && ! childList.isEmpty()) { 
     return childList.size(); 
    } 
    return 1; 
} 
+0

Merci pour la réponse. J'ai échangé le code et cela a fonctionné, mais il me donne toujours une exception NullPointerException, et cette fois, il pointe vers les méthodes getChild() et getChildView(). Une idée de comment je pourrais implémenter la même if-statement pour ces méthodes? Merci encore. – user5460905

+0

C'est parce que vous retournez 1 même si la liste des enfants est vide. Sauf si vous créez une vue enfant spéciale pour une liste vide, vous devez renvoyer 0 lorsque la liste est vide. –

+0

Ok! Merci beaucoup. Ça a marché. – user5460905