• NullPointerException whenever scroll the ListView

    
    
     @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            View newView = convertView;
            DayHolder holder;
            Integer curr = (Integer) theList.get(position);
            if (null == convertView){
                holder = new DayHolder();
                newView = mInflater.inflate(R.layout.activity_select_day,null);
                holder.mText = (TextView) newView.findViewById(R.id.selectDays);
            }else{
                holder = (DayHolder) newView.getTag();
            }
            holder.mText.setText(curr.toString());
            return newView;
        }
    
    1st, do not use inflate(R.layout.activity_select_day,null);. Use inflate(R.layout.activity_select_day, parent, false);. Otherwise, your layouts will not work properly when the root container of the layout file is a RelativeLayout.
    2nd, if you call getTag(), you should be calling setTag(). You are not; hence, your getTag() call will always return null. Call newView.setTag(holder) in your if block, after having created your holder instance.
    AFTER CORRECTION 
     @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            View newView = convertView;
            DayHolder holder;
            Integer curr = (Integer) theList.get(position);
            if (null == convertView){
                holder = new DayHolder();
                newView = mInflater.inflate(R.layout.activity_select_day, parent, false);
                holder.mText = (TextView) newView.findViewById(R.id.selectDays);
                newView.setTag(holder);
            }else{
                holder = (DayHolder) newView.getTag();
            }
            holder.mText.setText(curr.toString());
            return newView;
        }
    
  • 0 comments:

    Post a Comment

    FAVOURITE LINE

    To steal ideas from one is plagiarism. To steal from many is Research.

    ADDRESS

    Mumbai , Maharashtra

    EMAIL

    shikha.pathak6@gmail.com
    shikha.the.swt.pari@gmail.com

    Skype

    shikha_pari