与Google plus集成:初始化google plus API客户端客户端无法正常工作

时间:2014-10-05 12:12:38

标签: java android google-plus

我有一个主要活动,我有一个意图进行GooglePlus活动的按钮。

以下是GooglePlus.java最多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");
        }

my标记的logcat过滤器如下:

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

因此,错误在于初始化google plus api客户端。我试了一个小时,但无法确定问题。

logcat原始错误日志是:

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)

以下是清单代码:

 <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>

我错过了什么吗?请帮助。

2 个答案:

答案 0 :(得分:5)

最后我解决了错误:

发生错误是因为Google plus API不允许使用Null选项。

更正addApi(Plus.API,null)addApi(Plus.API)

所以,最后继续工作:

  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");
    } 

答案 1 :(得分:0)

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