【问题标题】:cannot be cast to java.lang.Long Android SharedPreferences无法转换为 java.lang.Long Android SharedPreferences
【发布时间】:2017-12-07 05:51:37
【问题描述】:

全新安装我的 APP 后,出现以下异常:

java.lang.RuntimeException: Unable to start activity ... 
java.lang.ClassCastException: java.lang.Integer cannot be cast to 
java.lang.Long

(活动)代码:

初始化时间长(onCreate 之外):

long spinTimer = 0L;

从 pefs- 分配上一个 long val 或默认为 0L(在 onCreate 内):

spinTimer = settings.getLong("spinTimer", 0L);

应该是long,int是从哪里来的?

sharepref 获取其值的其他代码参考(全长):

if (item.equals("Select...")) {editor.putLong("spinTimer", 0L);}
if (item.equals("5 mins")) {editor.putLong("spinTimer", System.currentTimeMillis() + (5 * 60 * 1000));}
if (item.equals("10 mins")) {editor.putLong("spinTimer", System.currentTimeMillis() + (10 * 60 * 1000));}
if (item.equals("15 mins")) {editor.putLong("spinTimer", System.currentTimeMillis() + (15 * 60 * 1000));}
if (item.equals("30 mins")) {editor.putLong("spinTimer", System.currentTimeMillis() + (30 * 60 * 1000));}
if (item.equals("1 hour")) {editor.putLong("spinTimer", System.currentTimeMillis() + (60 * 60 * 1000));}
editor.commit();
spinTimer = settings.getLong("spinTimer", 0L);

清除APP数据后,一切正常。每次我全新安装 APP 时,我都不应该这样做。

一个int怎么会偷偷溜进去?

请求的调试信息 - 不是标准的,但认为这应该足够了:

发生崩溃的相关代码行(简单的Activity):

log.debug("***Activity Start***");
log.debug("OnCreateEnter");
SharedPreferences settings = 
PreferenceManager.getDefaultSharedPreferences(getApplicationContext());
SharedPreferences.Editor editor = settings.edit();
log.debug("spinTimer: {}", spinTimer);
spinTimer = settings.getLong("spinTimer", 0L); // <-- APP crashes right here

输出:

***Activity Start***
OnCreateEnter
spinTimer: 0

1) 如果没有分配任何内容,.getLong (0L) 的第二个参数是否应该分配给变量 (spinTimer)?我也试过'0'。

2) 如何分配 int?键 'spinTimer' (0L) 和 var 'spinTimer' ('long spinTimer = 0L;') 都被初始化为 long。

3) 如果我清除 APP 数据(全新安装后)- 一切正常;

4)如果我在上述语句之前添加以下代码行 - 一切顺利(但这种方法扭曲了我的逻辑):

editor.putLong("spinTimer", spinTimer);
editor.commit();

【问题讨论】:

    标签: android casting sharedpreferences


    【解决方案1】:
    java.lang.ClassCastException: java.lang.Integer cannot be cast to 
    java.lang.Long
    

    由于ClassCastException问题,你应该使用Long.valueOf

    返回一个 Long 实例,表示指定的 long 值。如果一个 new Long 实例不是必须的,这个方法一般应该是 优先于构造函数 Long(long) 使用,因为此方法是 可能会产生显着更好的空间和时间性能 缓存经常请求的值。请注意,与 Integer类中对应的方法,这个方法不是必须的 缓存特定范围内的值。

    editor.putLong("spinTimer", Long.valueOf(System.currentTimeMillis() + (5 * 60 * 1000))
    

    【讨论】:

    • 好收获。完美的答案和有用的细节。
    • @IntelliJ:很好/谢谢!昨晚我坠毁后,我做了一个梦,今天早上第一件事就是尝试这个 - 认为没有其他可能的解释。感谢您的快速、非常明确的回复。支持,但稍后会尝试/验证。如果我不回来 - 一切都很好(肯定会)。
    • Studio 抱怨:'Long.valueOf(System.currentTimeMillis() + (60 * 60 * 1000)));'.Ignore?很好奇,如果 System.currentTimeMillis 已经被投射了很长时间(以及其他所有内容) - 为什么需要它?
    • 在谷歌上搜索Unnecessary boxing of Long.valueOf
    • @IntelliJ 谢谢会做。即使在添加代码后,卸载 APP/重新安装 - APP 崩溃并出现同样的错误。不确定如何在代码中的任何位置强制转换 int。
    猜你喜欢
    • 2021-09-12
    • 2013-12-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-07-27
    • 2014-04-24
    • 2018-11-07
    相关资源
    最近更新 更多