【发布时间】:2015-03-23 14:47:12
【问题描述】:
我无法将高分提交到 android 排行榜。现在排行榜是空的,没有提交任何内容。我有一个需要提交的 int "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);
}
}
Logcat:
原因:java.lang.NullPointerException:尝试调用接口 方法'无效 com.google.android.gms.common.api.GoogleApiClient.connect()' 为空 对象引用
【问题讨论】:
标签: java android android-studio nullpointerexception