来自 learnsauce.com 的 google+ 集成代码引发了很多错误并且构建失败

2024-03-22

我尝试了很多网站进行 google+ 登录集成,但即使是 Developers.android 代码也有很多错误。于是我看了一个学酱的视频:这是 Learn Sauce 的 G+ 集成视频教程的链接 https://www.youtube.com/watch?v=wcbMsd7dmYg我使用了所附 pdf 中给出的确切代码,但我遇到了巨大的错误。由于我是编程领域的新手,请帮助我。我附上以下代码:请不要标记这个问题的声誉,因为我是这个领域的新手!请 !

Build.gradle(模块应用程序)

apply plugin: 'com.android.application'

android {
    compileSdkVersion 23
    buildToolsVersion "23.0.2"

    defaultConfig {
        applicationId "com.example.user.noteapp"
        minSdkVersion 14
        targetSdkVersion 23
        versionCode 1
        versionName "1.0"
    }
    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
        }
    }
}

dependencies {
    compile fileTree(dir: 'libs', include: ['*.jar'])
    testCompile 'junit:junit:4.12'
    compile 'com.android.support:appcompat-v7:23.2.0'
    compile 'com.google.android.gms:play-services:6.5.87'
    compile 'de.hdodenhof:circleimageview:1.3.0'
}

清单文件

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.example.user.noteapp">

    <uses-permission android:name="android.permission.INTERNET" />
    <uses-permission android:name="android.permission.GET_ACCOUNTS" />
    <uses-permission android:name="android.permission.USE_CREDENTIALS" />

    <application
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:supportsRtl="true"
        android:theme="@style/AppTheme">
        <meta-data
            android:name="com.google.android.gms.version"
            android:value="@integer/google_play_services_version" />

        <activity android:name=".MainActivity">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
    </application>

</manifest>

字符串.xml

<resources>
    <string name="app_name">Note App</string>
    <string name="action_settings">Settings</string>

    <!-- Button text -->
    <string name="btn_logout_from_google">Logout from Google</string>
    <string name="btn_revoke_access">Revoke Access</string>
</resources>

活动主文件

<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=".MainActivity">
    <com.google.android.gms.common.SignInButton
        android:id="@+id/btn_sign_in"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_marginBottom="20dp"/>
    <RelativeLayout
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:id="@+id/profile_layout"
        android:layout_below="@+id/btn_sign_in"
        android:layout_alignParentLeft="true"
        android:visibility="gone"
        android:layout_alignParentStart="true">
        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:textAppearance="?android:attr/textAppearanceLarge"
            android:text="Profile Details"
            android:id="@+id/textView"
            android:textStyle="bold"
            android:textSize="18sp"
            android:layout_centerHorizontal="true"
            android:layout_alignParentTop="true"
            />
        <ImageView
            android:layout_width="150dp"
            android:layout_height="150dp"
            android:id="@+id/imageView_profile_image"
            android:layout_below="@+id/textView"
            android:layout_alignLeft="@+id/textView"
            android:layout_alignStart="@+id/textView"
            android:layout_marginTop="39dp" />
        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Name"
            android:id="@+id/textView_name"
            android:textAppearance="?android:attr/textAppearanceLarge"
            android:layout_below="@+id/imageView_profile_image"
            android:layout_centerHorizontal="true"
            android:layout_marginTop="40dp" />
        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:textAppearance="?android:attr/textAppearanceLarge"
            android:text="Email"
            android:id="@+id/textView_email"
            android:layout_below="@+id/textView_name"
            android:layout_centerHorizontal="true"
            android:layout_marginTop="25dp" />
        <Button
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="LogOut"
            android:id="@+id/button_logout"
            android:layout_below="@+id/textView_email"
            android:layout_centerHorizontal="true"
            android:layout_marginTop="24dp" />
        <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Revoke"
        android:id="@+id/button_revoke"
        android:layout_marginTop="10dp"
        android:layout_below="@+id/button_logout"
        android:layout_centerHorizontal="true" />
    </RelativeLayout>
</RelativeLayout>

