Android java.lang.Long 不能转换为 java.lang.String [英] Android java.lang.Long cannot be cast to java.lang.String
问题描述
好的,所以我使用
e.putLong("startTime", SystemClock.uptimeMillis());
我从分享偏好中得到它
SHARED_PREFS = context.getSharedPreferences(LOCAL_PREF, MODE_PRIVATE);
long startTime = SHARED_PREFS.getLong("startTime", 0L); <-- Stack trace points to this line.
但我在某些设备上遇到此错误并且不知道为什么,我根本没有尝试将其强制转换为 String
.
but I am getting this error on some devices and not sure why, I am not trying to cast it to a String
at all.
java.lang.Long cannot be cast to java.lang.String
我发现的一切都指向试图将其设置为 String
值,我根本不想这样做,我试图直接使用 long
值.
Everything I find points towards trying to set this as a String
value and I am not trying to do that at all, I am trying to use the long
value directly.
** 更新堆栈跟踪 **
** UPDATE STACK TRACE **
Caused by: java.lang.ClassCastException: java.lang.Long cannot be cast to java.lang.String
at android.app.SharedPreferencesImpl.getString(SharedPreferencesImpl.java:255)
at com.tech.utility.Tools.refreshPrefs(Tools.java:193)
at com.tech.activity.Menu_dashboard.onStart(Menu_dashboard.java:762)
at android.app.Instrumentation.callActivityOnStart(Instrumentation.java:1340)
at android.app.Activity.performStart(Activity.java:7191)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2920)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3032)
at android.app.ActivityThread.-wrap11(Unknown)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1696)
at android.os.Handler.dispatchMessage(Handler.java:105)
at android.os.Looper.loop(Looper.java:164)
at android.app.ActivityThread.main(ActivityThread.java:6940)
at java.lang.reflect.Method.invoke(Method.java:-2)
at com.android.internal.os.Zygote$MethodAndArgsCaller.run(Zygote.java:327)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1374)
推荐答案
如果您要 阅读文档 你会读到 getLong
方法将:
If you were to read the docs you would read that the getLong
method will:
如果此名称不长的首选项,则抛出 ClassCastException.
Throws ClassCastException if there is a preference with this name that is not a long.
因此,在应用程序的其他地方,您必须调用:
So, somewhere else in the app, you must have called:
e.putLong("startTime", "someString");
这篇关于Android java.lang.Long 不能转换为 java.lang.String的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!