Android Google Plus API 集成错误 未请求适当的 Api

Android Google Plus API Integration error Appropriate Api was not requested

我正在尝试将 google+ 应用到我的申请中,因为我已参考 Google Developer page

这是应用程序 Gradle 文件:

dependencies {
    compile fileTree(dir: 'libs', include: ['*.jar'])
    compile 'com.android.support:appcompat-v7:22.2.0'
    compile 'com.android.support:support-v4:22.2.0'
    compile 'com.google.android.gms:play-services-base:8.4.0'

    compile 'com.google.android.gms:play-services-auth:8.4.0'
    compile 'com.facebook.android:facebook-android-sdk:4.7.0'
    compile project(':androidHorizontalListView')
    compile('com.twitter.sdk.android:twitter:1.11.0@aar') {
        transitive = true;
    }
    compile 'com.google.android.gms:play-services-appindexing:8.4.0'
}
apply plugin: 'com.google.gms.google-services'

项目Gradle:

buildscript {
    repositories {
        jcenter()
    }
    dependencies {
        classpath 'com.android.tools.build:gradle:1.2.3'
        classpath 'com.google.gms:google-services:2.0.0-alpha3'
        // NOTE: Do not place your application dependencies here; they belong
        // in the individual module build.gradle files
    }
}

Google + 登录 activity

public class LoginActivity extends FragmentActivity implements View.OnClickListener, IServerResponse, GoogleApiClient.OnConnectionFailedListener {

     private LinearLayout back_layout    = null;

    private ImageView googlplus_btn = null;


    //    ImageView _rightImageView;





    /**
     *
     * G+ Login
     *
     */


    private static final int RC_SIGN_IN = 9001;
    private GoogleApiClient mGoogleApiClient;
    private ProgressDialog mProgressDialog;


    /**
     * ATTENTION: This was auto-generated to implement the App Indexing API.
     * See https://g.co/AppIndexing/AndroidStudio for more information.
     */

    @Override
    public void onStart() {
        super.onStart();

        // ATTENTION: This was auto-generated to implement the App Indexing API.
        // See https://g.co/AppIndexing/AndroidStudio for more information.
        // googleClient.connect();
        Action viewAction = Action.newAction(
                Action.TYPE_VIEW, // TODO: choose an action type.
                "LoginActivity Page", // TODO: Define a title for the content shown.
                // TODO: If you have web page content that matches this app activity's content,
                // make sure this auto-generated web page URL is correct.
                // Otherwise, set the URL to null.
                Uri.parse("http://host/path"),
                // TODO: Make sure this auto-generated app deep link URI is correct.
                Uri.parse("android-app://com.tv.grantsource/http/host/path")
        );
        AppIndex.AppIndexApi.start(mGoogleApiClient, viewAction);


        OptionalPendingResult<GoogleSignInResult> opr = Auth.GoogleSignInApi.silentSignIn(mGoogleApiClient);
        if (opr.isDone()) {
            // If the user's cached credentials are valid, the OptionalPendingResult will be "done"
            // and the GoogleSignInResult will be available instantly.
            Log.d("LOGIN", "Got cached sign-in");
            GoogleSignInResult result = opr.get();
            handleSignInResult(result);
        } else {
            // If the user has not previously signed in on this device or the sign-in has expired,
            // this asynchronous branch will attempt to sign in the user silently.  Cross-device
            // single sign-on will occur in this branch.
            showProgressDialog();
            opr.setResultCallback(new ResultCallback<GoogleSignInResult>() {
                @Override
                public void onResult(GoogleSignInResult googleSignInResult) {
                    hideProgressDialog();
                    handleSignInResult(googleSignInResult);
                }
            });
        }
    }

    @Override
    public void onStop() {
        super.onStop();

        // ATTENTION: This was auto-generated to implement the App Indexing API.
        // See https://g.co/AppIndexing/AndroidStudio for more information.
        Action viewAction = Action.newAction(
                Action.TYPE_VIEW, // TODO: choose an action type.
                "LoginActivity Page", // TODO: Define a title for the content shown.
                // TODO: If you have web page content that matches this app activity's content,
                // make sure this auto-generated web page URL is correct.
                // Otherwise, set the URL to null.
                Uri.parse("http://host/path"),
                // TODO: Make sure this auto-generated app deep link URI is correct.
                Uri.parse("android-app://com.tv.grantsource/http/host/path")
        );
        AppIndex.AppIndexApi.end(mGoogleApiClient, viewAction);

    }


    private void showProgressDialog() {
        if (mProgressDialog == null) {
            mProgressDialog = new ProgressDialog(this);
            mProgressDialog.setMessage("Loading...");
            mProgressDialog.setIndeterminate(true);
        }

        mProgressDialog.show();
    }

    private void hideProgressDialog() {
        if (mProgressDialog != null && mProgressDialog.isShowing()) {
            mProgressDialog.hide();
        }
    }


    @Override
    public void onConnectionFailed(ConnectionResult connectionResult) {
        // An unresolvable error has occurred and Google APIs (including Sign-In) will not
        // be available.
        Log.d("LoginActivity", "onConnectionFailed:" + connectionResult);
    }




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



        //Google Plus

