问题
I am packing an intent and one of the Extras I add is a date object, like this:
intent.putExtra(DATE_EXTRA, t.getDate());
Later, when I reading the extras, I try to get the Date like this:
this.date = new Date(intent.getExtras().getString(DATE_EXTRA));
However, this returns an error about the String being empty. I don't think the above is the right way to do it, because I am not looking for a string, but I have not been able to find an intent.getDateExtra() method anywhere. What do I do?
I know that the date was passed properly, because I can see it while debugging:
回答1:
Replace:
this.date = new Date(intent.getExtras().getString(DATE_EXTRA));
with:
this.date = (Date)intent.getSerializableExtra(DATE_EXTRA);
and see if that helps.
回答2:
Another workaround would be to pass the long value of the date:
intent.putExtra(DATE_EXTRA, t.getDate().getTime());
....
this.date = new Date(intent.getLongExtra(DATE_EXTRA, 0)); //0 is the default value
来源:https://stackoverflow.com/questions/27217280/how-to-get-date-extra-from-an-intent