[英]Integer cannot be cast to Long
我正在跟蹤我的 sqlite 數據庫的共享首選項中的行 ID。 我使用的是 int,但后來我意識到可能 long 會更適合這個,因為 id 值可能會隨着時間的推移變得很大。 當我嘗試從共享首選項 getLong 時出現此錯誤: java.lang.ClassCastException: java.lang.Integer cannot be cast to java.lang.Long
sharedPreferences = PreferenceManager.getDefaultSharedPreferences(this);
editor = sharedPreferences.edit();
workoutId = sharedPreferences.getLong("workoutId", 0);
我的鍛煉 ID 初始化代碼是一個全局變量private long workoutId;
我不確定它從錯誤消息中獲取整數值的位置。 或者也許 int 會很好?
問題在於您的默認值是整數。
你應該使用
workoutId = sharedPreferences.getLong("workoutId", 0L);
或者
workoutId = sharedPreferences.getLong("workoutId", (long) 0);
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.