简体   繁体   中英

integration with Google plus: initializing google plus API client client is not working

I have main activity in which I have a button that has intent to GooglePlus activity.

Here is my code of GooglePlus.java up to onCreateView() :

  import java.io.InputStream;

    import android.app.Activity;
    import android.content.Intent;
    import android.content.IntentSender.SendIntentException;
    import android.graphics.Bitmap;
    import android.graphics.BitmapFactory;
    import android.os.AsyncTask;
    import android.os.Bundle;
    import android.util.Log;
    import android.view.View;
    import android.view.View.OnClickListener;
    import android.widget.Button;
    import android.widget.ImageView;
    import android.widget.LinearLayout;
    import android.widget.TextView;
    import android.widget.Toast;

    import com.google.android.gms.common.ConnectionResult;
    import com.google.android.gms.common.GooglePlayServicesUtil;
    import com.google.android.gms.common.SignInButton;
    import com.google.android.gms.common.api.GoogleApiClient;
    import com.google.android.gms.common.api.GoogleApiClient.ConnectionCallbacks;
    import com.google.android.gms.common.api.GoogleApiClient.OnConnectionFailedListener;
    import com.google.android.gms.common.api.ResultCallback;
    import com.google.android.gms.common.api.Status;
    import com.google.android.gms.plus.Plus;
    import com.google.android.gms.plus.model.people.Person;

    public class GooglePlus extends Activity implements OnClickListener,
            ConnectionCallbacks, OnConnectionFailedListener {

        private static final int RC_SIGN_IN = 0;
        // Logcat tag
        private static final String TAG = "MainActivity";

        // Profile pic image size in pixels
        private static final int PROFILE_PIC_SIZE = 400;

        // Google client to interact with Google API
        private GoogleApiClient mGoogleApiClient;

        /**
         * A flag indicating that a PendingIntent is in progress and prevents us
         * from starting further intents.
         */
        private boolean mIntentInProgress;

        private boolean mSignInClicked;

        private ConnectionResult mConnectionResult;

        private SignInButton btnSignIn;
        private Button btnSignOut, btnRevokeAccess;
        private ImageView imgProfilePic;
        private TextView txtName, txtEmail;
        private LinearLayout llProfileLayout;

        @Override
        protected void onCreate(Bundle savedInstanceState) {

            Log.d("my", "in oncreateview");



            super.onCreate(savedInstanceState);
            setContentView(R.layout.googleplus);
            Log.d("my", "setcontentview is correct");
            btnSignIn = (SignInButton) findViewById(R.id.btn_sign_in);
            btnSignOut = (Button) findViewById(R.id.btn_sign_out);
            btnRevokeAccess = (Button) findViewById(R.id.btn_revoke_access);
            imgProfilePic = (ImageView) findViewById(R.id.imgProfilePic);
            txtName = (TextView) findViewById(R.id.txtName);
            txtEmail = (TextView) findViewById(R.id.txtEmail);
            llProfileLayout = (LinearLayout) findViewById(R.id.llProfile);

            Log.d("my", "layout mapped");


            // Button click listeners
            btnSignIn.setOnClickListener(this);
            btnSignOut.setOnClickListener(this);
            btnRevokeAccess.setOnClickListener(this);
            Log.d("my", "listeners are correct");

            // Initializing google plus api client

//ERROR IS POSSIBLY HERE:

            mGoogleApiClient = new GoogleApiClient.Builder(this)
            .addConnectionCallbacks(this)
            .addOnConnectionFailedListener(this).addApi(Plus.API, null)
            .addScope(Plus.SCOPE_PLUS_LOGIN).build();

            Log.d("my", " Initialized google plus api client");
        }

The logcat filter for my tag is as follows:

10-05 17:21:52.890: D/my(20539): intent started
10-05 17:21:52.910: D/my(20539): in oncreateview
10-05 17:21:52.920: D/my(20539): setcontentview is correct
10-05 17:21:52.920: D/my(20539): layout mapped
10-05 17:21:52.920: D/my(20539): listeners are correct

So, the error is at initializing google plus api client. I tried for an hour but couldn't identify the problem.

The logcat original error log is:

10-05 17:21:52.920: E/AndroidRuntime(20539): in writeCrashedAppName, pkgName :com.example.mapsdemo
10-05 17:21:52.920: E/AndroidRuntime(20539): FileNotFoundException while trying to write in crash app file
10-05 17:21:52.920: E/AndroidRuntime(20539): FATAL EXCEPTION: main
10-05 17:21:52.920: E/AndroidRuntime(20539): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.mapsdemo/com.example.mapsdemo.GooglePlus}: java.lang.NullPointerException: Null options are not permitted for this Api
10-05 17:21:52.920: E/AndroidRuntime(20539):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1956)
10-05 17:21:52.920: E/AndroidRuntime(20539):    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1981)
10-05 17:21:52.920: E/AndroidRuntime(20539):    at android.app.ActivityThread.access$600(ActivityThread.java:123)
10-05 17:21:52.920: E/AndroidRuntime(20539):    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1147)
10-05 17:21:52.920: E/AndroidRuntime(20539):    at android.os.Handler.dispatchMessage(Handler.java:99)
10-05 17:21:52.920: E/AndroidRuntime(20539):    at android.os.Looper.loop(Looper.java:137)
10-05 17:21:52.920: E/AndroidRuntime(20539):    at android.app.ActivityThread.main(ActivityThread.java:4424)
10-05 17:21:52.920: E/AndroidRuntime(20539):    at java.lang.reflect.Method.invokeNative(Native Method)
10-05 17:21:52.920: E/AndroidRuntime(20539):    at java.lang.reflect.Method.invoke(Method.java:511)
10-05 17:21:52.920: E/AndroidRuntime(20539):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:825)
10-05 17:21:52.920: E/AndroidRuntime(20539):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:592)
10-05 17:21:52.920: E/AndroidRuntime(20539):    at dalvik.system.NativeStart.main(Native Method)

