2012-08-03 261 views
0

我使用這個自定義適配器爲我的ListView:空指針異常

public class SideMenuAdapter extends BaseAdapter { 

    private static final int TYPE_MAX_COUNT = 2; 
    private static LayoutInflater inflater = null; 
    private Activity activity; 
    public static String[] values; 
    ListView myList; 

    public SideMenuAdapter(Activity a, String[] sa, ListView lv) { 

     values = sa; 
     activity = a; 
     inflater = (LayoutInflater) activity 
       .getSystemService(Context.LAYOUT_INFLATER_SERVICE); 
     myList = lv; 
    } 

    public int getCount() { 

     return values.length; 
    } 

    public Object getItem(int position) { 

     return position; 
    } 

    public long getItemId(int position) { 

     return position; 
    } 

    public static class ViewHolder { 

     public TextView mainText; 
     public TextView sideText; 
    } 

    public View getView(int position, View convertView, ViewGroup parent) { 

     ViewHolder holder = new ViewHolder(); 
     View vi = convertView; 
     if (vi == null) { 

      //here i am getting NullPointerException 
      vi.setTag(holder); 
     } else { 

      holder = (ViewHolder) vi.getTag(); 
     } 
     vi = inflater.inflate(R.layout.side_menu_list_item, null); 
     holder.mainText = (TextView) vi.findViewById(R.id.mainText_sideMenu); 
     holder.sideText = (TextView) vi.findViewById(R.id.sideText_sideMenu); 
     holder.mainText.setText(values[position]); 
     if(position == 2){ 

      holder.sideText.setText("3"); 
      holder.sideText.setBackgroundResource(R.drawable.orange); 
     } 
     return convertView; 
    } 
    @Override 
    public int getViewTypeCount() { 

     return TYPE_MAX_COUNT; 
    } 
} 

我正在這樣設置適配器到我的列表視圖:

String menuItems[] = new String[] { "My Wants", "Profile", "Notifications", 
      "Feedback", "Logout" }; 
    listView1.setAdapter(new SideMenuAdapter(this, menuItems, listView1)); 

我在哪裏錯誤??

回答

3

你應該初始化vi之前你vi.setTag(holder);

+0

到我使用的ListView項的佈局? – 2012-08-03 08:42:00

+0

是的。這裏是使用自定義BaseAdapter的例子http://thinkandroid.wordpress.com/2010/01/13/custom-baseadapters/ – Saasha 2012-08-03 08:46:42

3

試試這個,可能會幫助ü...

public View getView(int position, View convertView, ViewGroup parent) { 

    ViewHolder holder = new ViewHolder(); 
    View vi = convertView; 
    if (vi == null) { 
     LayoutInflater inflater = ((Activity)activity).getLayoutInflater(); 
     vi = inflater.inflate(R.layout.side_menu_list_item, null); 
     holder.mainText = (TextView) vi.findViewById(R.id.mainText_sideMenu); 
     holder.sideText = (TextView) vi.findViewById(R.id.sideText_sideMenu); 
     vi.setTag(holder); 
    } else { 

     holder = (ViewHolder) vi.getTag(); 
    } 

    holder.mainText.setText(values[position]); 
    if(position == 2){ 

     holder.sideText.setText("3"); 
     holder.sideText.setBackgroundResource(R.drawable.orange); 
    } 
    return convertView; 
} 
+0

yay ... :)這幫助我:http://stackoverflow.com/questions/12590342/空指針的異常逐findviewbyid功能於安卓 – user568021 2013-12-04 05:21:38