MainActivity.java

package com.example.user.noteapp;

import android.content.Intent;
import android.content.IntentSender;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.AsyncTask;
import android.service.carrier.CarrierMessagingService;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.RelativeLayout;
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.plus.Plus;
import com.google.android.gms.plus.model.people.Person;

import java.io.InputStream;

public class MainActivity extends AppCompatActivity {
    private GoogleApiClient mGoogleApiClient;
    private SignInButton btnSignIn;
    private Button button_revoke,button_logout;
    private TextView textView_name, textView_email;
    private RelativeLayout profile_layout;
    private ImageView imageView_profile_image;
    private boolean mIntentInProgress;
    private boolean mSignInClicked;
    private ConnectionResult mConnectionResult;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        btnSignIn = (SignInButton) findViewById(R.id.btn_sign_in);
        btnSignIn.setOnClickListener(this);
        button_revoke = (Button) findViewById(R.id.button_revoke);
        button_revoke.setOnClickListener(this);
        button_logout = (Button) findViewById(R.id.button_logout);
        button_logout.setOnClickListener(this);
        imageView_profile_image = (ImageView) findViewById(R.id.imageView_profile_image);
        textView_name = (TextView) findViewById(R.id.textView_name);
        textView_email = (TextView) findViewById(R.id.textView_email);
        profile_layout = (RelativeLayout) findViewById(R.id.profile_layout);
        // Initializing google plus api client
        mGoogleApiClient = new GoogleApiClient.Builder(this)
                .addConnectionCallbacks(this)
                .addOnConnectionFailedListener(this).addApi(Plus.API)
                .addScope(Plus.SCOPE_PLUS_LOGIN).build();
    }
    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.btn_sign_in:
// Signin button clicked
                signInWithGplus();
                break;
            case R.id.button_logout:
// logout button clicked
                signOutFromGplus();
                break;
            case R.id.button_revoke:
// revoke button clicked
                revokeGplusAccess();
                break;
        }
    }
    protected void onStart() {
        super.onStart();
        mGoogleApiClient.connect();
    }
    protected void onStop() {
        super.onStop();
        if (mGoogleApiClient.isConnected()) {
            mGoogleApiClient.disconnect();
        }
    }
    @Override
    public void onConnected(Bundle bundle) {
        mSignInClicked = false;
        Toast.makeText(this, "User is connected!", Toast.LENGTH_LONG).show();
// Get user's information
        getProfileInformation();
// Update the UI after signin
        updateUI(true);
    }
    @Override
    public void onConnectionSuspended(int i) {
        mGoogleApiClient.connect();
        updateUI(false);
    }
    @Override
    public void onConnectionFailed(ConnectionResult connectionResult) {
        if (!connectionResult.hasResolution()) {
            GooglePlayServicesUtil.getErrorDialog(connectionResult.getErrorCode(), this,
                    0).show();
            return;
        }
        if (!mIntentInProgress) {
// Store the ConnectionResult for later usage
            mConnectionResult = connectionResult;
            if (mSignInClicked) {
// The user has already clicked 'sign-in' so we attempt to
// resolve all
// errors until the user is signed in, or they cancel.
                resolveSignInError();
            }
        }
    }
    private static final int GOOGLE_SIGIN = 100;
    private void resolveSignInError() {
        if (mConnectionResult.hasResolution()) {
            try {
                mIntentInProgress = true;
                mConnectionResult.startResolutionForResult(this, GOOGLE_SIGIN);
            }
            catch (IntentSender.SendIntentException e) {
                mIntentInProgress = false;
                mGoogleApiClient.connect();
            }
        }
    }
    @Override
    protected void onActivityResult(int requestCode, int responseCode,
                                    Intent intent) {
        if (requestCode == GOOGLE_SIGIN) {
            if (responseCode != RESULT_OK) {
                mSignInClicked = false;
            }
            mIntentInProgress = false;
            if (!mGoogleApiClient.isConnecting()) {
                mGoogleApiClient.connect();
            }
        }
    }
    private void updateUI(boolean isSignedIn) {
        if (isSignedIn) {
            btnSignIn.setVisibility(View.GONE);
            profile_layout.setVisibility(View.VISIBLE);
        } else {
            btnSignIn.setVisibility(View.VISIBLE);
            profile_layout.setVisibility(View.GONE);
        }
    }
    private void getProfileInformation() {
        try {
            if (Plus.PeopleApi.getCurrentPerson(mGoogleApiClient) != null) {
                Person currentPerson = Plus.PeopleApi
                        .getCurrentPerson(mGoogleApiClient);
                String personName = currentPerson.getDisplayName();
                String personPhotoUrl = currentPerson.getImage().getUrl();
                String personGooglePlusProfile = currentPerson.getUrl();
                String email = Plus.AccountApi.getAccountName(mGoogleApiClient);
                textView_name.setText(personName);
                textView_email.setText(email);
// by default the profile url gives 50x50 px image only
// we can replace the value with whatever dimension we want by
// replacing sz=X
                personPhotoUrl = personPhotoUrl.substring(0,
                        personPhotoUrl.length() - 2)
                        + 400;
                new LoadProfileImage(imageView_profile_image).execute(personPhotoUrl);
            } else {
                Toast.makeText(getApplicationContext(),
                        "Person information is null", Toast.LENGTH_LONG).show();
            }
        }
        catch (Exception e) {
            e.printStackTrace();
        }
    }
    private void signInWithGplus() {
        if (!mGoogleApiClient.isConnecting()) {
            mSignInClicked = true;
            resolveSignInError();
        }
    }
