gpt4 book ai didi

java - 创建 Google 登录时出错

转载 作者:行者123 更新时间:2023-12-01 09:57:18 25 4
gpt4 key购买 nike

我正在尝试实现以下 Google 登录,但收到以下错误:

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

private LoginButton loginButton;
private CallbackManager callbackManager;
private Button mButton;
private TextView mTextDetail;
private TextView mTextAccessToken;
private GoogleApiClient mGoogleApiClient;
private static final String TAG = "SignInActivity";
private static final int RC_SIGN_IN = 9001;

private FacebookCallback<LoginResult> mCallback=new FacebookCallback<LoginResult>() {
@Override
public void onSuccess(LoginResult loginResult) {

// mTextDetail = (TextView) findViewById(R.id.editText);
// mTextAccessToken = (TextView) findViewById(R.id.editText2);

AccessToken accessToken = loginResult.getAccessToken();
Profile profile = Profile.getCurrentProfile();

if(profile != null ){
// mTextDetail.setText(profile.getFirstName());
// mTextAccessToken.setText(profile.getLastName());

Intent intent = new Intent(login.this,Home.class);
startActivity(intent);

}

}

@Override
public void onCancel() {

}

@Override
public void onError(FacebookException error) {

}
};





@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);

Log.i("Looks like it work","Looks like it work");
findViewById(R.id.sign_in_button).setOnClickListener(this);

findViewById(R.id.sign_in_button).setOnClickListener(this);
//findViewById(R.id.sign_out_button).setOnClickListener(this);
//findViewById(R.id.disconnect_button).setOnClickListener(this);

// [START configure_signin]
// Configure sign-in to request the user's ID, email address, and basic
// profile. ID and basic profile are included in DEFAULT_SIGN_IN.
GoogleSignInOptions gso = new GoogleSignInOptions.Builder(GoogleSignInOptions.DEFAULT_SIGN_IN)
.requestEmail()
.build();
// [END configure_signin]

// [START build_client]
// Build a GoogleApiClient with access to the Google Sign-In API and the
// options specified by gso.
mGoogleApiClient = new GoogleApiClient.Builder(this)
.enableAutoManage(this /* FragmentActivity */, this /* OnConnectionFailedListener */)
.addApi(Auth.GOOGLE_SIGN_IN_API, gso)
.build();
// [END build_client]

// [START customize_button]
// Customize sign-in button. The sign-in button can be displayed in
// multiple sizes and color schemes. It can also be contextually
// rendered based on the requested scopes. For example. a red button may
// be displayed when Google+ scopes are requested, but a white button
// may be displayed when only basic profile is requested. Try adding the
// Scopes.PLUS_LOGIN scope to the GoogleSignInOptions to see the
// difference.
SignInButton signInButton = (SignInButton) findViewById(R.id.sign_in_button);
signInButton.setSize(SignInButton.SIZE_STANDARD);
signInButton.setScopes(gso.getScopeArray());
// [END customize_button]







FacebookSdk.sdkInitialize(getApplicationContext());
AppEventsLogger.activateApp(this);
setContentView(R.layout.activity_login);

mButton = (Button) findViewById(R.id.but_goToInterest);

mButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {

Intent intent = new Intent(login.this, InterestScreen2.class);
startActivity(intent);
// Intent intent = new Intent(login.this,Home.class);
// startActivity(intent);
}
});

//Hide the Toolbar
getSupportActionBar().hide();

//Initialize Facebook SDK



loginButton = (LoginButton) findViewById(R.id.login_button);
loginButton.setReadPermissions("user_friends");
callbackManager = CallbackManager.Factory.create();

loginButton.registerCallback(callbackManager,mCallback);



}

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
callbackManager.onActivityResult(requestCode,resultCode,data);
}

@Override
public void onConnectionFailed(ConnectionResult connectionResult) {

}

private void signIn() {
Intent signInIntent = Auth.GoogleSignInApi.getSignInIntent(mGoogleApiClient);
startActivityForResult(signInIntent, RC_SIGN_IN);
}


@Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.sign_in_button:
signIn();
break;
//case R.id.sign_out_button:
// signOut();
// break;
//case R.id.disconnect_button:
// revokeAccess();
// break;
}
}
}

错误是:

Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.view.View.setOnClickListener(android.view.View$OnClickListener)' on a null object reference

我在这里做错了什么?

最佳答案

在此声明之后

signInButton.setScopes(gso.getScopeArray());

为signInButton放置一个点击监听器

signInButton.setOnClickListener(this);

以及此声明之后

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);

放这个

 setContentView(R.layout.activity_login);

也删除这些语句。

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

关于java - 创建 Google 登录时出错,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37092306/

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