Parse.com:JSONObject无法强制转换为ParseFile

时间:2014-07-16 08:20:04

标签: android parse-platform picasso

我试图从Parse.com检索图像。在DataBrowser中,如果图像文件为空,则会在代码中崩溃。所以我通过检查文件!= null来处理这个错误。

它在ParseFile file = (ParseFile) ob.get("image");这一行JSONObject cannot be cast to ParseFile处崩溃。

那么如果Parse文件为空,如何处理??

for (ParseObject ob : result) {
String perf = ob.getString("info");
ParseFile file = (ParseFile) ob.get("image");
if (file != null) {
    image_url = file.getUrl();
    } else {
         /*load some default image url*/
        image_url = "www.abc.com/image.png";
    }
    Picasso.with(getActivity()).load(image_url).into(imageView);
    textView.setText(perf);
    layoutCards.setVisibility(View.VISIBLE);
}

2 个答案:

答案 0 :(得分:3)

尝试:

(确保在解析数据库中有一个名为“image”的字段存储parsefile)

ParseFile parseFile = ob.getParseFile("image");

然后检查:

if (parseFile != null && parseFile.getUrl() != null && parseFile.getUrl().length() > 0) {
            ...
}

答案 1 :(得分:0)

如何捕获NullPointerException如下:

for(ParseObject ob : results) {
    try {
        ParseFile imageFile = (ParseFile) ob.get("image");

        imageFile.getDataInBackground(new GetDataCallback() {
            public void done(byte[] data, ParseException e) {
                if (e == null) {
                    // data has the bytes for the image
                    Log.d(TAG,"get image");
                } else {
                    // something went wrong
                }
            }
       });
    } catch (NullPointerException e) {
        Log.d(TAG,"got no image");
    }
}