-1

我有一个自定义适配器。我尝试比较当前日期和其中输入的日期。但是我得到了空指针异常。我不知道为什么是这个异常的原因。我的 LogCat 在下面,问题所在是“((ViewGroup) todoView).getChildAt(i).setBackgroundColor(0x01060018);”有人可以帮我吗?

我的适配器类;

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

日志猫

11-16 17:32:12.357: E/AndroidRuntime(3148): FATAL EXCEPTION: main
11-16 17:32:12.357: E/AndroidRuntime(3148): java.lang.NullPointerException
11-16 17:32:12.357: E/AndroidRuntime(3148):     at com.example.enhancedtodolistapplication.ToDoItemAdapter.getView(ToDoItemAdapter.java:107)
11-16 17:32:12.357: E/AndroidRuntime(3148):     at android.widget.AbsListView.obtainView(AbsListView.java:2161)
11-16 17:32:12.357: E/AndroidRuntime(3148):     at android.widget.ListView.measureHeightOfChildren(ListView.java:1247)
11-16 17:32:12.357: E/AndroidRuntime(3148):     at android.widget.ListView.onMeasure(ListView.java:1159)
11-16 17:32:12.357: E/AndroidRuntime(3148):     at android.view.View.measure(View.java:15848)
11-16 17:32:12.357: E/AndroidRuntime(3148):     at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5008)
11-16 17:32:12.357: E/AndroidRuntime(3148):     at android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:1404)
11-16 17:32:12.357: E/AndroidRuntime(3148):     at android.widget.LinearLayout.measureVertical(LinearLayout.java:695)
11-16 17:32:12.357: E/AndroidRuntime(3148):     at android.widget.LinearLayout.onMeasure(LinearLayout.java:588)
11-16 17:32:12.357: E/AndroidRuntime(3148):     at android.view.View.measure(View.java:15848)
11-16 17:32:12.357: E/AndroidRuntime(3148):     at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5008)
11-16 17:32:12.357: E/AndroidRuntime(3148):     at android.widget.FrameLayout.onMeasure(FrameLayout.java:310)
11-16 17:32:12.357: E/AndroidRuntime(3148):     at android.view.View.measure(View.java:15848)
11-16 17:32:12.357: E/AndroidRuntime(3148):     at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5008)
11-16 17:32:12.357: E/AndroidRuntime(3148):     at com.android.internal.widget.ActionBarOverlayLayout.onMeasure(ActionBarOverlayLayout.java:302)
11-16 17:32:12.357: E/AndroidRuntime(3148):     at android.view.View.measure(View.java:15848)
11-16 17:32:12.357: E/AndroidRuntime(3148):     at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5008)
11-16 17:32:12.357: E/AndroidRuntime(3148):     at android.widget.FrameLayout.onMeasure(FrameLayout.java:310)
11-16 17:32:12.357: E/AndroidRuntime(3148):     at com.android.internal.policy.impl.PhoneWindow$DecorView.onMeasure(PhoneWindow.java:2189)
11-16 17:32:12.357: E/AndroidRuntime(3148):     at android.view.View.measure(View.java:15848)
11-16 17:32:12.357: E/AndroidRuntime(3148):     at android.view.ViewRootImpl.performMeasure(ViewRootImpl.java:1905)
11-16 17:32:12.357: E/AndroidRuntime(3148):     at android.view.ViewRootImpl.measureHierarchy(ViewRootImpl.java:1104)
11-16 17:32:12.357: E/AndroidRuntime(3148):     at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:1284)
11-16 17:32:12.357: E/AndroidRuntime(3148):     at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:1004)
11-16 17:32:12.357: E/AndroidRuntime(3148):     at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:5481)
11-16 17:32:12.357: E/AndroidRuntime(3148):     at android.view.Choreographer$CallbackRecord.run(Choreographer.java:749)
11-16 17:32:12.357: E/AndroidRuntime(3148):     at android.view.Choreographer.doCallbacks(Choreographer.java:562)
11-16 17:32:12.357: E/AndroidRuntime(3148):     at android.view.Choreographer.doFrame(Choreographer.java:532)
11-16 17:32:12.357: E/AndroidRuntime(3148):     at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:735)
11-16 17:32:12.357: E/AndroidRuntime(3148):     at android.os.Handler.handleCallback(Handler.java:730)
11-16 17:32:12.357: E/AndroidRuntime(3148):     at android.os.Handler.dispatchMessage(Handler.java:92)
11-16 17:32:12.357: E/AndroidRuntime(3148):     at android.os.Looper.loop(Looper.java:137)
11-16 17:32:12.357: E/AndroidRuntime(3148):     at android.app.ActivityThread.main(ActivityThread.java:5103)
11-16 17:32:12.357: E/AndroidRuntime(3148):     at java.lang.reflect.Method.invokeNative(Native Method)
11-16 17:32:12.357: E/AndroidRuntime(3148):     at java.lang.reflect.Method.invoke(Method.java:525)
11-16 17:32:12.357: E/AndroidRuntime(3148):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:737)
11-16 17:32:12.357: E/AndroidRuntime(3148):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
11-16 17:32:12.357: E/AndroidRuntime(3148):     at dalvik.system.NativeStart.main(Native Method)

输出(针对两个不同项目的日期)

11-16 18:44:30.687: I/System.out(4087): variable deadline 15-10-2012
11-16 18:44:30.697: I/System.out(4087): variable currentDate 16-11-2013
11-16 18:44:30.717: I/System.out(4087): variable data Mon Oct 15 00:00:00 EDT 2012
11-16 18:44:30.727: I/System.out(4087): variable current Sat Nov 16 00:00:00 EST 2013
11-16 18:44:30.757: I/System.out(4087): variable deadline 17-12-2014
11-16 18:44:30.757: I/System.out(4087): variable currentDate 16-11-2013
11-16 18:44:30.767: I/System.out(4087): variable data Wed Dec 17 00:00:00 EST 2014
11-16 18:44:30.777: I/System.out(4087): variable current Sat Nov 16 00:00:00 EST 2013
4

1 回答 1

0

看起来您还没有为 (convertView == null) 案例正确初始化新布局。

你做

 li.inflate(resource, todoView, true); 

请意识到“膨胀”的结果是膨胀的视图。中间参数将成为父级 - 通常这是某种 ViewGroup 或 ListView。在任何情况下,它都是 getView() 的“父”参数。

然后,稍后,您尝试查找 todoView 的子视图。但是它是空的,所以这些调用将返回 null。后续引用会出错。

这是一种更简单的充气方法:

 LayoutInflater inflater = context.getLayoutInflater();
 todoView = (LinearLayout) inflater.inflate (resource, parent, false);
于 2013-11-17T01:29:36.917 回答