App crashes on googleApiClient.connect();

I have an app, and I want to add Google Play achievements. I followed this:

https://developers.google.com/games/services/android/init

I have this at my manifest (with the right ID):

<meta-data android:name="com.google.android.gms.appstate.APP_ID"
    android:value="000000000000" />
<meta-data android:name="com.google.android.gms.games.APP_ID"
    android:value="000000000000" />

I have this OnStart:

    @Override
protected void onStart() {
    try
    {
        super.onStart();
        googleApiClient.connect();
    }catch (Exception e)
    {
        Exception error;
        error = e;
    }
}

With debuging, when the ".connect()" is executed, it crashes, and "TRY CATCH" doesn´t detect it. This is my "OnCreate()".

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

   googleApiClient =  new GoogleApiClient.Builder(this)
            .addConnectionCallbacks(this)
            .addOnConnectionFailedListener(this)
            .addApi(Games.API).addScope(Games.SCOPE_GAMES)
            // add other APIs and scopes here as needed
            .build();
    getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);
    setContentView(R.layout.activity_main);

    lanzarFragments();
}

This is how my "MainActivity" is declared:

public class MainActivity extends AppCompatActivity implements
    GoogleApiClient.ConnectionCallbacks,
    GoogleApiClient.OnConnectionFailedListener

My GoogleApiClient, is declared like I show here:

public static  GoogleApiClient googleApiClient;

I want to add, that the method "lanzarFragments()", starts a Fragment. All my app is with fragments, changing one by another. But I just have one Activity, the Main, that has the "OnCreate()" I wrote up.

Some Idea about what it crashes and how to fix it? Thank you.

Upvotes: 0

Views: 325

Answers (1)

pnewby060
pnewby060

Reputation: 26

Try this in onStart():

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

in onStop();

@Override
protected void onStop() {
    super.onStop();
    mGoogleApiClient.disconnect();
}

This is from Official Docs. You don't need a try / catch for this method hence why the exception is not getting called.

Could you post your logcat also please to see in more detail as without it others wont be able to find out the exact problem.

Upvotes: 1

Related Questions