1

When I was trying to display items on ListView it showing NullPointerException. in this i think got null either position or convertview and TextView id and LinearLayout id both correct. The below are the lines to got exception in my code.

viewItem.ll.setBackgroundColor(rainbow[position]);
viewItem.time.setText(timeValuesList.get(position).getElapsetime().replace(" minutes", ""));

Here is MyCode :

       class TimerListAdapter extends BaseAdapter{

        LayoutInflater li;
        Context ct;
        public TimerListAdapter(Context c){
            this.ct=c;
        }
        @Override
        public int getCount() {
            // TODO Auto-generated method stub
            return timeValuesList.size();
        }

        @Override
        public Object getItem(int position) {
            // TODO Auto-generated method stub
            return position;
        }

        @Override
        public long getItemId(int position) {
            // TODO Auto-generated method stub
            return position;
        }

        @Override
        public View getView(final int position, View convertView, ViewGroup arg2) {
            li=(LayoutInflater)ct.getSystemService(LAYOUT_INFLATER_SERVICE);
            final ViewItem viewItem;
            if (convertView == null) {
              viewItem = new ViewItem();
              convertView=li.inflate(R.layout.salontimer_listitem, null);
              viewItem.time=(TextView)convertView.findViewById(R.id.elapse_time);
              viewItem.ll=(LinearLayout)convertView.findViewById(R.id.llayout);
              viewItem.playNpause=(ImageView)convertView.findViewById(R.id.play_pause_time);
              viewItem.addTime=(ImageView)convertView.findViewById(R.id.add_time);
              viewItem.deleteTime=(ImageView)convertView.findViewById(R.id.delete_time);
              viewItem.shareTime=(ImageView)convertView.findViewById(R.id.share_time);
            }else{
                viewItem = (ViewItem) convertView.getTag();
            }
            int[] rainbow = context.getResources().getIntArray(R.array.rainbow);
            viewItem.ll.setBackgroundColor(rainbow[position]);
            viewItem.time.setText(timeValuesList.get(position).getElapsetime().replace(" minutes", ""));
            viewItem.playNpause.setOnClickListener(new OnClickListener() {

                @Override
                public void onClick(View v) {
                    setTime(viewItem.time,viewItem.time.getTag().toString(),Integer.parseInt(timeValuesList.get(position).getElapsetime().replace(" minutes", ""))*60*1000);
                }
            });
            viewItem.addTime.setOnClickListener(new OnClickListener() {

                @Override
                public void onClick(View v) {

                }
            });
            viewItem.deleteTime.setOnClickListener(new OnClickListener() {

                @Override
                public void onClick(View v) {
                    sdb.DeleteSalonTime(timeValuesList.get(position).getId());
                    timeValuesList.remove(position);
                    tAdapter.notifyDataSetChanged();
                }
            });
            viewItem.shareTime.setOnClickListener(new OnClickListener() {

                @Override
                public void onClick(View v) {

                }
            });

            return convertView;
        }
        protected void setTime(final TextView tv, String tag,int time) {
             if (tv.getTag().toString().equals(tag)) {
                 CountDownTimer ct = new CountDownTimer(time, 1000) 
                  { //the timer runs for 30 seconds in this case
                     public void onTick(long millisUntilFinished){
                         int seconds = (int) ((millisUntilFinished) / 1000) % 60 ;
                         int minutes = (int) (((millisUntilFinished ) / (1000*60)) % 60); 
                         String min = Integer.toString(minutes);
                         String sec = Integer.toString(seconds);
                         tv.setText(min+":"+sec);
                     }

                    @Override
                    public void onFinish() {


                    }
                  }.start();
                }
        }

    }
2
  • pls post the logcat and also mention the line number which is responsible for the crash ? Commented Aug 18, 2014 at 7:10
  • 1
    Try to replace this code : viewItem.ll.setBackgroundColor(getResources().getColor(rainbow[position])); Commented Aug 18, 2014 at 7:10

1 Answer 1

1

If the convertView is not null, you retreive the ViewItem from the tag. But you have never set this tag before.

To fix it add this line at the end your if (convertView == null) block:

convertView.setTag(viewItem);
Sign up to request clarification or add additional context in comments.

1 Comment

Awesome. Working well

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.