Android Google登录按钮导致空对象引用
我试图实现一个Google登录按钮,当我调用Android Google登录按钮导致空对象引用,android,Android,我试图实现一个Google登录按钮,当我调用signInButton.setSize()时,得到一个空对象引用错误。我使用findViewById()初始化按钮。我在promptSignIn()中执行此操作,这是一个比onCreate()更高的两个堆栈级别。正如其他答案所示,我尝试在onCreate()中初始化,但得到了相同的错误 YourOrdersActivity.java package com.example.mattf.yankeetoyboxv1; import android.c
signInButton.setSize()
时,得到一个空对象引用错误。我使用findViewById()
初始化按钮。我在promptSignIn()
中执行此操作,这是一个比onCreate()
更高的两个堆栈级别。正如其他答案所示,我尝试在onCreate()
中初始化,但得到了相同的错误
YourOrdersActivity.java
package com.example.mattf.yankeetoyboxv1;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import com.google.android.gms.auth.api.signin.GoogleSignIn;
import com.google.android.gms.auth.api.signin.GoogleSignInAccount;
import com.google.android.gms.auth.api.signin.GoogleSignInClient;
import com.google.android.gms.auth.api.signin.GoogleSignInOptions;
import com.google.android.gms.common.SignInButton;
import com.google.android.gms.tasks.Task;
/**
* Created by mattf on 12/8/2017.
*/
public class SignInActivity extends BaseActivity{
private GoogleSignInClient mGoogleSignInClient;
private SignInButton signInButton;
private static int RC_SIGN_IN=100;
private static final String TAG="SignInActivity";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
isUserSignedIn();
}
@Override
protected void onStart() {
super.onStart();
isUserSignedIn();
}
@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode,resultCode,data);
//Result return from launching the Intent from GoogleSignInClient.getSignInIntent(...);
if (requestCode == RC_SIGN_IN) {
// The Task returned from this call is always completed
// no need to attach a listener
Task<GoogleSignInAccount> task = GoogleSignIn.getSignedInAccountFromIntent(data);
handleSignInResult(task);
}
}
private void isUserSignedIn() {
GoogleSignInAccount account = GoogleSignIn.getLastSignedInAccount(this);
if (account==null){
promptSignIn();
}
else updateUI(account);
}
private void promptSignIn() {
// 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();
// Build a GoogleSignInClient with the options specified by gso
mGoogleSignInClient = GoogleSignIn.getClient(this, gso);
// Set the dimensions of the sign-in button
signInButton = findViewById(R.id.sign_in_button);
signInButton.setSize(SignInButton.SIZE_STANDARD);
signInButton.setOnClickListener(new View.OnClickListener(){
@Override
public void onClick(View view) {
signIn();
}
});
}
private void signIn() {
Intent signInIntent = mGoogleSignInClient.getSignInIntent();
startActivityForResult(signInIntent,RC_SIGN_IN);
}
private void handleSignInResult(Task<GoogleSignInAccount> completedTask) {
try {
GoogleSignInAccount account = completedTask.getResult();
// Signed in successfully, show authenticated UI
updateUI(account);
} catch (Exception e) {
Log.w(TAG, "signInResult:failed code=" + e.getMessage());
}
}
protected void updateUI(GoogleSignInAccount account) {
if (signInButton != null) signInButton.setVisibility(View.GONE);
}
}
LogCat
12-08 20:31:51.624 12373-12373/com.example.mattf.yankeetoyboxv1 E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.example.mattf.yankeetoyboxv1, PID: 12373
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.mattf.yankeetoyboxv1/com.example.mattf.yankeetoyboxv1.YourOrdersActivity}: java.lang.NullPointerException: Attempt to invoke virtual method 'void com.google.android.gms.common.SignInButton.setSize(int)' on a null object reference
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2778)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2856)
at android.app.ActivityThread.-wrap11(Unknown Source:0)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1589)
at android.os.Handler.dispatchMessage(Handler.java:106)
at android.os.Looper.loop(Looper.java:164)
at android.app.ActivityThread.main(ActivityThread.java:6494)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:438)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:807)
Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'void com.google.android.gms.common.SignInButton.setSize(int)' on a null object reference
at com.example.mattf.yankeetoyboxv1.SignInActivity.promptSignIn(SignInActivity.java:72)
at com.example.mattf.yankeetoyboxv1.SignInActivity.isUserSignedIn(SignInActivity.java:57)
at com.example.mattf.yankeetoyboxv1.SignInActivity.onCreate(SignInActivity.java:30)
at android.app.Activity.performCreate(Activity.java:7000)
at android.app.Activity.performCreate(Activity.java:6991)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1214)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2731)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2856)
at android.app.ActivityThread.-wrap11(Unknown Source:0)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1589)
at android.os.Handler.dispatchMessage(Handler.java:106)
at android.os.Looper.loop(Looper.java:164)
at android.app.ActivityThread.main(ActivityThread.java:6494)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:438)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:807)
您没有在
onCreate
方法中调用setContentView(R.layout.your\u orders\u活动)
您的onCreate
应该如下所示:
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.your_orders_activity);
isUserSignedIn();
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.your_orders_activity);
isUserSignedIn();
}