我正在尝试在我的应用程序中动态创建 ExpandableListView。父行显示接下来 7 天的日期,我从 Web 服务中检索子项。但是,获取和展示孩子不是我的问题。我在动态显示父行时遇到了麻烦。我需要在父行中显示接下来 7 天的日期。
我在全局范围内声明了这个空的字符串数组。
private String[] parent = new String[7];
然后,在 onCreate() 中,我使用此代码创建父数组内容。
Calendar cal = Calendar.getInstance();
String strdate = null;
SimpleDateFormat sdf = new SimpleDateFormat("dd-MM-yyyy");
int i;
for(i=0;i<7;i++)
{
if (cal != null) {
strdate = sdf.format(cal.getTime());
}
parent[i] = strdate;
cal.add(Calendar.HOUR_OF_DAY, 24);
}
但是,该活动在 MyExpandableListAdapter.getGroupView() 处给出了 NullPointerException。
(也许是因为父数组最初没有初始化?)
因为当我最初将父数组声明为时代码有效
private String[] parent = new String[] {"","","".....};