0

我正在使用 parse.com 的服务,android 指南:

https://www.parse.com/docs/android_guide

它对我不起作用(我收到一条消息:应用程序必须关闭......)

这是我的代码:

gameScore = new ParseObject("GameScore");
gameScore.put("score", 1337);
gameScore.put("playerName", "Sean Plott");
gameScore.put("cheatMode", false);
try {
    gameScore.save();
} catch (ParseException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
}
gameScore.saveInBackground(new SaveCallback() {
      public void done(ParseException e) {
        //  Access the object id here
          objectId= gameScore.getObjectId();
      }
    });

ParseQuery query = new ParseQuery("GameScore");

query.getInBackground(objectId, new GetCallback() {

@Override
public void done(ParseObject parseObject, ParseException arg1) {
    // TODO Auto-generated method stub
    int score= parseObject.getInt("score");
    Integer i =new Integer(score);
    t1.setText(i.toString());
}
});

这是Logcat:

02-28 09:59:10.552: E/AndroidRuntime(773): FATAL EXCEPTION: main
02-28 09:59:10.552: E/AndroidRuntime(773): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.salebook/com.example.salebook.MainActivity}: java.lang.NullPointerException
02-28 09:59:10.552: E/AndroidRuntime(773):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2180)
02-28 09:59:10.552: E/AndroidRuntime(773):  at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2230)
02-28 09:59:10.552: E/AndroidRuntime(773):  at android.app.ActivityThread.access$600(ActivityThread.java:141)
02-28 09:59:10.552: E/AndroidRuntime(773):  at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1234)
02-28 09:59:10.552: E/AndroidRuntime(773):  at android.os.Handler.dispatchMessage(Handler.java:99)
02-28 09:59:10.552: E/AndroidRuntime(773):  at android.os.Looper.loop(Looper.java:137)
02-28 09:59:10.552: E/AndroidRuntime(773):  at android.app.ActivityThread.main(ActivityThread.java:5041)
02-28 09:59:10.552: E/AndroidRuntime(773):  at java.lang.reflect.Method.invokeNative(Native Method)
02-28 09:59:10.552: E/AndroidRuntime(773):  at java.lang.reflect.Method.invoke(Method.java:511)
02-28 09:59:10.552: E/AndroidRuntime(773):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
02-28 09:59:10.552: E/AndroidRuntime(773):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
02-28 09:59:10.552: E/AndroidRuntime(773):  at dalvik.system.NativeStart.main(Native Method)
02-28 09:59:10.552: E/AndroidRuntime(773): Caused by: java.lang.NullPointerException
02-28 09:59:10.552: E/AndroidRuntime(773):  at com.example.salebook.MainActivity.onCreate(MainActivity.java:69)
02-28 09:59:10.552: E/AndroidRuntime(773):  at android.app.Activity.performCreate(Activity.java:5104)
02-28 09:59:10.552: E/AndroidRuntime(773):  at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1080)
02-28 09:59:10.552: E/AndroidRuntime(773):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2144)
02-28 09:59:10.552: E/AndroidRuntime(773):  ... 11 more

此外,当我处于调试模式时,我看到 objectId 为 null ,尽管我可以在服务器中看到该对象成功上传并出现问题并且它具有有效的 objectId。

此外,当我在 eclipse 中查看 gameScore 对象时,我可以看到它具有与服务器中相同的 objectId,但是它再次在 objectId 中返回 null

谢谢

4

1 回答 1

0

所以发生的事情是你正在执行从一个新线程开始的 saveInBackground 。然后,您将立即使用 null objectId 调用 query.getInBackground。您需要在 gameScore.saveInBackground 中的 done 调用之后等待并调用 query.getInBackground。

我建议将第二次调用移动到上面的 gamerScore.saveInBackground 执行中的 done 函数调用。此外,您必须考虑保存可能会失败,因此您需要按如下方式捕获异常:

public void done(List<ParseObject> items, ParseException e) {
    if (e == null) {
       //execute query here
    } else {
       //handle exception here
    }

}

于 2013-02-28T10:43:17.397 回答