我有一个适用于一个列表视图的适配器,包含这些
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
,因此我可以使用一个倒计时器来管理它,使用此
Durations.add(holder.duration);
这会导致空指针异常。同样的事情也发生在arraylist时期。为什么会这样?
用于存储的arraylist在
之外初始化public ArrayList<TextView> Durations;
public ArrayList<Long> periods;
答案 0 :(得分:1)
当我没有初始化arrayList时,这发生在我身上。 试试这个...
public ArrayList<TextView> Durations = new ArrayList<TextView>();
public ArrayList<Long> periods = new ArrayList<Long>();
答案 1 :(得分:0)
您必须先在某处初始化ArrayList实例,然后才能像这样实际添加值
public ArrayList<TextView> Durations = new ArrayList<TextView>();
public ArrayList<Long> periods = new ArrayList<Long>();
直接来自文档,
何时抛出NullPointerException?
当应用程序在需要对象的情况下尝试使用null时抛出。其中包括:
答案 2 :(得分:0)
更改为:
public ArrayList<TextView> Durations= new ArrayList<TextView>() ;
public ArrayList<Long> periods=new ArrayList<Long>();
即使在那之后,我认为这不是一个好方法,因为每次显示视图时都会调用getView
并且ListView
重新使用它的视图,因此TextView
引用将被用于多个项目。
您可以编辑您的问题并告诉我们以下内容的用途:
periods.add(interval);
Durations.add(holder.duration);
total = total + 1 ;