2015-12-05 146 views
1

所以我做以下適配器可擴展的ListView:奇怪的空指針異常的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; 
    } 
} 

當我加載此片段包含列表,我得到以下異常:

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) 

這條線,它的指向,148是我吹的觀點線:

if(v==null) v=LayoutInflater.from(getContext()).inflate(R.layout.tips_subcategory_view, null, false); 

我嘗試刪除i,傳遞實際的父代而不是null,並從碎片或通過其他方式獲取充氣器。我無能爲力。

更新:爲防萬一有人再次遇到此問題,我無法寫出答案,因爲有人將此標記爲重複。 問題是在我正在膨脹的XML中使用<view而不是<View。編者對此一無所知,但這個問題以這種方式表達自己。

+0

[This is the evil line](http://grepcode.com/file/repo1.maven.org/maven2/org.robolectric/android-all/5.0.0_r2-robolectric-1/android/view/LayoutInflater的.java#715)。你應該使用調試器來知道爲什麼attrs.getAttributeValue(null,「class」);'返回null。 – Tom

回答

1
  1. 您還沒有列入您的適配器的構造,通過它你會被傳遞活動/片段上下文適配器,並且您將充氣內getGroupView()和getChildView()方法的視圖時將使用相同的上下文。 請包括。
+0

適配器類在Fragment類中寫入。所以我基本上使用片段佈局inflater,我在onCreate中存儲在一個實例變量 –

1

讓我爲你從我工作的代碼片斷

public class AccordionAdapter extends BaseExpandableListAdapter { 

    private Context mContext = null; 
    private LayoutInflater mInflater = null; 
    private ArrayList<SearchBean> mDataList = null; 

    public AccordionAdapter(Context iContext, ArrayList<SearchBean> iDataList) { 
    mContext = iContext; 
    mInflater = LayoutInflater.from(mContext); 
    mDataList = iDataList; 
    } 

    @Override 
    public Object getChild(int arg0, int arg1) { 
    return mDataList.get(arg0).getmChilds().get(arg1); 
    } 

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

    return childPosition; 
    } 

    @Override 
    public View getChildView(int groupPosition, int childPosition, 
     boolean isLastChild, View convertView, ViewGroup parent) { 
    ViewHolder holder = new ViewHolder(); 
    convertView = mInflater.inflate(R.layout.new_child_item, null); 
    holder.mTextView = (TextView) convertView 
     .findViewById(R.id.new_child_itemtxv); 

    holder.mTextView 
     .setText(mDataList.get(groupPosition).getmChilds() 
      .get(childPosition)[0] 
      + " - " 
      + mDataList.get(groupPosition).getmChilds() 
       .get(childPosition)[1]); 
    return convertView; 
    } 

    @Override 
    public int getChildrenCount(int groupPosition) { 

    return mDataList.get(groupPosition).getmChilds().size(); 
    } 

    @Override 
    public Object getGroup(int groupPosition) { 

    return mDataList.get(groupPosition); 
    } 

    @Override 
    public int getGroupCount() { 

    return mDataList.size(); 
    } 

    @Override 
    public long getGroupId(int groupPosition) { 

    return groupPosition; 
    } 

    @Override 
    public View getGroupView(int groupPosition, boolean isExpanded, 
     View convertView, ViewGroup parent) { 
    ViewHolder holder = new ViewHolder(); 
    convertView = mInflater.inflate(R.layout.new_group_item, null); 
    holder.mTextView = (TextView) convertView 
     .findViewById(R.id.new_grp_itemtxv); 
    holder.mTextView.setText(mDataList.get(groupPosition).getmGroup()); 
    return convertView; 
    } 

    @Override 
    public boolean hasStableIds() { 

    return true; 
    } 

    @Override 
    public boolean isChildSelectable(int groupPosition, int childPosition) { 

    return true; 
    } 

    // Data part 
    class ViewHolder { 
    private TextView mTextView = null; 
    } 
} 

莫代爾類使用

public class SearchBean { 
    public String getmGroup() { 
     return mGroup; 
    } 

    public void setmGroup(String mGroup) { 
     this.mGroup = mGroup; 
    } 

    private String mGroup = null; 

    public ArrayList<String[]> getmChilds() { 
     return mChilds; 
    } 

    public void setmChilds(ArrayList<String[]> mChilds) { 
     this.mChilds = mChilds; 
    } 

    private ArrayList<String[]> mChilds = null; 
} 

創建groupview和childview在每個單一的TextView從兩種佈局文件。

用一些虛擬數據創建SearchBean的數組列表。

然後使用此AccordionAdapter將數據設置爲expandablelistview。

希望它有幫助。