gpt4 book ai didi

Android Listview - 空指针异常

转载 作者:行者123 更新时间:2023-11-30 02:43:51 25 4
gpt4 key购买 nike

当我尝试在 ListView 上显示项目时,它显示了 NullPointerException。在此我认为 position 或 convertview 和 TextView id 和 LinearLayout id 都正确。以下是我的代码中出现异常的行。

viewItem.ll.setBackgroundColor(rainbow[position]);
viewItem.time.setText(timeValuesList.get(position).getElapsetime().replace(" minutes", ""));

这是我的代码:

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

}

最佳答案

如果 convertView 不为空,则从标记中检索 ViewItem。但是你以前从未设置过这个标签。

要修复它,请在 if (convertView == null) block 的末尾添加此行:

convertView.setTag(viewItem);

关于Android Listview - 空指针异常,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25357916/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com