Google+登录按钮无法正常运行Android Studio

时间:2015-07-27 10:59:16

标签: android scope google-api-client google-plus-signin

您好我正在将Google登录集成到我的应用中。最近,由于某些原因,Android Studio无法识别Plus_Login范围(应用程序访问基本配置文件信息等所必需的),这是我唯一关心的问题。我目前的错误是

Error:(40, 17) error: method addScope in class Builder cannot be applied to given types;
required: Scope
found: String
reason: actual argument String cannot be converted to Scope by method invocation conversion

这是我的onCreate文件,其中找到了错误

编辑1 - 添加了导入语句和变量声明

package com.instigate.aggregator06;

import android.content.Intent;
import android.content.IntentSender;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.view.View;

import com.google.android.gms.common.ConnectionResult;
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.Scopes;
import com.google.android.gms.common.api.Scope;
import com.google.android.gms.plus.Plus;

public class SocialNetworkActivity extends AppCompatActivity implements
        ConnectionCallbacks, OnConnectionFailedListener, View.OnClickListener {

    private static final String TAG = "Social Network Activity";

    /* Request code used to invoke sign in user interactions. */
    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;

public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        mGoogleApiClient = new GoogleApiClient.Builder(this)
                .addConnectionCallbacks(this)
                .addOnConnectionFailedListener(this)
                .addApi(Plus.API)
                .addScope(Scopes.PLUS_LOGIN) // <----- This is where the error is found
                .addScope(Scopes.PLUS_ME)
                .build();

        findViewById(R.id.sign_in_button).setOnClickListener(this);
    }

我已经搜索了一段时间来找到这个错误的解决方案(因为Plus_Login范围应该正常运行)但是找不到解决方案。

编辑2 - 解决后的错误 我找到了答案。显然不是:

.addScope(Scopes.PLUS_LOGIN)
.addScope(Scopes.PLUS_ME)

我们写这个

.addScope(new Scope(Scopes.PLUS_LOGIN))
.addScope(new Scope(Scopes.PLUS_ME))

解决了我的问题。

然而,它揭示了一个新问题:

    findViewById(R.id.sign_in_button).setOnClickListener(this);

其中logcat指出此时存在NullPointerException。

编辑3 - 解决了NullPointerException 我已经解决了NullPointer异常问题,但我的Google+登录按钮仍无法正常运行。

这是整体课程(上面提到的代码段的延续)

package com.instigate.aggregator06;

import android.content.Intent;
import android.content.IntentSender;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.view.View;

import com.google.android.gms.common.ConnectionResult;
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.Scopes;
import com.google.android.gms.common.api.Scope;
import com.google.android.gms.plus.Plus;