        GoogleSignInOptions gso = new GoogleSignInOptions.Builder(GoogleSignInOptions.DEFAULT_SIGN_IN)
                .requestEmail()
                .build();
        // Build a GoogleApiClient with access to the Google Sign-In API and the
// options specified by gso.
        mGoogleApiClient = new GoogleApiClient.Builder(this)
                .enableAutoManage(this,this)
                .addApi(Auth.GOOGLE_SIGN_IN_API, gso)
                .build();

        setContentView(R.layout.login_screen);
        sharedprefclass = new SharedprefClass(this);
        initialize();
        setonclick();

        // ATTENTION: This was auto-generated to implement the App Indexing API.
        // See https://g.co/AppIndexing/AndroidStudio for more information.
        mGoogleApiClient = new GoogleApiClient.Builder(this).addApi(AppIndex.API).build();
    }

    private void initialize() {
        back_layout = (LinearLayout) findViewById(R.id.back_layout);
        googlplus_btn = (ImageView) findViewById(R.id.googlplus_btn);
        setHedder();


        }

    private void setonclick() {
        googlplus_btn.setOnClickListener(this);
        back_layout.setOnClickListener(this);


    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.googlplus_btn:
                signIn();
                break;
        }

    }

@Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (requestCode == RC_SIGN_IN) {
            GoogleSignInResult result = Auth.GoogleSignInApi.getSignInResultFromIntent(data);
            handleSignInResult(result);
        }
    }

    /**
     *
     * Google plus
     *
     */

    private void signIn() {
        Intent signInIntent = Auth.GoogleSignInApi.getSignInIntent(mGoogleApiClient);
        startActivityForResult(signInIntent, RC_SIGN_IN);
    }
    // [END signIn]

    // [START signOut]
    private void signOut() {
        Auth.GoogleSignInApi.signOut(mGoogleApiClient).setResultCallback(
                new ResultCallback<Status>() {
                    @Override
                    public void onResult(Status status) {
                        // [START_EXCLUDE]
                        //  updateUI(false);
                        // [END_EXCLUDE]
                    }
                });
    }

    private void handleSignInResult(GoogleSignInResult result) {
        Log.d("LOGIN", "handleSignInResult:" + result.isSuccess());
        if (result.isSuccess()) {
            // Signed in successfully, show authenticated UI.
            GoogleSignInAccount acct = result.getSignInAccount();
      /*      mStatusTextView.setText(getString(R.string.signed_in_fmt, acct.getDisplayName()));
            updateUI(true);
      */  } else {
            // Signed out, show unauthenticated UI.
            //    updateUI(false);
        }
    }


}

调试 Logcat :

01-25 16:14:08.391 28129-28129/com.tv.grantsource E/AndroidRuntime: FATAL EXCEPTION: main
                                                                    Process: com.tv.grantsource, PID: 28129
                                                                    java.lang.RuntimeException: Unable to start activity ComponentInfo{com.tv.grantsource/com.tv.grantsource.LoginActivity}: java.lang.NullPointerException: Appropriate Api was not requested.
                                                                        at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2314)
                                                                        at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2388)
                                                                        at android.app.ActivityThread.access0(ActivityThread.java:148)
                                                                        at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1292)
                                                                        at android.os.Handler.dispatchMessage(Handler.java:102)
                                                                        at android.os.Looper.loop(Looper.java:135)
                                                                        at android.app.ActivityThread.main(ActivityThread.java:5312)
                                                                        at java.lang.reflect.Method.invoke(Native Method)
                                                                        at java.lang.reflect.Method.invoke(Method.java:372)
                                                                        at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:901)
                                                                        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:696)
                                                                     Caused by: java.lang.NullPointerException: Appropriate Api was not requested.
                                                                        at com.google.android.gms.common.internal.zzx.zzb(Unknown Source)
                                                                        at com.google.android.gms.common.api.internal.zzj.zza(Unknown Source)
                                                                        at com.google.android.gms.auth.api.signin.internal.zzc.zzb(Unknown Source)
                                                                        at com.google.android.gms.auth.api.signin.internal.zzc.silentSignIn(Unknown Source)
                                                                        at com.tv.grantsource.LoginActivity.onStart(LoginActivity.java:194)
                                                                        at android.app.Instrumentation.callActivityOnStart(Instrumentation.java:1243)
                                                                        at android.app.Activity.performStart(Activity.java:5969)
                                                                        at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2277)
                                                                        at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2388) 
                                                                        at android.app.ActivityThread.access0(ActivityThread.java:148) 
                                                                        at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1292) 
                                                                        at android.os.Handler.dispatchMessage(Handler.java:102) 
                                                                        at android.os.Looper.loop(Looper.java:135) 
                                                                        at android.app.ActivityThread.main(ActivityThread.java:5312) 
                                                                        at java.lang.reflect.Method.invoke(Native Method) 
                                                                        at java.lang.reflect.Method.invoke(Method.java:372) 
                                                                        at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:901) 
                                                                        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:696) 

有什么我遗漏的,如果有请告诉我

删除:

mGoogleApiClient = new GoogleApiClient.Builder(this).addApi(AppIndex.API).build();

从 onCreate() 最后一行开始,它覆盖对象。

你可以这样使用:

Intent shareIntent = new PlusShare.Builder(this)
.setType("text/plain")
.setText("Android Tutorial #android #tutorial")
.setContentUrl(Uri.parse("http://androidtutorialforbeginners.com"))
.getIntent();

参考link