Reputation: 997
https://developers.google.com/+/mobile/android/getting-started This is my code:
public class MainActivity extends Activity implements ConnectionCallbacks, OnConnectionFailedListener{
private static final int RC_SIGN_IN = 0;
/* Client used to interact with Google APIs. */
private GoogleApiClient mGoogleApiClient;
/* A flag indicating that a PendingIntent is in progress and prevents
* us from starting further intents.
*/
private boolean mIntentInProgress;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
mGoogleApiClient = new GoogleApiClient.Builder(this).addConnectionCallbacks(this).addOnConnectionFailedListener(this).addApi(Plus.API, null).addScope(Plus.SCOPE_PLUS_LOGIN).build();
setContentView(R.layout.activity_main);
}
protected void onStart() {
super.onStart();
mGoogleApiClient.connect();
}
protected void onStop() {
super.onStop();
if (mGoogleApiClient.isConnected()) {
mGoogleApiClient.disconnect();
}
}
public void onConnectionFailed(ConnectionResult result) {
if (!mIntentInProgress && result.hasResolution()) {
try {
mIntentInProgress = true;
result.startResolutionForResult(this, RC_SIGN_IN);
} catch (SendIntentException e) {
// The intent was canceled before it was sent. Return to the default
// state and attempt to connect to get an updated ConnectionResult.
mIntentInProgress = false;
mGoogleApiClient.connect();
}
}
}
public void onConnected(Bundle connectionHint) {
// We've resolved any connection errors. mGoogleApiClient can be used to
// access Google APIs on behalf of the user.
}
protected void onActivityResult(int requestCode, int responseCode, Intent intent) {
if (requestCode == RC_SIGN_IN) {
mIntentInProgress = false;
if (!mGoogleApiClient.isConnecting()) {
mGoogleApiClient.connect();
}
}
}
@Override
public void onConnectionSuspended(int arg0) {
mGoogleApiClient.connect();
}
}.
At first, i see the line mIntentInProgress == false;
and receive error:
Syntax error on token "==", invalid AssignmentOperator
so i changed to mIntentInProgress = false;
but then, when i run, it crashes.
Logcat:.......................
06-30 01:33:49.186: E/AndroidRuntime(2776): FATAL EXCEPTION: main
06-30 01:33:49.186: E/AndroidRuntime(2776): Process: com.example.oauth, PID: 2776
06-30 01:33:49.186: E/AndroidRuntime(2776): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.oauth/com.example.oauth.MainActivity}: java.lang.NullPointerException: Null options are not permitted for this Api
06-30 01:33:49.186: E/AndroidRuntime(2776): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2195)
06-30 01:33:49.186: E/AndroidRuntime(2776): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2245)
06-30 01:33:49.186: E/AndroidRuntime(2776): at android.app.ActivityThread.access$800(ActivityThread.java:135)
06-30 01:33:49.186: E/AndroidRuntime(2776): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1196)
06-30 01:33:49.186: E/AndroidRuntime(2776): at android.os.Handler.dispatchMessage(Handler.java:102)
06-30 01:33:49.186: E/AndroidRuntime(2776): at android.os.Looper.loop(Looper.java:136)
06-30 01:33:49.186: E/AndroidRuntime(2776): at android.app.ActivityThread.main(ActivityThread.java:5017)
06-30 01:33:49.186: E/AndroidRuntime(2776): at java.lang.reflect.Method.invokeNative(Native Method)
06-30 01:33:49.186: E/AndroidRuntime(2776): at java.lang.reflect.Method.invoke(Method.java:515)
06-30 01:33:49.186: E/AndroidRuntime(2776): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:779)
06-30 01:33:49.186: E/AndroidRuntime(2776): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595)
06-30 01:33:49.186: E/AndroidRuntime(2776): at dalvik.system.NativeStart.main(Native Method)
06-30 01:33:49.186: E/AndroidRuntime(2776): Caused by: java.lang.NullPointerException: Null options are not permitted for this Api
06-30 01:33:49.186: E/AndroidRuntime(2776): at com.google.android.gms.internal.fq.b(Unknown Source)
06-30 01:33:49.186: E/AndroidRuntime(2776): at com.google.android.gms.common.api.GoogleApiClient$Builder.addApi(Unknown Source)
06-30 01:33:49.186: E/AndroidRuntime(2776): at com.example.oauth.MainActivity.onCreate(MainActivity.java:28)
06-30 01:33:49.186: E/AndroidRuntime(2776): at android.app.Activity.performCreate(Activity.java:5231)
06-30 01:33:49.186: E/AndroidRuntime(2776): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087)
06-30 01:33:49.186: E/AndroidRuntime(2776): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2159)
06-30 01:33:49.186: E/AndroidRuntime(2776): ... 11 more
Upvotes: 2
Views: 2464
Reputation: 2093
Instead of
.addApi(Plus.API, null)
try this:
.addApi(Plus.API)
That worked for me.
Upvotes: 0
Reputation: 200130
As stated in the error - you cannot use a null PlusOptions
- connecting to the Plus API should look like:
mGoogleApiClient = new GoogleApiClient.Builder(this)
.addConnectionCallbacks(this).addOnConnectionFailedListener(this)
.addApi(Plus.API, new Plus.PlusOptions.Builder().build()) // note the options
.addScope(Plus.SCOPE_PLUS_LOGIN).build();
Upvotes: 9