0

我正在使用 CountDownTimer 来显示 View holder 文本视图中的剩余时间。我的代码结构如下:

     Class Demo extends ArrayAdapter<ListItem>
     {        
         ListItemHolder holder=new ListItemHolder();

        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
           View row = convertView;
           holder.tv= (TextView)row.findViewById(R.id.tv_timer);

          //Starting the countdown timer
            return row;
        }

        static class ListItemHolder
        {
            TextView tv;
            ............        
        }         

        public class MyCount extends CountDownTimer{

            public MyCount(long millisInFuture, long countDownInterval) {
                super(millisInFuture, countDownInterval);
            }           

            @Override
            public void onFinish() {

            }

           @Override


            public void onTick(long millisUntilFinished) {
                   holder.tv.setText("time");
                   }
         }
   }

当我尝试将 onTick 值设置为列表项持有者内的文本视图时,应用程序抛出空指针异常。谁能建议我在这里做错了什么?

提前致谢,

蒂姆森

4

2 回答 2

0

getView 中的 convertView 可能为空

@Override
    public View getView(int position, View convertView, ViewGroup parent) {
       View row = convertView;
       if (row == null) {
           LayoutInflater vi = (LayoutInflater)getSystemService(Context.LAYOUT_INFLATER_SERVICE);
                row = vi.inflate(R.layout.your layout, null);
          }
       holder.tv= (TextView)row.findViewById(R.id.tv_timer);

      //Starting the countdown timer
        return row;
    }
于 2013-04-30T09:15:45.483 回答
0

您需要检查 convertView 是否为空,如果为空,则膨胀视图。

public View getView(int position, View convertView, ViewGroup parent) {
    View row = convertView;
    if(row==null){
        holder = new ListItemHolder();
        LayoutInflater inflater = (LayoutInflater) getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        row = inflater.inflate(R.layout.your_layout, null);
        holder.tv= (TextView)row.findViewById(R.id.tv_timer);
        row.setTag(holder);
    }else{
        holder = (ListItemHolder) row.getTag();
    }
    //Starting the countdown timer
    return row;
}
于 2013-04-30T09:21:29.613 回答