Android 比较自定义适配器类中的日期(空指针异常)?
我有一个自定义适配器。我尝试比较当前日期和其中输入的日期。 但ı得到了空指针异常。我不明白为什么会出现这种异常。 我的LogCat在下面,出现问题的行是“((ViewGroup)todoView).getChildAt(i).setBackgroundColor(0x01060018); “有人能帮我吗 我的适配器类Android 比较自定义适配器类中的日期(空指针异常)?,android,android-custom-view,custom-adapter,android-date,Android,Android Custom View,Custom Adapter,Android Date,我有一个自定义适配器。我尝试比较当前日期和其中输入的日期。 但ı得到了空指针异常。我不明白为什么会出现这种异常。 我的LogCat在下面,出现问题的行是“((ViewGroup)todoView).getChildAt(i).setBackgroundColor(0x01060018); “有人能帮我吗 我的适配器类 public class ToDoItemAdapter extends ArrayAdapter<ToDoItem> { private int reso
public class ToDoItemAdapter extends ArrayAdapter<ToDoItem> {
private int resource;
Context context;
ArrayList<ToDoItem> todoItem;
public ToDoItemAdapter(Context context, int resource, ArrayList<ToDoItem> objects) {
super(context, resource, objects); // TODO Auto-generated constructor stub
this.resource = resource;
this.context = context;
this.todoItem = objects;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
// TODO Auto-generated method stub
LinearLayout todoView;
ToDoItem todo = getItem(position);
String priorityString = todo.getPriority();
String taskString = todo.getTask();
String dateString = todo.getDeadline();
String statusString = todo.getStatus();
if (convertView == null)
{
todoView = new LinearLayout(getContext());
String inflater = Context.LAYOUT_INFLATER_SERVICE;
LayoutInflater li;
li = (LayoutInflater)getContext().getSystemService(inflater);
li.inflate(resource, todoView, true);
}
else
{
todoView = (LinearLayout) convertView;
int len = todoItem.size();
for(int i = 0; i < len; i++)
{
String deadline = todoItem.get(i).getDeadline();
String status = todoItem.get(i).getStatus();
String currentDate = findCurrentDate();
SimpleDateFormat sdf = new SimpleDateFormat("dd-MM-yyyy");
java.util.Date data=null;
try {
data = sdf.parse(deadline);
} catch (ParseException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (java.text.ParseException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
java.util.Date current =null;
try {
current = sdf.parse(currentDate);
} catch (ParseException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (java.text.ParseException e) {
// TODO Auto-generated catch block
e.printStackTrace();
System.out.println(data);
}
if(status.equals("DONE"))
{
((ViewGroup) todoView).getChildAt(i).setBackgroundColor(0xff00ff00);
System.out.println(data);
}
else
{
System.out.println(data);
if(data.compareTo(current)>0 || data.compareTo(current)==0){
((ViewGroup) todoView).getChildAt(i).setBackgroundColor(0x01060018);
}else if(data.compareTo(current)<0){
((ViewGroup) todoView).getChildAt(i).setBackgroundColor(0x01060016);
}
}
}
}
TextView priorityView = (TextView) todoView.findViewById(R.id.itemPriority);
TextView taskView = (TextView) todoView.findViewById(R.id.itemTask);
TextView dateView = (TextView) todoView.findViewById(R.id.itemDeadline);
TextView statusView = (TextView) todoView.findViewById(R.id.itemStatus);
priorityView.setText(priorityString);
taskView.setText(taskString);
dateView.setText(dateString);
statusView.setText(statusString);
return todoView;
}
private String findCurrentDate() {
// TODO Auto-generated method stub
Calendar cal = Calendar.getInstance();
int year = cal.get(Calendar.YEAR);
int month = cal.get(Calendar.MONTH);
int day = cal.get(Calendar.DAY_OF_MONTH);
String date = day+"-"+(month+1)+"-"+year;
return date;
}
}
输出(针对两个不同项目的日期)
看起来您尚未正确初始化(convertView==null)案例的新布局 是吗
li.inflate(resource, todoView, true);
请注意,“充气”的结果是充气视图。中间的参数将成为父级-通常这是某种ViewGroup或ListView。在任何情况下,它都是getView()的“父”参数
然后,稍后,您将尝试查找todoView的子视图。但是它是空的,因此这些调用将返回null。后续引用将出错
以下是一种简单的充气方法:
LayoutInflater inflater = context.getLayoutInflater();
todoView = (LinearLayout) inflater.inflate (resource, parent, false);
要么数据为空,要么当前为空,我们不能为您做太多,找出原因!如果
数据
或当前
的日期解析失败,则只打印堆栈跟踪,在这种情况下,可以获得NullPointerException。检查deadline
和currentDate
值以查看有何错误。确实如此。打印并向我们显示:截止日期、当前日期、数据(初始化后)和当前值。不过,到那时,您应该能够找到是哪一个导致了问题……我编辑了我的问题,以获取输出是哪一行导致了故障?我想猜猜看下面我的答案。我不认为问题出在哪里
li.inflate(resource, todoView, true);
LayoutInflater inflater = context.getLayoutInflater();
todoView = (LinearLayout) inflater.inflate (resource, parent, false);