gpt4 book ai didi

java.lang.ClassCastException : java. util.HashSet 无法转换为自定义类

转载 作者:行者123 更新时间:2023-11-30 06:53:05 24 4
gpt4 key购买 nike

FATAL EXCEPTION: AsyncTask #2
Process: my.app, PID: 15441
java.lang.ClassCastException: java.util.HashSet cannot be cast to my.app.ChatNotificationHashSet
at my.app.UserHandler.getChatNotificationMessage(UserHandler.java:127)
at my.app.NotificationUtil.getChatTitle(NotificationUtil.java:100)
at my.app.NotificationUtil.showChat(NotificationUtil.java:71)
at my.app.service.GCMListenerService.onMessageReceived(GCMListenerService.java:47)
at com.google.android.gms.gcm.GcmListenerService.zzq(Unknown Source)
at com.google.android.gms.gcm.GcmListenerService.zzp(Unknown Source)
at com.google.android.gms.gcm.GcmListenerService.zzo(Unknown Source)
at com.google.android.gms.gcm.GcmListenerService.zza(Unknown Source)
at com.google.android.gms.gcm.GcmListenerService$1.run(Unknown Source)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1112)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:587)
at java.lang.Thread.run(Thread.java:818)

ChatNotificationHashSet

public class ChatNotificationHashSet<E> extends LinkedHashSet<String> {

@Override
public String toString() {
// ...
}
}

UserHandler(发生异常的地方)

public ChatNotificationHashSet<String> getChatNotificationMessage() {
return (ChatNotificationHashSet<String>) pref.getStringSet(CHAT_NOTIFICATION_MESSAGE, null); // <- Exception occurs here
}

public void setChatNotificationMessage(ChatNotificationHashSet<String> messages) {
SharedPreferences.Editor editor = pref.edit();
editor.putStringSet(CHAT_NOTIFICATION_MESSAGE, messages);
editor.commit();
}

怎么会发生这种事?类型转换时应该不会有任何问题nullChatNotificationHashSet<String> , 正确的?我想不出还有什么问题。

最佳答案

getStringSet() 返回的 Set 不需要与传递给 SharedPreferences.Editor.putStringSet() 的相同> 方法。这通常取决于内部实现。我假设它会序列化数据并将其放入某些存储中,并在检索时反序列化。

如果您需要特定的结构,最好实现一些包装器,它可以读取构造函数中的所有设置,或者根据需要从底层 Set 检索设置。

关于java.lang.ClassCastException : java. util.HashSet 无法转换为自定义类,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37916845/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com