/**
 * Sign-out from google
 * */
private void signOutFromGplus() {
    if (mGoogleApiClient.isConnected()) {
        Plus.AccountApi.clearDefaultAccount(mGoogleApiClient);
        mGoogleApiClient.disconnect();
        mGoogleApiClient.connect();
        updateUI(false);
    }
}
    /**
     * Revoking access from google
     * */
    private void revokeGplusAccess() {
        if (mGoogleApiClient.isConnected()) {
            Plus.AccountApi.clearDefaultAccount(mGoogleApiClient);
            Plus.AccountApi.revokeAccessAndDisconnect(mGoogleApiClient)
                    .setResultCallback(new CarrierMessagingService.ResultCallback<AsyncTask.Status>() {
                        @Override
                        public void onResult(AsyncTask.Status arg0) {
                            Log.e("pavan", "User access revoked!");
                            mGoogleApiClient.connect();
                            updateUI(false);
                        }
                    });
        }
    }
    /**
     * Background Async task to load user profile picture from url
     * */
    private class LoadProfileImage extends AsyncTask<String, Void, Bitmap> {
        ImageView bmImage;
        public LoadProfileImage(ImageView bmImage) {
            this.bmImage = bmImage;
        }
        protected Bitmap doInBackground(String... urls) {
            String urldisplay = urls[0];
            Bitmap mIcon11 = null;
            try {
                InputStream in = new java.net.URL(urldisplay).openStream();
                mIcon11 = BitmapFactory.decodeStream(in);
            } catch (Exception e) {
                Log.e("Error", e.getMessage());
                e.printStackTrace();
            }
            return mIcon11;
        }
        protected void onPostExecute(Bitmap result) {
            bmImage.setImageBitmap(result);
        }
    }
}

Errors:

