GoogleApiClient标识符预计会出现Android Studio错误

时间:2017-05-16 00:48:14

标签: android android-studio google-api

我正在使用Android Studio,我想使用Google API作为登录信息。如果登录成功,则可以更改为其他活动。除此之外,我想在另一个活动中显示帐户名称和个人资料图片,但是我收到了这个错误:

  

错误:标识符期望GoogleApiClient = new GoogleApiClient.Builder(this).enableAutoManage(this,this).addApi(Auth.GOOGLE_SIGN_IN_API,gso).build();

我的代码:

package com.example.alif.angkotcoba;

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

import com.google.android.gms.auth.api.Auth;
import com.google.android.gms.auth.api.signin.GoogleSignInAccount;
import com.google.android.gms.auth.api.signin.GoogleSignInOptions;
import com.google.android.gms.auth.api.signin.GoogleSignInResult;
import com.google.android.gms.common.ConnectionResult;
import com.google.android.gms.common.SignInButton;
import com.google.android.gms.common.api.GoogleApiClient;

public class MainActivity extends AppCompatActivity implements 
View.OnClickListener,GoogleApiClient.OnConnectionFailedListener {

    private SignInButton SignIn;
    private GoogleApiClient GoogleApiClient;
    private static final int RC_SIGN_IN =9001;
    private static final String TAG ="signInActivity";
    GoogleSignInOptions gso=new GoogleSignInOptions.Builder(GoogleSignInOptions.DEFAULT_SIGN_IN)
            .requestIdToken(getString(R.string.default_web_client_id))
            .requestEmail()
            .build();
    GoogleApiClient = new GoogleApiClient.Builder(this).enableAutoManage(this,this).addApi(Auth.GOOGLE_SIGN_IN_API, gso).build();


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        SignIn = (SignInButton) findViewById(R.id.btngoogle);
        SignIn.setOnClickListener(this);
    }

    public void gotoSecondActivity (View view) {
        Intent intent = new Intent(this, SecondActivity.class);
        startActivity(intent);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.btngoogle:
                signIn ();
                break;
        }
    }

    public void signIn ()
    {
        Intent signInIntent = Auth.GoogleSignInApi.getSignInIntent(GoogleApiClient);
        startActivityForResult(signInIntent, RC_SIGN_IN);
    }
    public void handleResult (GoogleSignInResult result) {
        GoogleSignInAccount account = result.getSignInAccount();
        String name = account.getDisplayName();
        String email = account.getEmail();
        String img_url = account.getPhotoUrl().toString();
    }

    @Override
    public void onActivityResult(int requestcode,int resultcode,Intent data) {
        super.onActivityResult(requestcode,resultcode,data);
        if (requestcode==RC_SIGN_IN){
            GoogleSignInResult result=Auth.GoogleSignInApi.getSignInResultFromIntent(data);
            handleSignInResult(result);
        }
    }

    private void handleSignInResult(GoogleSignInResult result) {
        Log.d(TAG,"handleSignInResult:"+ result.isSuccess());
        if (result.isSuccess()){
            Intent intent = new Intent(this, SecondActivity.class);
            startActivity(intent);
        }
    }

    @Override
    public void onConnectionFailed(@NonNull ConnectionResult connectionResult) {
        Log.d(TAG,"onConnectionFailed:"+connectionResult);
    }
}

1 个答案:

答案 0 :(得分:0)

您需要在声明中使用变量名称。问题是你试图声明一个与类同名的变量:

private GoogleApiClient GoogleApiClient;

那不会奏效。就编译器而言,分配左侧的所有内容都是类型名称GoogleApiClient。尝试这样的事情:

private GoogleApiClient googleApiClient; // note change of case!

googleApiClient = ...

此外,您最好初始化依赖于this内的onCreate而不是声明变量的任何内容。