android - Fragment 中的 SignInButton - 并且从不在 MainActivity 中调用 onClick

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

我已成功遵循指南 Accessing Google APIs .

之后我决定将一些代码移动到 fragment 中:

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 属性)实现?

最佳答案

this同样的问题一次。它不会在 XML 文件中工作。在您的 Fragment 或 Activity 中使用它。请记住在使用它之前初始化您的按钮:

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

关于android - Fragment 中的 SignInButton - 并且从不在 MainActivity 中调用 onClick,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32610705/

相关文章:

javascript - 是什么原因导致 Blogspot 中的 Google+ 评论在某些页面上不起作用?

android - Google API 的单独 key

Android 启动画面 : Scale Bitmap

android - Android 上的服务器模式 SSL startHandshake 需要 12 秒才能完成

android - 在 android 中更新 SQLite 表的问题

android - 为什么 android 文档要我将播放服务作为项目而不是库导入?

android - 玩游戏服务 : The application is incorrectly configured error?

java - Android读取大文件

javascript - 我将如何使用 google+ Hangouts api 创建角色?

scope - 是否可以单独获得电子邮件地址?