public class SocialNetworkActivity extends AppCompatActivity implements
        ConnectionCallbacks, OnConnectionFailedListener, View.OnClickListener {

    private static final String TAG = "Social Network Activity";

    /* Request code used to invoke sign in user interactions. */
    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;

    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_social_network);

        mGoogleApiClient = new GoogleApiClient.Builder(this)
                .addConnectionCallbacks(this)
                .addOnConnectionFailedListener(this)
                .addApi(Plus.API)
                //.addScope(Scopes.PLUS_LOGIN)
                //.addScope(Scopes.PLUS_ME)
                .addScope(new Scope(Scopes.PLUS_ME))
                .addScope(new Scope(Scopes.PLUS_LOGIN))
                .build();

        this.findViewById(R.id.sign_in_button).setOnClickListener(this);
    }

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

    protected void onStop() {
        super.onStop();

        if (mGoogleApiClient.isConnected()) {
            mGoogleApiClient.disconnect();
        }
    }

    /* Code in case second one doesn't work

    @Override
    public void onConnectionFailed(ConnectionResult connectionResult) {
        // Could not connect to Google Play Services.  The user needs to select an account,
        // grant permissions or resolve an error in order to sign in. Refer to the javadoc for
        // ConnectionResult to see possible error codes.
        Log.d(TAG, "onConnectionFailed:" + connectionResult);

        if (!mIsResolving && mShouldResolve) {
            if (connectionResult.hasResolution()) {
                try {
                    connectionResult.startResolutionForResult(this, RC_SIGN_IN);
                    mIsResolving = true;
                } catch (IntentSender.SendIntentException e) {
                    Log.e(TAG, "Could not resolve ConnectionResult.", e);
                    mIsResolving = false;
                    mGoogleApiClient.connect();
                }
            } else {
                // Could not resolve the connection result, show the user an
                // error dialog.
                showErrorDialog(connectionResult);
            }
        }
    }*/

    public void onConnectionFailed(ConnectionResult result) {
        if (!mIntentInProgress && result.hasResolution()) {
            try {
                mIntentInProgress = true;
                startIntentSenderForResult(result.getResolution().getIntentSender(),
                        RC_SIGN_IN, null, 0, 0, 0);
            } catch (IntentSender.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.

        Log.d(TAG, "onConnected:" + connectionHint);
        Intent j = new Intent(SocialNetworkActivity.this, AccountPageActivity.class);
        startActivity(j);

    }

    protected void onActivityResult(int requestCode, int responseCode, Intent intent) {
        if (requestCode == RC_SIGN_IN) {
            mIntentInProgress = false;

            if (!mGoogleApiClient.isConnecting()) {
                mGoogleApiClient.connect();
            }
        }
    }

    public void onConnectionSuspended(int cause) {
        mGoogleApiClient.connect();
    }

    @Override
    public void onClick(View v) {
        if (v.getId() == R.id.sign_in_button) {
            //onSignInClicked();
            mGoogleApiClient.connect();
        }

        // ...
    }

    /* I find this unnecesary

    private void onSignInClicked() {
        // User clicked the sign-in button, so begin the sign-in process and automatically
        // attempt to resolve any errors that occur.
        //mShouldResolve = true;
        mGoogleApiClient.connect();

        // Show a message to the user that we are signing in.
        //mStatusTextView.setText(R.string.signing_in);
    }*/

    public void backToMainPage(View view) {
        Intent j = new Intent(SocialNetworkActivity.this, MainActivity.class);
        startActivity(j);
    }

}

这是我的该类文件的xml

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent"
    android:layout_height="match_parent" android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    android:paddingBottom="@dimen/activity_vertical_margin"
    tools:context="com.instigate.aggregator06.SelectSNActivity"
    android:orientation="vertical"
    android:id="@+id/SelectSNActivity"
    android:weightSum="1">

    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/backbtn2"
        android:id="@+id/backbtntest"
        android:layout_gravity="right"
        android:layout_alignParentBottom="true"
        android:layout_alignParentRight="true"
        android:layout_alignParentEnd="true"
        android:layout_marginBottom="43dp"
        android:onClick="backToMainPage"/>

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textAppearance="?android:attr/textAppearanceLarge"
        android:text="@string/selectSocialNetwork"
        android:id="@+id/selectSocialNetworkHeader"
        android:textStyle="bold"
        android:textSize="36sp"
        android:layout_gravity="center_horizontal"
        android:layout_alignParentTop="true"
        android:layout_centerHorizontal="true" />

    <com.google.android.gms.common.SignInButton
        android:id="@+id/sign_in_button"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@+id/selectSocialNetworkHeader"
        android:layout_alignParentLeft="true"
        android:layout_alignParentStart="true"
        android:layout_marginTop="52dp" />


</RelativeLayout>

非常感谢任何帮助,谢谢。

1 个答案:

答案 0 :(得分:1)

你有

 public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

您缺少setContentView(R.layout.activity_main)

您的布局必须

 <com.google.android.gms.common.SignInButton
    android:id="@+id/sign_in_button"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content" />

另外,请检查xml中按钮的ID,并确保在活动中引用相同的内容