简体   繁体   中英

Android Listview - Null Pointer Exception

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();
                }
        }

    }

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);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM