Chris Chris - 5 months ago 56
Java Question

java.lang.ClassCastException: java.util.HashSet cannot be cast to Custom Class

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 (where the exception occurs)

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();
}





How can this happen? There shouldn't be any problems when casting
null
to
ChatNotificationHashSet<String>
, right? I couldn't think of any other problem.

Answer

The Set that is returned by getStringSet() does not need to be the same as you pass to SharedPreferences.Editor.putStringSet() method. It's generally up to internal implementation. I assume it serializes the data and puts it into some storage and unserializes when retrieving back.

If you need your specific structure, it's probably better to implement some wrapper which would either read all settings in the constructor or retrieve the settings on demand from underlying Set.