gpt4 book ai didi

android - 实现 GoogleApiClient 时遇到问题

转载 作者:行者123 更新时间:2023-11-30 01:53:52 26 4
gpt4 key购买 nike

我正在尝试在我正在创建的应用程序上使用 Google SignIn。我一直在使用 Google 的开发人员教程,但我收到错误消息“类‘HomeActivity’必须声明为抽象或在‘ConnectionCallbacks’中实现抽象方法‘onConnected(Bundle)’,突出显示如下:

public class HomeActivity extends Activity implements
ConnectionCallbacks,
OnConnectionFailedListener,
OnClickListener

这是我的 HomeActivity 代码,其中大部分是我从 Google 使用的:

import android.app.Activity;
import android.content.IntentSender;
import android.os.Bundle;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;


import com.google.android.gms.common.ConnectionResult;
import com.google.android.gms.common.Scopes;
import com.google.android.gms.common.api.GoogleApiClient;
import com.google.android.gms.common.api.GoogleApiClient.ConnectionCallbacks;
import com.google.android.gms.common.api.GoogleApiClient.OnConnectionFailedListener;
import com.google.android.gms.common.api.Scope;
import com.google.android.gms.plus.Plus;
import static android.view.View.*;


public class HomeActivity extends Activity implements
ConnectionCallbacks,
OnConnectionFailedListener,
OnClickListener {

private static final int RC_SIGN_IN = 0;
private GoogleApiClient mGoogleApiClient;
private Button mLoginButton;
private TextView mSignUpLabel;

/* Is there a ConnectionResult resolution in progress? */
private boolean mIsResolving = false;

/* Should we automatically resolve ConnectionResults when possible? */
private boolean mShouldResolve = false;


@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_home);

mLoginButton = (Button) findViewById(R.id.btnLogin);
mLoginButton.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
Toast.makeText(HomeActivity.this, "Button Pressed", Toast.LENGTH_SHORT).show();
}
});

mSignUpLabel = (TextView) findViewById(R.id.lblAboutUs);
mSignUpLabel.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
Toast.makeText(HomeActivity.this, "WAY TO SIGN UP MAN", Toast.LENGTH_SHORT).show();
}
});

findViewById(R.id.sign_in_button).setOnClickListener(this);

// Build GoogleApiClient with access to basic profile
mGoogleApiClient = new GoogleApiClient.Builder(this)
.addConnectionCallbacks(this)
.addOnConnectionFailedListener(this)
.addApi(Plus.API)
.addScope(new Scope(Scopes.PROFILE))
.build();
}

@Override
public void onConnectionSuspended(int arg0) {

// what should i do here ? should i call mGoogleApiClient.connect() again ? ?

}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu_home, menu);
return true;
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();

//noinspection SimplifiableIfStatement
if (id == R.id.action_settings) {
return true;
}

return super.onOptionsItemSelected(item);
}


@Override
public void onConnectionFailed(ConnectionResult connectionResult) {
// Could not connect to Google Play Services. The user needs to select an account,
// grant permissions or resolve an error in order to sign in. Refer to the javadoc for
// ConnectionResult to see possible error codes.
Log.d(TAG, "onConnectionFailed:" + connectionResult);

if (!mIsResolving && mShouldResolve) {
if (connectionResult.hasResolution()) {
try {
connectionResult.startResolutionForResult(this, RC_SIGN_IN);
mIsResolving = true;
} catch (IntentSender.SendIntentException e) {
Log.e(TAG, "Could not resolve ConnectionResult.", e);
mIsResolving = false;
mGoogleApiClient.connect();
}
} else {
// Could not resolve the connection result, show the user an
// error dialog.
showErrorDialog(connectionResult);
}
} else {
// Show the signed-out UI
showSignedOutUI();
}
}

@Override
protected void onStart() {
super.onStart();
mGoogleApiClient.connect();
}

@Override
protected void onStop() {
super.onStop();
mGoogleApiClient.disconnect();
}
}

任何帮助将不胜感激!!!

最佳答案

您的类声明它将实现 ConnectionCallbacks,这意味着您必须定义该接口(interface)(以及您希望实现的所有其他接口(interface))中包含的所有方法。两者都是

Android Studio(点击左端的灯泡)和 Eclipse 必须能够自动实现未实现的方法(当然你必须用任何自定义代码填充它们)

关于android - 实现 GoogleApiClient 时遇到问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32576253/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com