Je fais un listView extensible avec l'adaptateur suivant:Weird NullPointer Exception dans ExpandableListView
class SubcategoriesAdapter extends BaseExpandableListAdapter {
@Override
public View getChildView(int groupPosition, int childPosition, boolean isLastChild, View convertView, ViewGroup parent) {
View v=convertView;
if(v==null) v=layoutInflater.inflate(R.layout.tip_view, null, false);
((TextView)v.findViewById(R.id.title)).setText(data.subcategories.get(groupPosition).tips.get(childPosition).title);
((TextView)v.findViewById(R.id.content)).setText(data.subcategories.get(groupPosition).tips.get(childPosition).text);
return v;
}
@Override
public boolean isChildSelectable(int groupPosition, int childPosition) {
return true;
}
@Override
public int getGroupCount() {
return data.subcategories.size();
}
@Override
public int getChildrenCount(int groupPosition) {
return data.subcategories.get(groupPosition).tips.size();
}
@Override
public boolean hasStableIds() {
return false;
}
@Override
public Object getChild(int groupPosition, int childPosition) {
return data.subcategories.get(groupPosition).tips.get(childPosition);
}
@Override
public long getGroupId(int groupPosition) {
return 0;
}
@Override
public Object getGroup(int groupPosition) {
return data.subcategories.get(groupPosition);
}
@Override
public long getChildId(int groupPosition, int childPosition) {
return 0;
}
@Override
public View getGroupView(int groupPosition, boolean isExpanded, View convertView, ViewGroup parent) {
View v=convertView;
Tips.TipsSubcategory me=data.subcategories.get(groupPosition);
if(v==null) v=LayoutInflater.from(getContext()).inflate(R.layout.tips_subcategory_view, null, false);
/* ((TextView)v.findViewById(R.id.title)).setText(me.title);
Picasso.with(getActivity()).load(me.ad_url).into(((ImageView) v.findViewById(R.id.adImageView)));
Picasso.with(getActivity()).load(me.image_url).into((ImageView)v.findViewById(R.id.image)); */
return v;
}
}
Quand je charge ce fragment qui contient la liste, je reçois l'exception suivante:
java.lang.NullPointerException: Attempt to invoke virtual method 'boolean java.lang.String.equals(java.lang.Object)' on a null object reference
at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:715)
at android.view.LayoutInflater.rInflate(LayoutInflater.java:806)
at android.view.LayoutInflater.rInflate(LayoutInflater.java:809)
at android.view.LayoutInflater.rInflate(LayoutInflater.java:809)
at android.view.LayoutInflater.inflate(LayoutInflater.java:504)
at android.view.LayoutInflater.inflate(LayoutInflater.java:414)
at biz.energit.cookingfridge.user.fragments.TipsCategory$SubcategoriesAdapter.getGroupView(TipsCategory.java:148)
Cette ligne qu'il pointe vers, 148 est la ligne où je gonfler le point de vue:
if(v==null) v=LayoutInflater.from(getContext()).inflate(R.layout.tips_subcategory_view, null, false);
J'ai essayé de supprimer le i
, en passant le parent réel au lieu de null
, et d'obtenir l'inflateur du fragment ou par d'autres moyens. Je suis désemparé. MISE À JOUR: Juste au cas où quelqu'un a encore ce problème, je ne peux pas écrire une réponse parce que quelqu'un l'a marqué comme doublon. Le problème était en utilisant <view
au lieu de <View
dans le XML que je gonflais. Le compilateur ne dit rien à ce sujet, mais le problème se résout de cette façon.
[Ceci est la ligne maléfique] (http://grepcode.com/file/repo1.maven.org/maven2/org.robolectric/android-all/5.0.0_r2-robolectric-1/android/view/LayoutInflater .java # 715). Vous devriez utiliser un débogueur pour savoir pourquoi 'attrs.getAttributeValue (null," class ");' retourne 'null'. – Tom