[英]ArrayList<TextView> cannot be set
我有一个列表视图适配器,其中包含这些
final ViewHolder holder;
if (convertView == null)
{
view = mInflater.inflate(R.layout.auction_list_item, parent , false);
holder = new ViewHolder();
holder.image = (ImageView) view.findViewById(R.id.auction_picture_imageView);
holder.name = (TextView) view.findViewById(R.id.auction_name);
holder.seller = (TextView) view.findViewById(R.id.auction_seller);
holder.time = (TextView) view.findViewById(R.id.auction_time);
holder.duration = (TextView) view.findViewById(R.id.auction_duration);
holder.currentbid = (TextView) view.findViewById(R.id.auction_currentbid);
holder.buynow = (TextView) view.findViewById(R.id.auction_buynow);
holder.layout = (LinearLayout) view.findViewById(R.id.auction_item);
holder.buynowtext = (TextView) view.findViewById(R.id.auction_buynow_textView);
view.setTag(holder);
} else
{
holder = (ViewHolder) view.getTag();
}
String aucend = auctionList.get(position).get(TAG_AUCTIONENDTIME);
Integer timeSecond = Integer.parseInt(aucend.substring(17, 19));
Integer timeMinute = Integer.parseInt(aucend.substring(14, 16));
Integer timeHour = Integer.parseInt(aucend.substring(11, 13));
Integer timeDay = Integer.parseInt(aucend.substring(0, 2));
Integer timeMonth = Integer.parseInt(aucend.substring(3, 5)) - 1;
Integer timeYear = Integer.parseInt(aucend.substring(6, 10));
Time future = new Time();
future.set(timeSecond, timeMinute, timeHour, timeDay, timeMonth, timeYear);
future.normalize(true);
Long futureMillis = future.toMillis(true);
Long interval = futureMillis - nowMillis;
periods.add(interval);
Durations.add(holder.duration);
total = total + 1 ;
我正在尝试将holder.duration
插入ArrayList<TextView> Durations
,因此我可以使用一个countdowntimer对此进行管理
Durations.add(holder.duration);
这导致空指针异常。 同样的事情也发生在arraylist期间。 为什么会这样呢?
用于存储它们的arraylist在外部初始化
public ArrayList<TextView> Durations;
public ArrayList<Long> periods;
当我没有初始化arrayList时,这发生在我身上。 尝试这个...
public ArrayList<TextView> Durations = new ArrayList<TextView>();
public ArrayList<Long> periods = new ArrayList<Long>();
您必须先在某处初始化ArrayList实例,然后才能向其实际添加值
public ArrayList<TextView> Durations = new ArrayList<TextView>();
public ArrayList<Long> periods = new ArrayList<Long>();
直接从文档中
当抛出NullPointerException时?
当应用程序在需要对象的情况下尝试使用null时抛出。 这些包括:
改成:
public ArrayList<TextView> Durations= new ArrayList<TextView>() ;
public ArrayList<Long> periods=new ArrayList<Long>();
即使在那之后,我也不认为这是个好方法,因为在每次显示视图时都会调用getView
并且每次视图被ListView
重用时, ListView
重用它的Views,因此相同的TextView
引用将用于多个Items。
您可以编辑问题并告诉我们以下点赞的用途:
periods.add(interval);
Durations.add(holder.duration);
total = total + 1 ;
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.