堆栈在Android应用程序中集成解析云代码

时间:2015-10-10 07:33:47

标签: android android-activity android-studio parse-platform

这是main.js:

Parse.Cloud.define("averageStats", function(request, response) {
  var Rating = Parse.Object.extend("StatObject");
  var query = new Parse.Query("Rating");
  query.equalTo("Location", request.params.location);
  query.find({
    success: function(results) {
      var sum = 0;
      for (var i = 0; i < results.length; ++i) {
        sum += results[i].get("Rating");
      }
      response.success(sum / results.length);
    },
    error: function() {
      response.error("Location lookup failed");
    }
  });
});

在mainactivity.java中:

Location = (EditText) findViewById(R.id.fldLocation);
String loc = Location.getText().toString();
HashMap<String, Object> params = new HashMap<String, Object>();
params.put("location", loc);
ParseCloud.callFunctionInBackground("averageStats", params, new FunctionCallback <Float> () {
    @Override
    public void done(Float curentRating, com.parse.ParseException e) {
        if (e==null){
            //return curentRating;
            Rating = curentRating;
        }
        else{
            Rating = 0.0f;
        }
    }
});
Stats = (EditText) findViewById(R.id.fldStats);
Stats.setText(String.valueOf(Rating));

程序崩溃:Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'java.io.File com.parse.ParsePlugins.getParseDir()' on a null object reference

指针转到行:ParseCloud.callFunctionInBackground("averageStats", params, new FunctionCallback <Float> ()

请帮助理解错误。

1 个答案:

答案 0 :(得分:0)

解决了!!!

刚刚添加

    Parse.enableLocalDatastore(this);
    Parse.initialize(this,xxxxxx,xxxxxxxxxx);

调用Cloud方法之前