简体   繁体   中英

Parse Android SDK: ClassCastException when LocalDatastore is activated and List of Subclass Message is loaded from the Server

Background: I am experimenting with a Messaging App

Issue Description: Normally Codesnipped 2 works like charm and loads data from the Server. But when activating the LocalDatastore with Codesnipped 1, the error

java.lang.ClassCastException: com.parse.ParseObject cannot be cast to at.test.activity.Message

arrises. Message is a Subclass of ParseObject.

When looking with the Debugger at the line where the error occures, one can see that the list that is handed over to the done function contains only one object of the Type "Message" in the first postition and all other elements have the type "ParseObject" (-> Screenshot: DebuggerOutput when local datastore is activated ) . Without activated local datastore every element of this list is Type "Message".

What am I doing wrong? Thanks in advance!

Codesnipped 1:

Parse.initialize(new Parse.Configuration.Builder(this)
.applicationId("test")
.enableLocalDataStore()
.clientBuilder(builder)
.server("http://someinstallation.com/parse/")
.build());

Codesnipped 2:

ParseQuery query = ParseQuery.getQuery(Message.class);
ArrayList idsToQuery = new ArrayList<>();
idsToQuery.add(getSenderID());
idsToQuery.add(getRecipiantID());
query.whereContainedIn("sender", idsToQuery);
query.whereContainedIn("recipiant", idsToQuery);
query.orderByDescending("createdAt");
query.findInBackground(new FindCallback() {
    public void done(List messages, ParseException e) {
        if (e == null) {
           addMessagesTomList(messages);
        }
        else {
           Log.e("message", "Error Loading Messages" + e);
        }
    }
}); `

I think you forgot to do some steps before started to use your Parse subclasses so in order to create a sub class (Message in your case) you need to do the following:

  1. Create a new class which extends from ParseObject class
  2. Add the properties that you want this subclass to have via setters and getters
  3. Annotate the new class with @ParseClassName("Message")

At the end your subclass should look like the following:

import com.parse.ParseClassName;
import com.parse.ParseObject;

@ParseClassName("Message")
public class CheckIn extends ParseObject {

    public String getContent() {
        return getString("content");
    }

    public void setContent(String content) {
        put("content", content);
    }

    public String getTitle() {
        return getString("title");
    }

    public void setTitle(String title) {
        put("title", title);
    }

}

Finally go to where you init Parse SDK (usually in your application file) and register the new subclass that you created:

ParseObject.registerSubclass(Message.class);

If you will go through the steps above it will work for you.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM