extends BaseAdapter appear NullPointerException

匿名 (未验证) 提交于 2019-12-03 01:44:01

问题:

i am public class MyHomePageAdapter extends BaseAdapter and some code is:

public static class ViewHolder{     public GridView myfriendsGridView;     public Button myfriendsPre,myfriendsNext;       public ListView listview;     public Button searchBu;     public Button footerBu;     public EditText editText;       public ListView myfans_listview;     public Button myfans_searchBu;     public EditText myfans_editText; }   @Override public View getView(int position, View convertView, ViewGroup parent) {     int view = getItemViewType(position);   if(view==1)     {         if (convertView == null)         {               convertView = mInflater.inflate(R.layout.usercenter_myhome_friendschange, null);               //holder=new ViewHolder();               holder.listview=(ListView) convertView.findViewById(R.id.myfriend_list);               holder.searchBu=(Button) convertView.findViewById(R.id.headbutton);               holder.editText=(EditText) convertView.findViewById(R.id.SearchEdit);               convertView.setTag(holder);         }         else         {              holder=(ViewHolder)convertView.getTag();         }          switcher = new ViewSwitcher(context);          holder.footerBu = (Button)mInflater.inflate( R.layout.usercenter_myhomepage_myfriends_loadmore, null);         holder.footerBu.setOnClickListener(new View.OnClickListener() {         @Override         public void onClick(View v) {             // TODO Auto-generated method stub              ++counter;             switcher.showNext();                   //new getMyFriendsMoreItems().execute();                        if(NUMBER_OF_ITEMS_IN_COLUMN+counter*NUMBER_OF_MOVE>ConstValue.myfriendsListInfo.size())                     {                          MyFriendssubSet=getDataSubset(0,ConstValue.myfriendsListInfo.size(),ConstValue.myfriendsListInfo);                      }                     else                      {                           MyFriendssubSet=getDataSubset(0, NUMBER_OF_ITEMS_IN_COLUMN+counter*NUMBER_OF_MOVE,ConstValue.myfriendsListInfo);                       }                       if(MyFriendssubSet.size()>0)                     {                          adapter.setMasteList(MyFriendssubSet);                         adapter.notifyDataSetChanged();                      }                     switcher.showPrevious();                     Log.i("aa", "rr: "+holder.listview);                     //holder.listview.setSelection(MyFriendssubSet.size()-1-NUMBER_OF_MOVE);            }         });            View progress = mInflater.inflate(R.layout.usercenter_myhome_myfriends_loading_footer, null);           switcher.addView(holder.footerBu);           switcher.addView(progress);           holder.listview.addFooterView(switcher);           if(ConstValue.myfriendsListInfo.size() > 0)          {               MyFriendssubSet=getDataSubset(0, NUMBER_OF_ITEMS_IN_COLUMN, ConstValue.myfriendsListInfo);              adapter =new FriendsLazyAdapter((Activity) context, MyFriendssubSet);              holder.listview.setAdapter(adapter);          } 

i found that my mistake is in this :

switcher.showPrevious(); Log.i("aa", "rr: "+holder.listview); //holder.listview.setSelection(MyFriendssubSet.size()-1-NUMBER_OF_MOVE); the holder.listview=null,so if i delete "//" can call holder.listview.setSelection(MyFriendssubSet.size()-1-NUMBER_OF_MOVE); it weill appear NullPointerException ,so my question how to used the holder.listview so that it not null

回答1:

i have modify the code:

              lv=holder.listview;           View progress = mInflater.inflate(R.layout.usercenter_myhome_myfriends_loading_footer, null);           switcher.addView(holder.footerBu);           switcher.addView(progress);           lv.addFooterView(switcher);           if(ConstValue.myfriendsListInfo.size() > 0)          {               MyFriendssubSet=getDataSubset(0, NUMBER_OF_ITEMS_IN_COLUMN, ConstValue.myfriendsListInfo);              adapter =new FriendsLazyAdapter((Activity) context, MyFriendssubSet);              lv.setAdapter(adapter);          } 

i put the holder.listview to a new ListView,the problem is solve



标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!