片段中的SignInButton - 而onClick永远不会在MainActivity中调用

时间:2015-09-16 13:57:39

标签: android google-plus google-play-services google-plus-signin

我已成功遵循指南Accessing Google APIs

之后我决定将一些代码移到片段中:

screenshot

<FrameLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <com.google.android.gms.common.SignInButton
        android:id="@+id/login_button"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center"
        android:clickable="true"
        android:onClick="googleLogin" />

</FrameLayout>

在MainActivity中,我已将GoogleApiClient对象的创建从onCreate()移至googleLogin()方法:

public void googleLogin(View v) {
    mGoogleApiClient = new GoogleApiClient.Builder(this)
            .addConnectionCallbacks(this)
            .addOnConnectionFailedListener(this)
            .addApi(Plus.API)
            .addScope(Plus.SCOPE_PLUS_PROFILE)
            .build();

    mGoogleApiClient.connect();
}

但是从不调用该方法(正如我在调试器中看到的那样)。

这里有什么问题,它是当前SignInButton实现中的一个错误(忽略onClick XML属性)吗?

3 个答案:

答案 0 :(得分:3)

曾有this次问题。它无法在XML文件中运行。在Fragment或Activity.Remember中使用它来在使用之前初始化按钮:

signInButton = (SignInButton)findViewById(R.id.btn_sign_in);
  signInButton.setOnClickListener(this);

答案 1 :(得分:2)

在按钮之前单击

         mGoogleApiClient = new GoogleApiClient.Builder(this)
                    .addConnectionCallbacks(this)
                    .addOnConnectionFailedListener(this)
                    .addApi(Plus.API)
                    .addScope(Plus.SCOPE_PLUS_LOGIN)
                    .build();
         btnSignIn = (SignInButton)findViewById(R.id.btn_sign_in);
         btnSignIn.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v)
                    {
                        if (!mGoogleApiClient.isConnecting()) {
                            mSignInClicked = true;
                            mGoogleApiClient.connect();
                        }
                    }
                });


     @Override
        public void onActivityResult(int requestCode, int resultCode, Intent data)
        {
            super.onActivityResult(requestCode, resultCode, data);
           if (requestCode == RC_SIGN_IN)
                {
                    if (resultCode != RESULT_OK)
                    {
                        mSignInClicked = false;
                    }

                    mIntentInProgress = false;

                    if (!mGoogleApiClient.isConnected()) {
                        mGoogleApiClient.reconnect();
                    }
                }


        }



@Override
    public void onConnected(Bundle bundle)
    {
        mSignInClicked = false;
        if (Plus.PeopleApi.getCurrentPerson(mGoogleApiClient) != null)
        {

            Person currentPerson = Plus.PeopleApi.getCurrentPerson(mGoogleApiClient);
            //Toast.makeText(Login_Activity.this,str_id,Toast.LENGTH_LONG).show();

            str_first_name = currentPerson.getDisplayName();
            google_id = currentPerson.getImage().getUrl();
            System.out.println("google_id==  "+google_id);
            str_email = Plus.AccountApi.getAccountName(mGoogleApiClient);
            str_mobile ="";
            int temp=currentPerson.getGender();
            if(temp==0)
            {
                str_gender="male";
            }
            else if(temp==1)
            {
                str_gender="female";
            }

            str_date_of_birth=currentPerson.getBirthday();
           // Toast.makeText(Login_Activity.this,str_gender,Toast.LENGTH_LONG).show();
           // Toast.makeText(Login_Activity.this,str_date_of_birth,Toast.LENGTH_LONG).show();



        }

    }

    @Override
    public void onConnectionSuspended(int i) {
        mGoogleApiClient.connect();
    }

    @Override
    public void onConnectionFailed(ConnectionResult connectionResult) {

        if (!mIntentInProgress) {
            if (mSignInClicked && connectionResult.hasResolution()) {
                // The user has already clicked 'sign-in' so we attempt to resolve all
                // errors until the user is signed in, or they cancel.
                try {
                    connectionResult.startResolutionForResult(this, RC_SIGN_IN);
                    mIntentInProgress = true;

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

    }

Google+ Platform for Android

答案 2 :(得分:1)

为什么不尝试删除xml中的onClick并将其放入您正在创建布局的片段中。

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)
{
    view = inflater.inflate(R.layout.layout, container, false);
    login_button = ( com.google.android.gms.common.SignInButton ) view.findViewById(R.id.login_button);
    login_button.setOnClickListener( this );
    return view;
}

@Override
public void onClick( View v )
{
    switch ( v.getId() )
    {
        case R.id.login_button:
            mGoogleApiClient = new GoogleApiClient.Builder(this)
            .addConnectionCallbacks(this)
            .addOnConnectionFailedListener(this)
            .addApi(Plus.API)
            .addScope(Plus.SCOPE_PLUS_PROFILE)
            .build();
            mGoogleApiClient.connect();
            break;
    }
}