我正在使用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
感谢
答案 0 :(得分:0)
所以发生的事情是你正在执行从新线程开始的saveInBackground。然后,您将立即使用null objectId调用query.getInBackground。您需要等待并在gameScore.saveInBackground中完成调用之后调用query.getInBackground。
我建议在上面的gamerScore.saveInBackground执行中将第二次调用移到done函数调用。此外,您必须考虑保存可能会失败,因此您需要捕获异常,如下所示:
public void done(List<ParseObject> items, ParseException e) {
if (e == null) {
//execute query here
} else {
//handle exception here
}
}