我有一个月视图日历应用程序,其中我有高亮度的事件。我想使用ListView以列表格式显示所有事件。
使用this tutorial我设法实现了适配器。但是,我无法循环整月,检查哪些日期是假日,并在列表视图中显示。
代码:
Fragment.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);
}
SummaryAdapter.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
。
我哪里错了?
答案 0 :(得分:1)
你调试了吗?尝试添加断点并仔细检查是否所有变量都已初始化,因为我看到我会直接转到变量“data”,检查它是否有值。
答案 1 :(得分:0)
将验证设置为
if(data != null) {
Summary summary = data[position];
}
在列表视图构建期间可能会抛出这些错误。