我正在创建活动日历。我是这个例外java.lang.NumberFormatException: Invalid long: “null”
Utility.java
public class Utility {
public static ArrayList<String> nameOfEvent = new ArrayList<String>();
public static ArrayList<String> startDates = new ArrayList<String>();
public static ArrayList<String> endDates = new ArrayList<String>();
public static ArrayList<String> descriptions = new ArrayList<String>();
public static ArrayList<String> readCalendarEvent(Context context) {
Cursor cursor = context.getContentResolver()
.query(Uri.parse("content://com.android.calendar/events"),
new String[] { "calendar_id", "title", "description",
"dtstart", "dtend", "eventLocation" }, null,
null, null);
cursor.moveToFirst();
// fetching calendars name
String CNames[] = new String[cursor.getCount()];
// fetching calendars id
nameOfEvent.clear();
startDates.clear();
endDates.clear();
descriptions.clear();
for (int i = 0; i < CNames.length; i++) {
nameOfEvent.add(cursor.getString(1));
startDates.add(getDate(Long.parseLong(cursor.getString(3))));
endDates.add(getDate(Long.parseLong(cursor.getString(4))));
descriptions.add(cursor.getString(2));
CNames[i] = cursor.getString(1);
cursor.moveToNext();
}
return nameOfEvent;
}
public static String getDate(long milliSeconds) {
SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd");
Calendar calendar = Calendar.getInstance();
calendar.setTimeInMillis(milliSeconds);
return formatter.format(calendar.getTime());
}
}
logcat的
E/AndroidRuntime(2990): java.lang.NumberFormatException: Invalid long: "null"
E/AndroidRuntime(2990): at java.lang.Long.invalidLong(Long.java:125)
E/AndroidRuntime(2990): at java.lang.Long.parseLong(Long.java:342)
E/AndroidRuntime(2990): at java.lang.Long.parseLong(Long.java:319)
E/AndroidRuntime(2990): at com.examples.android.calendar.Utility.readCalendarEvent(Utility.java:36)
E/AndroidRuntime(2990): at com.examples.android.calendar.CalendarView$1.run(CalendarView.java:184)
为什么我这样做?任何人帮助我。非常感谢提前..
答案 0 :(得分:1)
我相信您的以下陈述会产生错误,
startDates.add(getDate(Long.parseLong(cursor.getString(3))));
endDates.add(getDate(Long.parseLong(cursor.getString(4))));
只需将其更改为以下内容,然后重试。
startDates.add(getDate(Long.parseLong(cursor.getString(3).trim())));
endDates.add(getDate(Long.parseLong(cursor.getString(4).trim())));
您的字段可能包含一个空格,您需要在将其转换为长数据类型之前将其删除。
答案 1 :(得分:0)
使用
startDates.add(getDate(cursor.getLong(3)));
endDates.add(getDate(cursor.getLong(4)));
startDates.add(getDate(Long.parseLong(cursor.getString(3))));
endDates.add(getDate(Long.parseLong(cursor.getString(4))));