繁体   English   中英

数组列表 <TextView> 无法设定

[英]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时抛出。 这些包括:

  • 调用空对象的实例方法。
  • 访问或修改空对象的字段。 将null的长度视为数组。
  • 访问或修改null插槽,就好像它是一个数组一样。
  • 将null抛出,就好像它是一个Throwable值一样。

改成:

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.

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