4

I am having trouble submitting the highscores to android leaderboards. Right now the leaderboards are empty and nothing is being submitted. I have an int "oldScore" that needs to be submitted. Right now I am trying a piece of code to submit it but the activity crashes when called. My code:

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:

Caused by: java.lang.NullPointerException: Attempt to invoke interface method 'void com.google.android.gms.common.api.GoogleApiClient.connect()' on a null object reference

Anubhav Gupta
  • 2,492
  • 14
  • 27
l7ivine
  • 81
  • 1
  • 1
  • 9

4 Answers4

11

The statement

mGoogleApiClient.connect();

should appear after mGoogleApiClient has been instantiated

Reimeus
  • 158,255
  • 15
  • 216
  • 276
  • When I do this I then get this error : "java.lang.RuntimeException: Unable to start activity ComponentInfo: java.lang.IllegalStateException: GoogleApiClient is not connected yet." – l7ivine Jan 24 '15 at 19:05
9

You are invoking the method

connect()

on the object

mGoogleApiClient

without instantiating it

You need to write this first,

  mGoogleApiClient = new GoogleApiClient.Builder(this)
                .addApi(Drive.API)
                .addScope(Drive.SCOPE_FILE)
                .addConnectionCallbacks(this)
                .addOnConnectionFailedListener(this)
                .build();  

then this,

mGoogleApiClient.connect();
ANAND SONI
  • 625
  • 9
  • 13
Sanjeet A
  • 5,171
  • 3
  • 23
  • 40
  • When I do this I then get this error : "java.lang.RuntimeException: Unable to start activity ComponentInfo: java.lang.IllegalStateException: GoogleApiClient is not connected yet." – l7ivine Jan 24 '15 at 19:06
  • You are using the google login, You should call the method from onStart() – Sanjeet A Jan 24 '15 at 19:16
  • I just created methods onStart() and onStop() and put mGoogleApiClient.connect(); in OnStart yet i am getting the same error. – l7ivine Jan 24 '15 at 19:28
  • when i delete "Games.Leaderboards.submitScoreImmediate(getApiClient(), String.valueOf(R.string.number_guesses_leaderboard), oldScore);" my activity works fine.. so this is where my error is coming from. How should i change this to submit the score? – l7ivine Jan 24 '15 at 19:39
2

You can't call connect() until after you initialize the mGoogleApiClient reference. Like,

// mGoogleApiClient.connect();
mGoogleApiClient = new GoogleApiClient.Builder(this)
                .addApi(Drive.API)
                .addScope(Drive.SCOPE_FILE)
                .addConnectionCallbacks(this)
                .addOnConnectionFailedListener(this)
                .build();       
mGoogleApiClient.connect(); // <-- move to here.
Elliott Frisch
  • 198,278
  • 20
  • 158
  • 249
  • When I do this I then get this error : "java.lang.RuntimeException: Unable to start activity ComponentInfo: java.lang.IllegalStateException: GoogleApiClient is not connected yet." – l7ivine Jan 24 '15 at 19:06
  • 1
    Okay. That sounds like we've fixed your `NullPointerException`. Have you done any research at all on your [new question](http://stackoverflow.com/questions/21831224/googleapiclient-is-not-connected-yet-exception-in-cast-application)? – Elliott Frisch Jan 24 '15 at 19:09
1

Try this process : Assign google client before loading contentview:

private GoogleApiClient mGoogleApiClient;
public static int score;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    mGoogleApiClient = new GoogleApiClient.Builder(this)
    .addApi(Drive.API)
    .addScope(Drive.SCOPE_FILE)
    .addConnectionCallbacks(this)
    .addOnConnectionFailedListener(this)
    .build();

    setContentView(R.layout.activity_game_over);
}

Add override onStart and connect the google client.

@Override
     public void onStart() {
        super.onStart();
        mGoogleApiClient.connect();
    }

@Override
public void onStop() {
    super.onStop();
    mGoogleApiClient.disconnect();
}
Santhosh
  • 89
  • 1
  • 3