And here is the manifest code:

 <permission
        android:name="com.example.mapsdemo.permission.MAPS_RECEIVE"
        android:protectionLevel="signature" />

    <uses-permission android:name="com.example.mapsdemo.permission.MAPS_RECEIVE" />

    <uses-sdk
        android:minSdkVersion="14"
        android:targetSdkVersion="19" />

    <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
    <uses-permission android:name="android.permission.INTERNET" />
    <uses-permission android:name="com.google.android.providers.gsf.permission.READ_GSERVICES" />
    <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
    <!-- Required to show current location -->
    <uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
    <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
    <uses-permission android:name="android.permission.GET_ACCOUNTS" />
    <uses-permission android:name="android.permission.USE_CREDENTIALS" />

    <!-- Required OpenGL ES 2.0. for Maps V2 -->
    <uses-feature
        android:glEsVersion="0x00020000"
        android:required="true" />

    <application
        android:allowBackup="true"
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >
        <activity
            android:name=".MainActivity"
            android:label="@string/app_name" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
        <activity
            android:name="com.facebook.LoginActivity"
            android:label="@string/app_name" >
        </activity>
        <!-- Goolge Maps API Key -->
        <meta-data
            android:name="com.google.android.maps.v2.API_KEY"
            android:value="xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx" />
        <meta-data
            android:name="com.google.android.gms.version"
            android:value="@integer/google_play_services_version" />



        <meta-data
            android:name="com.facebook.sdk.ApplicationId"
            android:value="@string/facebook_app_id" />

        <activity
            android:name=".GooglePlus"
            android:label="@string/title_activity_google_plus" >
        </activity>
    </application>

Am I missing anything? plz help.

Finally I solved the error:

The error occurred because Null options are not permitted for Google plus API.

Correct addApi(Plus.API,null) to addApi(Plus.API) .

So, finally following works:

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

        Log.d("my", " Initialized google plus api client");
    } 

.addApi(Plus.API,Plus.PlusOptions.builder()。build())

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM