Android应用程序在for循环中使用解析崩溃了

时间:2015-05-04 09:36:33

标签: android parse-platform

我从解析数据收到时遇到问题。

日志说问题就在这个循环中:

q.findInBackground(new FindCallback<ParseObject>() {

    @Override
    public void done(List<ParseObject> parseObjects, ParseException e) {

        for (int i = 0; i < parseObjects.size(); i++) {

            Comment c = new Comment();
            ParseObject o = parseObjects.get(i);
            c.created = o.getCreatedAt();
            c.name = o.getString(Comment.NAME);
            c.text = o.getString(Comment.TEXT);
            c.email = o.getString(Comment.EMAIL);
            c.rating = o.getDouble(Comment.RATING);
            c.isActive = true;
            c.place = o.getString(Comment.PLACE);
            p.comments.add(c);
        }
        Comment.increaseDownloaded();
    }
});

当我收到少于40个物体时,它可以正常工作。但如果我收到更多,我会收到此错误:

05-04 11:25:20.755    1473-1473/com.myapp E/AndroidRuntime﹕ FATAL EXCEPTION: main
Process: com.myapp, PID: 1473
java.lang.NullPointerException: Attempt to invoke interface method 'int java.util.List.size()' on a null object reference
        at com.myapp.place.PlaceActivity$2$3.done(PlaceActivity.java:362)
        at com.parse.FindCallback.internalDone(FindCallback.java:45)
        at com.parse.FindCallback.internalDone(FindCallback.java:31)
        at com.parse.Parse$4$1.run(Parse.java:792)
        at android.os.Handler.handleCallback(Handler.java:739)
        at android.os.Handler.dispatchMessage(Handler.java:95)
        at android.os.Looper.loop(Looper.java:135)
        at android.app.ActivityThread.main(ActivityThread.java:5312)
        at java.lang.reflect.Method.invoke(Native Method)
        at java.lang.reflect.Method.invoke(Method.java:372)
        at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:901)

任何人都知道这是什么问题?

非常感谢你!

1 个答案:

答案 0 :(得分:0)

检查parseObjects == null,因为我相信您的parseObjects为空。

qp.findInBackground(new FindCallback<ParseObject>() {
                    @Override
                    public void done(List<ParseObject> parseObjects, ParseException e) {
                        if (e != null)
                            e.printStackTrace();
                      if(parseObjects!=null)
                         {
                        for (int i = 0; i < parseObjects.size(); i++) {
                            Photo pp = new Photo();
                            ParseObject o = parseObjects.get(i);
                            pp.isActive = true;
                            pp.place = o.getString(Photo.PLACE);
                            ParseFile f = o.getParseFile(Photo.PHOTO);
                            pp.url = f.getUrl();
                            p.photos.add(pp);
                          }
                        Photo.increaseDownloaded();
                        }
                        else 
                        { // Toast here  
                        }
                    }
                });