我在onCreate方法中执行此操作:
Parse.enableLocalDatastore(this);
Parse.initialize(this);
ParseObject testObject = new ParseObject("TestObject");
testObject.put("Becky", "is dumb");
testObject.put("SammyHuang", "is smart");
testObject.saveInBackground();
然后我有一个按钮就可以了:
ParseQuery<ParseObject> query = ParseQuery.getQuery("SammyHuang");
query.getInBackground("2wTGHJ230q", new GetCallback<ParseObject>() {
public void done(ParseObject object, ParseException e) {
if (e == null) {
// object will be your game score
becky = object.getString("Becky");
sammy = object.getString("SammyHuang");
System.out.println(sammy);
}
else {
System.out.println("bbbbbbbbbbbbbbbbbbbbbbbbruh");
// something went wrong
}
}
});
我从解析网站复制对象ID“2wTGHJ230q”并尝试将其显示在终端但加班我运行它打印“bruh”,我无法弄清楚出了什么问题。 thx in advanced。
编辑: 我将getQuery(“SammyHuang”)修复为getQuery(“TestObject”),它仍然打印“bruh”。
重新编辑: ++++++++++++++++++++++++++++++++++++++++++++++++++ ++++++++++++++++++
public class MainActivity extends AppCompatActivity {
private String sammy;
private String becky;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// [Optional] Power your app with Local Datastore. For more info, go to
// https://parse.com/docs/android/guide#local-datastore
Parse.enableLocalDatastore(this);
Parse.initialize(this);
setContentView(R.layout.activity_main);
ParseObject testObject = new ParseObject("TestObject");
testObject.put("Becky", "is dumb");
testObject.put("SammyHuang", "is smart");
testObject.saveInBackground();
}
public void helloFunc(View view){
final TextView lol = (TextView)findViewById(R.id.moron);
ParseQuery<ParseObject> query = ParseQuery.getQuery("TestObject");
query.getInBackground("u2VE9tIAwA", new GetCallback<ParseObject>() {
public void done(ParseObject object, ParseException e) {
if (e == null) {
becky = new String(object.getString("Becky"));
sammy = new String(object.getString("SammyHuang"));
} else {
System.out.println("bbbbbbbbbbbbbbbbbbbbbbbbruh");
e.printStackTrace();
System.out.println("bbbbbbbbbbbbbbbbbbbbbbbbruh");
// something went wrong
}
}
});
lol.setText("becky " + becky + "\n" + "sammy " + sammy);
}
}
我不知道为什么lol.setText会给我null。似乎在方法getinbackground中分配变量“becky”和“sammy”不起作用。而且我也很困惑这个方法如何有一个参数,该参数是另一个在其中定义的方法的对象。
答案 0 :(得分:0)
您将对象保存为TestObject
(执行new ParseObject("TestObject")
时),因此您还需要通过执行ParseQuery.getQuery("TestObject")
将其检索为一个。
我认为您最好查看saving和retrieving对象上的Parse文档。
如果再次遇到错误,请务必检查ParseError的消息,因为它通常包含大量可以帮助您找到问题的信息。