0

我有一个月视图日历应用程序,其中有突出显示的事件。我想使用 ListView 以列表格式显示所有事件。

使用本教程,我设法实现了适配器。但是,我无法遍历月份来检查哪些日期是假期并将它们显示在列表视图中。

代码:

片段.java

public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {

    //initialize calendar 
    .....

   //loop through the month to get the events
    for(int i = 0; i < _calendar.getActualMaximum(Calendar.DAY_OF_MONTH); i++){
            if(isHoliday(day, month, year))
            {
                Log.d("Calendar", "Events");
                CalendarEvents events = new CalendarEvents();
                final ArrayList<Event> e = new ArrayList<Event>();
                e.addAll(events.eventDetails(month, day));

                for (int j = 0; j < e.size(); j++)
                {
                   Event event = e.get(i);
                   summary_data = new Summary[]
                    {
                       new Summary("EVENT 1", "Event")
                    };
                } 
            }
        }


       //this code works:
        /*summary_data = new Summary[]
        {
             new Summary("Cloudy", "Cloudy"),
             new Summary("Showers", "Showers"),
             new Summary("Snow", "Snow"),
             new Summary("Storm", "Storm"),
             new Summary("Sunny", "Sunny")
        };*/

        summaryAdapter = new SummaryAdapter(this.getActivity().getApplicationContext(), R.layout.listview_item_row, summary_data);

        calendarSummary = (ListView) v.findViewById(R.id.calendarSummary);
        calendarSummary.setAdapter(summaryAdapter);
}

摘要Adapter.java:

@Override
public View getView(int position, View convertView, ViewGroup parent) {
    View row = convertView;
    SummaryHolder holder = null;

    if(row == null)
    {

        row = mInflater.inflate(layoutResourceId, parent, false);

        holder = new SummaryHolder();
        holder.summaryDate = (TextView)row.findViewById(R.id.summaryDate);
        holder.summaryDetail = (TextView)row.findViewById(R.id.summaryDetail);

        row.setTag(holder);
    }
    else
    {
        holder = (SummaryHolder)row.getTag();
    }

    Summary summary = data[position];
    holder.summaryDate.setText(summary.summaryDate); //throws NullPointerException
    holder.summaryDetail.setText(summary.summaryDetail);

    return row;
}

holder.summaryDate.setText(summary.summaryDate);它抛出NullPointerException

我哪里错了?

4

2 回答 2

1

Did you debug it? Try to add a break point and double check if all of your variables have been initialized, for what I see I would go straight to the variable "data", check if it has values.

于 2012-09-17T13:26:42.263 回答
0

将验证设置为

 if(data != null) {
    Summary summary = data[position];
 }

在列表视图构建期间可能会引发这些错误。

于 2012-09-17T13:21:54.893 回答