gpt4 book ai didi

java - 无法启动 Activity ComponentInfo - Android

转载 作者:行者123 更新时间:2023-12-02 01:07:47 27 4
gpt4 key购买 nike

我正在为 Android 创建移动应用程序,在通过 Google 下载连接后遇到问题

应用程序崩溃。有人能给我一个理由以及如何附加它吗?

力所能及的主要 Activity 。

public class MainActivity extends AppCompatActivity {
GoogleSignInClient mGoogleSignInClient;
private int RC_SIGN_IN = 3;
SignInButton signInButton;

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

signInButton = findViewById(R.id.sign_in_button);

signInButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
switch (view.getId()) {
case R.id.sign_in_button:
signIn();
break;
// ...
}
}
});
GoogleSignInOptions gso = new GoogleSignInOptions.Builder(GoogleSignInOptions.DEFAULT_SIGN_IN)
.requestEmail()
.build();

mGoogleSignInClient = GoogleSignIn.getClient(this, gso);

}
private void signIn() {
Intent signInIntent = mGoogleSignInClient.getSignInIntent();
startActivityForResult(signInIntent, RC_SIGN_IN);
}
@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
// Result returned 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 handleSignInResult(Task<GoogleSignInAccount> completedTask) {
try {
GoogleSignInAccount account = completedTask.getResult(ApiException.class);

Intent intent = new Intent(MainActivity.this, MenuActivity.class);
startActivity(intent);
} catch (ApiException e) {
// The ApiException status code indicates the detailed failure reason.
// Please refer to the GoogleSignInStatusCodes class reference for more information.
Log.w("TAG", "signInResult:failed code=" + e.getStatusCode());
// updateUI(null);
}
}

}

登录后的目标 Activity :

public class MenuActivity extends AppCompatActivity  {

GoogleSignInClient mGoogleSignInClient;
Button logoutBtn;
TextView userName;
ImageView profileImage;
private GoogleSignInOptions gso;

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

GoogleSignInOptions gso = new GoogleSignInOptions.Builder(GoogleSignInOptions.DEFAULT_SIGN_IN)
.requestEmail()
.build();

mGoogleSignInClient = GoogleSignIn.getClient(this, gso);

logoutBtn=(Button)findViewById(R.id.button_wyl);
profileImage=(ImageView)findViewById(R.id.profileImage);
userName = findViewById(R.id.name);
logoutBtn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
switch (view.getId()) {
// ...
case R.id.button_wyl:
signOut();
break;
// ...
}
}
});

GoogleSignInAccount acct = GoogleSignIn.getLastSignedInAccount(this);
if (acct != null) {
String personName = acct.getDisplayName();
Uri personPhoto = acct.getPhotoUrl();

userName.setText(personName);
Glide.with(this).load(String.valueOf(personPhoto)).into(profileImage);

}
}

private void signOut() {
mGoogleSignInClient.signOut()
.addOnCompleteListener(this, new OnCompleteListener<Void>() {
@Override
public void onComplete(@NonNull Task<Void> task) {
Toast.makeText(MenuActivity.this, "Signed out Successfully", Toast.LENGTH_LONG).show();
finish();
}
});
}

}

异常(exception):

E/AndroidRuntime: FATAL EXCEPTION: main Process: com.example.goodmath, PID: 1780 java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.goodmath/com.example.goodmath.MenuActivity}: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.Button.setOnClickListener(android.view.View$OnClickListener)' on a null object reference at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2913) at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3048) at android.app.servertransaction.LaunchActivityItem.execute(LaunchActivityItem.java:78) at android.app.servertransaction.TransactionExecutor.executeCallbacks(TransactionExecutor.java:108) at android.app.servertransaction.TransactionExecutor.execute(TransactionExecutor.java:68) at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1808) at android.os.Handler.dispatchMessage(Handler.java:106) at android.os.Looper.loop(Looper.java:193) at android.app.ActivityThread.main(ActivityThread.java:6669) at java.lang.reflect.Method.invoke(Native Method) at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:493) at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:858) Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.Button.setOnClickListener(android.view.View$OnClickListener)' on a null object reference at com.example.goodmath.MenuActivity.onCreate(MenuActivity.java:46) at android.app.Activity.performCreate(Activity.java:7136) at android.app.Activity.performCreate(Activity.java:7127) at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1271) at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2893) at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3048)  at android.app.servertransaction.LaunchActivityItem.execute(LaunchActivityItem.java:78)  at android.app.servertransaction.TransactionExecutor.executeCallbacks(TransactionExecutor.java:108)  at android.app.servertransaction.TransactionExecutor.execute(TransactionExecutor.java:68)  at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1808)  at android.os.Handler.dispatchMessage(Handler.java:106)  at android.os.Looper.loop(Looper.java:193)  at android.app.ActivityThread.main(ActivityThread.java:6669)  at java.lang.reflect.Method.invoke(Native Method)  at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:493)  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:858)  I/Process: Sending signal. PID: 1780 SIG: 9

最佳答案

您没有在 MenuActivityonCreate() 中调用 setContentView(),因此您的 findViewById()查找将会失败。因此,logoutBtnnull,因此当您尝试调用其方法时,会因 NullPointerException 崩溃。

关于java - 无法启动 Activity ComponentInfo - Android,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59800597/

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