我正在尝试这段代码,它可以成功构建,但立即崩溃。
错误显示:
java.lang.RuntimeException:无法启动活动 ComponentInfo{com.example.logintest/com.example.logintest.LoginActivity}:java.lang.NullPointerException:尝试调用虚拟方法 'android.view.View com.google .android.gms.common.SignInButton.findViewById(int)' 在一个空对象引用上。
它说它是由:
引起:java.lang.NullPointerException:尝试在空对象引用上调用虚拟方法“android.view.View com.google.android.gms.common.SignInButton.findViewById(int)”
试了很久还是不知道怎么解决,求各位大侠帮忙,谢谢!
LoginActivity.java 代码如下:
REPLACE ("HELLO", "L", "$0hi")
和错误代码:
public class LoginActivity extends AppCompatActivity {
public static final int GOOGLE_SIGN_IN_CODE = 10005;
SignInButton signIn;
GoogleSignInOptions gso;
GoogleSignInClient signInClient;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_login);
signIn.findViewById(R.id.signIn);
gso = new GoogleSignInOptions.Builder(GoogleSignInOptions.DEFAULT_SIGN_IN)
.requestEmail()
.build();
signInClient = GoogleSignIn.getClient(this,gso);
GoogleSignInAccount signInAccount = GoogleSignIn.getLastSignedInAccount(this);
if(signInAccount != null) {
startActivity(new Intent(this,MainActivity.class));
}
signIn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Intent sign = signInClient.getSignInIntent();
startActivityForResult(sign, GOOGLE_SIGN_IN_CODE);
}
});
}
}
activity_login.xml 的一部分:
2021-05-28 21:37:33.617 10526-10526/com.example.logintest E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.example.logintest, PID: 10526
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.logintest/com.example.logintest.LoginActivity}: java.lang.NullPointerException: Attempt to invoke virtual method 'android.view.View com.google.android.gms.common.SignInButton.findViewById(int)' on a null object reference
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2957)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3032)
at android.app.ActivityThread.-wrap11(Unknown Source:0)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1696)
at android.os.Handler.dispatchMessage(Handler.java:105)
at android.os.Looper.loop(Looper.java:164)
at android.app.ActivityThread.main(ActivityThread.java:6944)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.Zygote$MethodAndArgsCaller.run(Zygote.java:327)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1374)
Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'android.view.View com.google.android.gms.common.SignInButton.findViewById(int)' on a null object reference
at com.example.logintest.LoginActivity.onCreate(LoginActivity.java:29)
at android.app.Activity.performCreate(Activity.java:7183)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1220)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2910)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3032)
at android.app.ActivityThread.-wrap11(Unknown Source:0)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1696)
at android.os.Handler.dispatchMessage(Handler.java:105)
at android.os.Looper.loop(Looper.java:164)
at android.app.ActivityThread.main(ActivityThread.java:6944)