Error:(44, 18) error: no suitable method found for setOnClickListener(MainActivity)
method View.setOnClickListener(OnClickListener) is not applicable
(argument mismatch; MainActivity cannot be converted to OnClickListener)
method SignInButton.setOnClickListener(OnClickListener) is not applicable
(argument mismatch; MainActivity cannot be converted to OnClickListener)
Error:(95, 5) error: method does not override or implement a method from a supertype
Error:(204, 103) error: <anonymous com.example.user.noteapp.MainActivity$1> is not abstract and does not override abstract method onReceiveResult(Status) in ResultCallback
Error:(100, 5) error: method does not override or implement a method from a supertype
Error:(55, 41) error: incompatible types: MainActivity cannot be converted to ConnectionCallbacks
Error:(86, 5) error: method does not override or implement a method from a supertype
Error:(48, 42) error: incompatible types: MainActivity cannot be converted to OnClickListener
Error:(46, 42) error: incompatible types: MainActivity cannot be converted to OnClickListener
Error:(59, 5) error: method does not override or implement a method from a supertype
Error:(205, 25) error: method does not override or implement a method from a supertype
Error:(204, 40) error: incompatible types: <anonymous android.service.carrier.CarrierMessagingService.ResultCallback<android.os.AsyncTask.Status>> cannot be converted to com.google.android.gms.common.api.ResultCallback<com.google.android.gms.common.api.Status>
Note: Some messages have been simplified; recompile with -Xdiags:verbose to get full output
Error:Execution failed for task ':app:compileDebugJavaWithJavac'.
> Compilation failed; see the compiler error output for details.

MainActivity 应该实现 OnClickListener,GoogleApiClient.ConnectionCallbacks, 和 GoogleApiClient.OnConnectionFailedListener,如下所示:

    public class MainActivity extends AppCompatActivity implements GoogleApiClient.ConnectionCallbacks,
            GoogleApiClient.OnConnectionFailedListener, OnClickListener{
//Class Content 

//And this function needs changed
         /**
         * Revoking access from google
         * */
        private void revokeGplusAccess() {
            if (mGoogleApiClient.isConnected()) {
                Plus.AccountApi.clearDefaultAccount(mGoogleApiClient);



                //You will notice AsyncTask.Status changed to com.google.android.gms.common.api.Status
                Plus.AccountApi.revokeAccessAndDisconnect(mGoogleApiClient)
                    .setResultCallback(new CarrierMessagingService.ResultCallback<com.google.android.gms.common.api.Status>() {
                            @Override
                            public void onResult(AsyncTask.Status arg0) {
                                Log.e("pavan", "User access revoked!");
                                mGoogleApiClient.connect();
                                updateUI(false);
                            }
                        });
            }
        }
}

代替 :new CarrierMessagingService.ResultCallback<com.google.android.gms.common.api.Status>

Use new com.google.android.gms.common.api.ResultCallback<com.google.android.gms.common.api.Status>

另外,如果您按键盘上的 Ctr+I,它会显示可以实现的方法。

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

来自 learnsauce.com 的 google+ 集成代码引发了很多错误并且构建失败 的相关文章

