数字格式异常 日历的长无效



我遇到了这个 NumberFormatException 无效长的问题。log cat 显示错误来自方法的Long.parseLong部分。

  public static String getDateTimeStr(String p_time_in_millis) {
    SimpleDateFormat sdf = new SimpleDateFormat(DATE_TIME_FORMAT);
    Date l_time = new Date(Long.parseLong(p_time_in_millis));
    return sdf.format(l_time);
}

有人可以告诉我为什么当我在某些日历中获取和显示数据时,此代码可以正常工作,然后在设备上的其他日历中,请问我得到这个NumberFormatException Invalid Long

编辑:这是代码的其余部分...

private void getEvents() {
    Uri l_eventUri;
    ArrayList<Map<String, String>> allStudents = new ArrayList<Map<String, String>>();
    if (Build.VERSION.SDK_INT >= 8) {
        l_eventUri = Uri.parse("content://com.android.calendar/events");
    } else {
        l_eventUri = Uri.parse("content://calendar/events");
    }
    String[] l_projection = new String[]{"title", "dtstart", "dtend"};
    @SuppressWarnings("deprecation")
    Cursor l_managedCursor = this.managedQuery(l_eventUri, l_projection, "calendar_id=" + m_selectedCalendarId, null, "dtstart DESC, dtend DESC");
    if (l_managedCursor.moveToFirst()) {

        int l_colTitle = l_managedCursor.getColumnIndex(l_projection[0]);
        int l_colBegin = l_managedCursor.getColumnIndex(l_projection[1]);
        int l_colEnd = l_managedCursor.getColumnIndex(l_projection[2]);
        String l_title = String.valueOf(l_colTitle);
        String l_begin = Integer.toString(l_colBegin);
        String l_end = Integer.toString(l_colEnd);
        do {
            Map<String, String> map = new HashMap<String, String>();    
            l_title = l_managedCursor.getString(l_colTitle);
            l_begin = getDateTimeStr(l_managedCursor.getString(l_colBegin));
            l_end = getDateTimeStr(l_managedCursor.getString(l_colEnd));
            map.put("eventTitles", l_title);
            map.put("event_begin", l_begin);
            map.put("event_end", l_end);
            allStudents.add(map);

        } while (l_managedCursor.moveToNext());
        l_managedCursor.close();
        SimpleAdapter adapter = new SimpleAdapter(this, allStudents, R.layout.notice_layout, new String[] { "eventTitles", "event_begin", "event_end" }, new int[] { R.id.tvTitle, R.id.tvBody, R.id.tvTeacherCode}); 
        listViewCalendar.setAdapter(adapter);
    }

}

编辑 2:

出于某种原因,没有这行代码的代码可以正常工作,所以我把它钉在这一行代码上。

l_end = getDateTimeStr(l_managedCursor.getString(l_colEnd));

为什么l_colEnd会陷入NumberFormatExcetion?当以下代码行也可以被卷入同一NumberFormatException时,因为它正在查询相同的 int 格式?

l_begin = getDateTimeStr(l_managedCursor.getString(l_colBegin));

也感谢所有帮助过的人。另一个有趣的事情也是当我添加这个

int l_cnt = 0;
do {
   ++l_cnt;
} while (l_managedCursor.moveToNext() && l_cnt < 100);

while 子句,如下面在以下代码末尾所示,该应用程序运行良好,没有代码行抛出NumberFormatException

if (l_managedCursor.moveToFirst()) {
        int l_cnt = 0;
        int l_colTitle = l_managedCursor.getColumnIndex(l_projection[0]);
        int l_colBegin = l_managedCursor.getColumnIndex(l_projection[1]);
        int l_colEnd = l_managedCursor.getColumnIndex(l_projection[2]);
        String l_title = String.valueOf(l_colTitle);
        String l_begin = Integer.toString(l_colBegin);
        String l_end = Integer.toString(l_colEnd);
        do {
            Map<String, String> map = new HashMap<String, String>();    
            l_title = l_managedCursor.getString(l_colTitle);
            l_begin = getDateTimeStr(l_managedCursor.getString(l_colBegin));
            l_end = getDateTimeStr(l_managedCursor.getString(l_colEnd));
            map.put("eventTitles", l_title);
            map.put("event_begin", l_begin);
            map.put("event_end", l_end);
            allStudents.add(map);
            ++l_cnt;
        } while (l_managedCursor.moveToNext() && l_cnt < 100);

有人可以告诉我为什么当我获取和显示时这段代码工作正常吗 某些日历中的数据,然后在我设备上的其他日历中的数据 I 请问得到这个数字格式异常无效长?

Date

l_time = new Date(Long.parseLong(p_time_in_millis((;

国际 海事 组织。此代码是"坏"代码。

为什么?您尝试从未经检查的来源获取未知数据

"content://com.android.calendar/events""content://calendar/events"

几乎每个人都可以访问日历并保存他喜欢的任何内容......可悲的是,那里没有使用它的规则!因此,疯狂猜测应用程序正在使用此事件列以您期望的另一种格式保存数据。

关于检查l_cnt <100,其中失败停止

它不会失败,因为错误发生在 101 事件或更高版本!


我的

解决方案是检查我的数据,并且永远不要相信其他应用程序会按照我的预期或应有的方式运行。

所以我建议更改getDateTimeStr方法如下:

   public static String getDateTimeStr(String p_time_in_millis) {
      SimpleDateFormat sdf = new SimpleDateFormat(DATE_TIME_FORMAT);
      long timestamp = 0;
      try {
         timestamp = Long.parseLong(p_time_in_millis)
      } catch(NumberFormatException e) {
         Log.w("getDateTimeStr", "Cannot convert '"+p_time_in_millis+"' to long");
         e.printStackTrace(); // Prints full error exception
      }
      Date l_time = new Date(timestamp);
      return sdf.format(l_time);
   }

删除l_cnt < 100检查并留下代码以运行检查您的日志猫!您现在将更好地了解正在发生的事情,并且您的错误数据日期将是 1/1/1970(由于 0 时间戳(代码可以响应更改,以便处理没有预期格式的日期。

处理错误的一些想法:

  • getDateTimeStr向可能捕获它的getEvents()抛出异常,并忽略包含意外数据的事件。(处理逻辑,忽略我无法理解的东西。
  • 识别每个不同情况下p_time_in_millis的格式,并对每个事件的确切格式使用不同的DATE_TIME_FORMAT类型。好吧,这需要大量调查,但仍然可能失败。此外,您还必须添加格式仍然未知的情况,因此可以忽略它或使用默认值以免崩溃。

通常,您总是尝试编写一个稳定的应用程序,如果另一个应用程序以不同的格式保存数据(因为它喜欢或由于它自己的错误(,该应用程序不会失败

最新更新