获取共享首选项值时无法将 ClassCastException java.lang.Integer 转换为 java.lang.Long

ClassCastException java.lang.Integer cannot be cast to java.lang.Long on getting the Shared Preference value

我得到

java.lang.ClassCastException: java.lang.Integer cannot be cast to java.lang.Long

如果值不存在于首选项中,获取共享首选项默认值的异常。 下面是我的代码。

public long getLongSharedValue(String key, long def) {
        SharedPreferences settings = context.getSharedPreferences(
                context.getApplicationInfo().packageName + "_prefs", Context.MODE_PRIVATE);
        return settings.getLong(key, def); // Error is logged here...
    }

谁能帮我解决这个问题。 我曾尝试转换强制类型转换以及如下所示:

return settings.getLong(key, Long.valueOf(def));

但问题依旧。

错误日志如下:

java.lang.ClassCastException: java.lang.Integer cannot be cast to java.lang.Long
        at android.app.SharedPreferencesImpl.getLong(SharedPreferencesImpl.java:249)
        at package-name.UtilityClass.getLongSharedValue(UtilityClass.java:104)
        at package-name.Fragment1.getPullData(Fragment1.java:130)
        at package-name.Fragment1.onCreateView(Fragment1.java:107)
        at androidx.fragment.app.Fragment.performCreateView(Fragment.java:2600)
        at androidx.fragment.app.FragmentManagerImpl.moveToState(FragmentManagerImpl.java:881)
        at androidx.fragment.app.FragmentManagerImpl.moveFragmentToExpectedState(FragmentManagerImpl.java:1238)
        at androidx.fragment.app.FragmentManagerImpl.moveToState(FragmentManagerImpl.java:1303)
        at androidx.fragment.app.BackStackRecord.executeOps(BackStackRecord.java:439)
        at androidx.fragment.app.FragmentManagerImpl.executeOps(FragmentManagerImpl.java:2079)
        at androidx.fragment.app.FragmentManagerImpl.executeOpsTogether(FragmentManagerImpl.java:1869)
        at androidx.fragment.app.FragmentManagerImpl.removeRedundantOperationsAndExecute(FragmentManagerImpl.java:1824)
        at androidx.fragment.app.FragmentManagerImpl.execPendingActions(FragmentManagerImpl.java:1727)
        at androidx.fragment.app.FragmentManagerImpl.run(FragmentManagerImpl.java:150)
        at android.os.Handler.handleCallback(Handler.java:751)
        at android.os.Handler.dispatchMessage(Handler.java:95)
        at android.os.Looper.loop(Looper.java:154)
        at android.app.ActivityThread.main(ActivityThread.java:6119)
        at java.lang.reflect.Method.invoke(Native Method)
        at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:886)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:776)

我在我的应用程序中使用的方法是:

    long id = mUtility.getLongSharedValue(Constants.SharedPreferencesKeys.USER_ID, 0);
//long id = mUtility.getLongSharedValue(Constants.SharedPreferencesKeys.USER_ID, 0L); // Tried this as well but same issue is perishing...

当您将值传递给此 getLongSharedValue(String, long) 时,请确保您传递的第二个参数值是长数据类型值。

因为 Long.valueOf() 主要接受两种参数类型,字符串或长。

因为它对你不起作用似乎传递给 getLongSharedValue(String, long) 第二个参数的值不是长值。

SharedPreferencesImpl 的代码片段:

public long getLong(String key, long defValue) {
    synchronized (mLock) {
        awaitLoadedLocked();
        Long v = (Long)mMap.get(key); 
        return v != null ? v : defValue;
    }
}

所以大概地图上有一个整数。

如果你需要长期尝试

try {
    return settings.getLong(key, def);
}
catch (ClassCastException e) {
    int val = settings.getInt(key,0);
    return (val == 0) ? def : Long.valueOf(val);
}

编辑:如果地图包含 0,这将失败,将返回 def,在您的情况下没问题,但是...