【问题标题】:com.parse.ParseObject cannot be cast tocom.parse.ParseObject 不能强制转换为
【发布时间】:2019-06-09 04:22:14
【问题描述】:

我正在开发具有Parse Platform 的应用程序。为了获取数据,我调用了ParseCloud.callFunctionInBackground 函数。

我已将 Parse 及其子类注册到 Application 类中,如下所示:

public class App extends Application {
    @Override
    public void onCreate(){
        super.onCreate();
        Parse.setLogLevel(Parse.LOG_LEVEL_VERBOSE);    
        OkHttpClient.Builder builder = new OkHttpClient.Builder();
        HttpLoggingInterceptor httpLoggingInterceptor = new HttpLoggingInterceptor();
        httpLoggingInterceptor.setLevel(HttpLoggingInterceptor.Level.BODY);
        builder.networkInterceptors().add(httpLoggingInterceptor);    
        ParseObject.registerSubclass(ParseMessage.class);
        Parse.initialize(new Parse.Configuration.Builder(this)
                        .applicationId("KEY")
                        .server("URL")
                        .build());
    }
}

我有以下扩展 ParseObject 的模型类:

@ParseClassName("ParseMessage")
public class ParseMessage extends ParseObject {

    // Ensure that your subclass has a public default constructor
    public ParseMessage() {
        super();
    }

    public ParsePhoto getPhotos() {
        return (ParsePhoto) getParseObject("photos");
    }

    public void setPhotos(ParsePhoto value) {
        put("photos", value);
    }

    public String getCaption() {
        return getString("caption");
    }

    public void setCaption(String value) {
        put("caption", value);
    }

}

当我从我的 Fragment 调用以下方法时:

HashMap<String, Object> params = new HashMap<String, Object>();
ParseCloud.callFunctionInBackground("MY_METHOD", params, new FunctionCallback<ArrayList<ParseMessage>>() {
            public void done(ArrayList<ParseMessage> mapObject, ParseException e) {
                if (e == null) {
                    ParseMessage object = mapObject.get(i);
                    }
                } else {
                }
    }
});

但我遇到了异常:

java.lang.ClassCastException: com.parse.ParseObject 无法转换为 com.example.ParseMessage

我已经从 Google 和 Stackoverflow 搜索了很多东西,但我没有得到任何解决方案。任何人都可以帮助我解决这个问题,因为我已经花了很多时间在这上面。下面是我从 Parse 得到的回复:

【问题讨论】:

  • 你能解决这个问题吗?

标签: java android parse-platform parse-server parse-android-sdk


【解决方案1】:

您提供的信息不是很具体,但从调试器屏幕看来,您正在尝试将ParsePhoto 转换为ParseMessageParsePhotoParseObject 的子类,我相信这是导致问题的原因。

【讨论】:

  • 我不确定哪些附加信息仍由我自行决定,但根据您的评论让我快速回顾一下,ParseMessage 是具有 ParsePhoto、Caption 和其他参数的父类。所以,这里 ParsePhoto 是一个子类。
  • 从您提供的截图看来,您正在获取 ParseObject 类型的对象,但您正试图存储 ParseMessage 类型的引用,它是 ParseObject 的子类。
  • 是的@RipudamanSingh,但是我可以直接从解析平台获得基于 ParseMessage 的响应吗?
  • @RonakJoshi,我对你的措辞有点困惑“ParseMessage 是具有 ParsePhoto 的父类,...,所以,这里 ParsePhoto 是一个子类”。如果ParsePhotoParseMessage 的实例字段,则它不会成为ParseMessagechild。你能发布ParsePhotoParseMessage的完整定义吗?
猜你喜欢
  • 2015-09-03
  • 2017-06-02
  • 2013-07-21
  • 2016-09-26
  • 1970-01-01
  • 2013-03-20
  • 2020-12-06
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多