随机推荐

  • Seaborn:ValueError:调色板='jet'否

    从seaborn运行以下示例docs https seaborn pydata org generated seaborn boxplot html有论据palette jet import seaborn as sns tips sns
  • 金字塔资源:简单的英语

    我一直在阅读对新创建的金字塔应用程序实施授权 和身份验证 的方法 我不断遇到 资源 这个概念 我在应用程序中使用 python couchdb 根本不使用 RDBMS 因此没有 SQLAlchemy 如果我像这样创建一个 Product 对
  • 为什么说malloc()和printf()是不可重入的?

    在 UNIX 系统中我们知道malloc 是不可重入函数 系统调用 这是为什么 相似地 printf 也被认为是不可重入的 为什么 我知道可重入的定义 但我想知道为什么它适用于这些函数 是什么阻止了它们保证可重入 malloc and pr
  • Cassandra 种子节点和连接到节点的客户端

    我对 Cassandra 种子节点以及客户端如何连接到集群有点困惑 我似乎在文档中找不到这一点信息 客户端是否仅包含种子节点列表 并且每个节点委托一个新主机供客户端连接 种子节点是否真的仅用于节点到节点的发现 而不是客户端的特殊节点 每个客
  • 如何捕获发送到模拟的参数?

    有谁知道如何捕获发送到 OCMock 对象的参数 id mock OCMockObject mockForClass someClass NSObject captureThisArgument mock expect foo
  • 如何从 onBind 函数获取尝试绑定我的服务的应用程序包名称或 UID?

    我在一个应用程序中有一个服务 我可以从不同的应用程序访问该服务 当应用程序尝试绑定此服务时 我想知道哪个应用程序正在尝试在 onBind 函数中绑定我的服务 但我无法在 onBind 函数中获取该应用程序的包名称或 UID 是否可以获取尝试
  • 使用 MPJ Express 发送对象

    我是并行编程的新手 我想用 java 来完成它 我想知道是否可以通过 MPI 发送和接收更复杂的对象 我用的是 MPJ Express 然而 每当我想发送一个对象时 我都会收到 ClassCastException MPI Init arg
  • 如何使用 vscode:// 链接打开文件

    我想像phpstorm一样使用vscode ide链接 我知道我们可以phpstorm open file filepath line line 如何使用vscode达到同样的效果 多谢 这个链接 vscode file file line
  • SwiftUI 在 NavigationLink 视图中隐藏 TabView 栏

    我为每个选项卡项目都有一个 TabView 和单独的 NavigationView 堆栈 它工作得很好 但是当我打开任何 NavigationLink 时 TabView 栏仍然显示 我希望每当我单击任何导航链接时它就会消失 struct
  • 使用 iPhone 将图片发布到 Twitter

    我对在 ios4 上使用 Twitter API 完全陌生 我正在寻找将 uiimage 发布到用户页面的最简单方法 如何 use http dev twitpic com http dev twitpic com 在此处发布您的 http
  • jQuery 中元素的总宽度(包括内边距和边框)

    正如主题所示 如何使用 jQuery 获取元素的总宽度 包括其边框和填充 我已经有了 jQuery 尺寸插件 并且正在运行 width on my 760px wide 10px paddingDIV 回报760 也许我做错了什么 但如果我
  • 有什么方法可以初始化 unique_ptr 向量吗?

    例如 struct A vector
  • React - 作为 npm 包发布的组件之间的通信和路由

    我正在尝试为该项目设置微前端架构 该项目包含多个react应用 以下是项目结构 容器 标头 npm 包 仪表板 npm 包 app1 npm 包 app2 npm 包 app3 npm 包 在这里 容器充当其他应用程序的包装器 仪表板应用程
  • 带 where 子句的 T-SQL Group by

    Masterid CC CLA DES NLCLA NLDES 53006141 CN 0 0 1 1 53006141 US 1 1 1 1 53006141 UK 1 1 0 0 53006142 US 1 1 0 0 53006142
  • 在危险的SetInnerHTML中传递反应组件

    服务器返回类似以下内容 内容 p Hello world smile strong NICE strong p 这是因为我们支持降价 现在我有一个解析器可以解析所有内容 text 变成表情符号 我在用emoji mart对于这个 这就是内容
  • 自动从外部文件替换表

    我正在尝试使用外部 XML 文件替换大型 300 MB XML 文件中的多个表 大约有 30 000 个表 并且有 23 000 个 XML 文件 因为某些表未更改 例如 如果我有
  • Scrapy:为什么提取的字符串是这种格式?

    我正在做 item desc site select a text extract 但这会像这样打印 u n A mano libera n 我必须做什么来添加和删除奇怪的字符 例如 u in 尾随空格和 我无法修剪 剥离 exceptio
  • jQuery mobile - 将页面拆分为不同的文件

    我正在开发一个基于 jQuery 和 jQuery mobile 的网络应用程序 我想显示不同的页面 但由于相应的 html 标记可能会变得很大 我想将 html 分成不同的文件 即 div div
  • 从另一个控制器调用 WebApi 控制器

    所以我的解决方案中有两个项目 两者都是WebApi 我在两者中都有控制器 我可以从客户端调用它们 javascript 问题是我需要从第一个项目的控制器中的方法调用第二个项目的控制器中的方法 我还认为我应该声明我需要调用的方法是 Post
  • 来自 learnsauce.com 的 google+ 集成代码引发了很多错误并且构建失败

    我尝试了很多网站进行 google 登录集成 但即使是 Developers android 代码也有很多错误 于是我看了一个学酱的视频 这是 Learn Sauce 的 G 集成视频教程的链接 https www youtube com