我正在为android创建移动应用程序,但在Google下载连接后遇到问题
应用程序崩溃。有人可以给我一个理由以及如何附加吗?
主要活动触手可及。
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);
}
}
}
登录后的目标活动:
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();
}
});
}
}
例外:
E / Android运行时:致命异常:主要 流程:com.example.goodmath,PID:1780 java.lang.RuntimeException:无法启动活动ComponentInfo {com.example.goodmath / com.example.goodmath.MenuActivity}: java.lang.NullPointerException:尝试调用虚拟方法'void android.widget.Button.setOnClickListener(android.view.View $ OnClickListener)' 在空对象引用上 在android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2913) 在android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3048) 在android.app.servertransaction.LaunchActivityItem.execute(LaunchActivityItem.java:78) 在android.app.servertransaction.TransactionExecutor.executeCallbacks(TransactionExecutor.java:108) 在android.app.servertransaction.TransactionExecutor.execute(TransactionExecutor.java:68) 在android.app.ActivityThread $ H.handleMessage(ActivityThread.java:1808) 在android.os.Handler.dispatchMessage(Handler.java:106) 在android.os.Looper.loop(Looper.java:193) 在android.app.ActivityThread.main(ActivityThread.java:6669) 在java.lang.reflect.Method.invoke(本机方法) 在com.android.internal.os.RuntimeInit $ MethodAndArgsCaller.run(RuntimeInit.java:493) 在com.android.internal.os.ZygoteInit.main(ZygoteInit.java:858) 原因:java.lang.NullPointerException:尝试调用虚拟方法'void android.widget.Button.setOnClickListener(android.view.View $ OnClickListener)' 在空对象引用上 在com.example.goodmath.MenuActivity.onCreate(MenuActivity.java:46) 在android.app.Activity.performCreate(Activity.java:7136) 在android.app.Activity.performCreate(Activity.java:7127) 在android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1271) 在android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2893) 在android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3048) 在android.app.servertransaction.LaunchActivityItem.execute(LaunchActivityItem.java:78) 在android.app.servertransaction.TransactionExecutor.executeCallbacks(TransactionExecutor.java:108) 在android.app.servertransaction.TransactionExecutor.execute(TransactionExecutor.java:68) 在android.app.ActivityThread $ H.handleMessage(ActivityThread.java:1808) 在android.os.Handler.dispatchMessage(Handler.java:106) 在android.os.Looper.loop(Looper.java:193) 在android.app.ActivityThread.main(ActivityThread.java:6669) 在java.lang.reflect.Method.invoke(本机方法) 在com.android.internal.os.RuntimeInit $ MethodAndArgsCaller.run(RuntimeInit.java:493) 在com.android.internal.os.ZygoteInit.main(ZygoteInit.java:858) I / Process:正在发送信号。 PID:1780 SIG:9
答案 0 :(得分:1)
您不会在setContentView()
的{{1}}中调用onCreate()
,因此您的MenuActivity
查找将失败。结果,findViewById()
是logoutBtn
,因此当您尝试在其上调用方法时,会崩溃null
。
答案 1 :(得分:1)
实际错误是:
java.lang.NullPointerException: Attempt to invoke virtual method 'void
android.widget.Button.setOnClickListener(android.view.View$OnClickListener)'
on a null object
因此findViewById失败。
在super.onCreate()之后直接进行setContentView(R.layout.activity_menu)
如果这行不通,您确定所引用的视图在activity_menu布局文件中吗?