Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/221.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 谷歌+;来自learnsauce.com的集成代码引发了如此多的错误,构建失败_Java_Android_Android Studio - Fatal编程技术网

Java 谷歌+;来自learnsauce.com的集成代码引发了如此多的错误,构建失败

Java 谷歌+;来自learnsauce.com的集成代码引发了如此多的错误,构建失败,java,android,android-studio,Java,Android,Android Studio,我尝试了很多google+登录集成的站点,但即使是开发者。android代码也有很多错误。所以我看了一段LearnSauce的视频:我使用了附件pdf中给出的确切代码,但我面临着巨大的错误。由于我是编程领域的新手,请帮助我。我附上以下代码: 请不要记下这个问题的名声,因为我是这个领域的新手!求你了 Build.gradle(模块应用程序) 清单文件 <?xml version="1.0" encoding="utf-8"?> <manifest xmlns:android="h

我尝试了很多google+登录集成的站点,但即使是开发者。android代码也有很多错误。所以我看了一段LearnSauce的视频:我使用了附件pdf中给出的确切代码,但我面临着巨大的错误。由于我是编程领域的新手,请帮助我。我附上以下代码: 请不要记下这个问题的名声,因为我是这个领域的新手!求你了

Build.gradle(模块应用程序)

清单文件

<?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>

Strings.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>

便笺应用程序
设置
从谷歌注销
撤销访问权
活动\u main.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);
        }
    }
}
package com.example.user.noteapp;
导入android.content.Intent;
导入android.content.IntentSender;
导入android.graphics.Bitmap;
导入android.graphics.BitmapFactory;
导入android.os.AsyncTask;
导入android.service.carrier.carrierMessageService;
导入android.support.v7.app.AppActivity;
导入android.os.Bundle;
导入android.util.Log;
导入android.view.view;
导入android.widget.Button;
导入android.widget.ImageView;
导入android.widget.RelativeLayout;
导入android.widget.TextView;
导入android.widget.Toast;
导入com.google.android.gms.common.ConnectionResult;
导入com.google.android.gms.common.GooglePlayServicesUtil;
导入com.google.android.gms.common.SignInButton;
导入com.google.android.gms.common.api.GoogleAppClient;
导入com.google.android.gms.plus.plus;
导入com.google.android.gms.plus.model.people.Person;
导入java.io.InputStream;
公共类MainActivity扩展了AppCompatActivity{
私人GoogleapClient MGoogleapClient;
私人签名按钮;
私有按钮按钮撤销、按钮注销;
私人文本视图文本视图名称、文本视图电子邮件;
私密的相对性布局;
私有图像查看图像查看配置文件图像;
私有布尔mIntentInProgress;
私有布尔msignincled;
私有连接结果mConnectionResult;
@凌驾
创建时受保护的void(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
btnSignIn=(签名按钮)findViewById(R.id.btn\u sign\u in);
btnSignIn.setOnClickListener(此);
button_revoke=(button)findViewById(R.id.button_revoke);
按钮_revoke.setOnClickListener(此);
按钮\注销=(按钮)findViewById(R.id.button\注销);
按钮\注销.setOnClickListener(此);
imageView\u profile\u image=(imageView)findViewById(R.id.imageView\u profile\u image);
textView\u name=(textView)findViewById(R.id.textView\u name);
textView_email=(textView)findViewById(R.id.textView_email);
profile_布局=(RelativeLayout)findViewById(R.id.profile_布局);
//初始化google plus api客户端
mgoogleapclient=新的Googleapclient.Builder(此)
.addConnectionCallbacks(此)
.addOnConnectionFailedListener(this).addApi(Plus.API)
.addScope(Plus.SCOPE\u Plus\u LOGIN).build();
}
@凌驾
公共void onClick(视图v){
开关(v.getId()){
案例R.id.btn\u登录:
//已单击登录按钮
用gplus()表示;
打破
案例R.id.按钮\注销:
//已单击注销按钮
signOutFromGplus();
打破
案例R.id.button\u撤销:
//单击撤销按钮
revokeGplusAccess();
打破
}
}
受保护的void onStart(){
super.onStart();
mGoogleApiClient.connect();
}
受保护的void onStop(){
super.onStop();
if(mgoogleapClient.isConnected()){
mGoogleApiClient.disconnect();
}
}
@凌驾
未连接的公共空间(捆绑包){
mSignInClicked=false;
Toast.makeText(此“用户已连接!”,Toast.LENGTH_LONG.show();
//获取用户信息
getProfileInformation();
//登录后更新UI
更新(真);
}
@凌驾
公共空间连接暂停(int i){
mGoogleApiClient.connect();
更新(假);
}
@凌驾
公共无效onConnectionFailed(ConnectionResult ConnectionResult){
如果(!connectionResult.hasResolution()){
GooglePlayServicesUtil.getErrorDialog(connectionResult.getErrorCode(),此,
0)show();
返回;
}
如果(!mIntentInProgress){
//存储ConnectionResult供以后使用
mConnectionResult=连接结果;
如果(mSignInClicked){
//用户已单击“登录”,因此我们尝试
//解决所有问题
//错误,直到用户登录或取消。
ResolveSignError();
}
}
}
私有静态最终整数GOOGLE_SIGIN=100;
私有无效解析错误(){
if(mcConnectionResult.hasResolution()){
试一试{
mIntentInProgress=true;
mConnectionResult.startResolutionForResult(这是GOOGLE_SIGIN);
}
catch(IntentSender.sendtintentexe){
mIntentInProgress=false;
mGoogleApiClient.connect();
}
}
}
@凌驾
ActivityResult上的受保护无效(int请求代码、int响应代码、,
意图(意图){
if(requestCode==GOOGLE\u SIGIN){
if(responseCode!=结果\u正常){
mSignInClicked=false
@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;
        }
    }
    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);
                            }
                        });
            }
        }
}