我有一个应用程序,我已经发布并集成了Android排行榜,但是当我与另一个用户玩游戏时(用户未在playstore仪表板中添加为测试人员),当我尝试提交分数时,应用程序崩溃了。 该应用程序已经发布,我知道它应该工作!?!有人能告诉我这个问题会是什么吗?
这是我的代码:
private void submitScore(final int points) {
if (getApiClient().isConnected()) {
Log.d("--", "submiting score " + points);
PendingResult<Leaderboards.LoadPlayerScoreResult> result = Games.Leaderboards
.loadCurrentPlayerLeaderboardScore(getApiClient(),
getString(R.string.poeni_tabela),
LeaderboardVariant.TIME_SPAN_ALL_TIME,
LeaderboardVariant.COLLECTION_PUBLIC);
result.setResultCallback(new ResultCallback<Leaderboards.LoadPlayerScoreResult>() {
@Override
public void onResult(
Leaderboards.LoadPlayerScoreResult loadPlayerScoreResult) {
Games.Leaderboards.submitScore(getApiClient(),
getString(R.string.poeni_tabela),
loadPlayerScoreResult.getScore().getRawScore()
+ points);
}
});
}
}
这是日志:
java.lang.NullPointerException
at com.myapp.QuizActivity$9.onResult(QuizActivity.java:611)
at com.myapp.QuizActivity$9.onResult(QuizActivity.java:1)
at com.google.android.gms.common.api.BaseImplementation$CallbackHandler.deliverResultCallback(Unknown Source)
at com.google.android.gms.common.api.BaseImplementation$CallbackHandler.handleMessage(Unknown Source)
at android.os.Handler.dispatchMessage(Handler.java:106)
at android.os.Looper.loop(Looper.java:136)
at android.app.ActivityThread.main(ActivityThread.java:5196)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:515)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:786)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:602)
at dalvik.system.NativeStart.main(Native Method)
第com.myapp.QuizActivity$9.onResult(QuizActivity.java:611)
行是此代码:
Games.Leaderboards.submitScore(getApiClient(),
getString(R.string.poeni_tabela),
loadPlayerScoreResult.getScore().getRawScore()
+ points);
答案 0 :(得分:2)
还有更多可能性:
Games.Leaderboards is null
loadPlayerScoreResult is null
loadPlayerScoreResult.getScore() is null
loadPlayerScoreResult.getScore().getRawScore() is null
使用调试器找出发生的事情。
答案 1 :(得分:2)
好的,感谢@Jens,我找到了问题的答案。好像loadPlayerScoreResult.getScore().getRawScore()
似乎是null
。我想这是因为玩家还没有提交任何分数,所以在这种情况下你只需要添加初始分数,下次如果不是null
- 它不应该是,你可以加总旧分数与新的。
这是我现在在submitScore
方法中使用的代码:
if (loadPlayerScoreResult.getScore() != null) {
Games.Leaderboards.submitScore(getApiClient(),
getString(R.string.poeni_tabela),
loadPlayerScoreResult.getScore().getRawScore()
+ points);
} else {
Games.Leaderboards.submitScore(getApiClient(),
getString(R.string.poeni_tabela), points);
}