我在向Android排行榜提交高分时遇到了问题。目前排行榜是空的,没有任何内容被提交。我有一个需要提交的整数"oldScore"。目前我正在尝试一段代码进行提交,但调用时活动会崩溃。我的代码:
public class GameOver extends BaseGameActivity implements GoogleApiClient.ConnectionCallbacks, GoogleApiClient.OnConnectionFailedListener {
private GoogleApiClient mGoogleApiClient;
public static int score;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_game_over);
mGoogleApiClient.connect();
mGoogleApiClient = new GoogleApiClient.Builder(this)
.addApi(Drive.API)
.addScope(Drive.SCOPE_FILE)
.addConnectionCallbacks(this)
.addOnConnectionFailedListener(this)
.build();
int newScore = GameOver.score;
SharedPreferences prefs = this.getSharedPreferences("myPrefsKey", Context.MODE_PRIVATE);
int oldScore = prefs.getInt("key", 0);
if (newScore > oldScore) {
SharedPreferences.Editor edit = prefs.edit();
edit.putInt("key", newScore);
edit.commit();
EditText HighScore = (EditText) findViewById(R.id.HighScoreT);
HighScore.setText("" + newScore);
} else {
EditText HighScore = (EditText) findViewById(R.id.HighScoreT);
HighScore.setText("" + oldScore);
Games.Leaderboards.submitScoreImmediate(getApiClient(),String.valueOf(R.string.number_guesses_leaderboard), oldScore);
}
}
日志记录:
造成的原因:java.lang.NullPointerException: 尝试在空对象引用上调用接口 方法'void com.google.android.gms.common.api.GoogleApiClient.connect()'