【问题标题】:I can't recover a Calendar using getSerializableExtra of an Intent我无法使用 Intent 的 getSerializableExtra 恢复日历
【发布时间】:2012-05-02 04:36:45
【问题描述】:

我把Extras放在活动A中:

notificationIntent.putExtra(KEY_ID, id);
notificationIntent.putExtra(KEY_CALENDAR, day);
A.logCalendar("calendar: ", day); // the Calendar is not null at this point

I getExtras en Activity B:

int id = intent.getIntExtra(Constantes.KEY_ID, 0);
A.b("My id: " + id); // The id is recovery
Calendar time = (Calendar) intent.getSerializableExtra(Constantes.KEY_CALENDAR);
if (time == null)
    A.b("null calendar"); // The calendar is always null, WHY?

我做错了什么。提前致谢。

【问题讨论】:

  • 你为什么要这样做?只需在每个活动中创建一个日历。 Calendar 只是日期和时间计算的助手。如果您想传递一个时间点,请使用java.util.Date 类或毫秒作为long
  • 有时我们离树太近了以至于看不到森林。谢谢。我将使用 long 和毫秒。

标签: android android-intent serializable


【解决方案1】:

您不能序列化整个 Calendar 对象并使用 Extra 发送它。您只能发送原语、数组和字符串。您可能需要考虑 Parcellable 等替代方案。

【讨论】:

    猜你喜欢
    • 2015-07-27
    • 1970-01-01
    • 1970-01-01
    • 2017-08-13
    • 2017-07-21
    • 2021-04-25
    • 2020-11-26
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多