开发者社区 问答 正文

无法启动活动ComponentInfo-Android

我正在为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();
                }
            });
}
}

例外:

在com.android.internal.os.RuntimeInit $ MethodAndArgsCaller处的java.lang.reflect.Method.invoke(Native Method)在android.app.ActivityThread.main(ActivityThread.java:6669)处循环(Looper.java:193)。在com.android.internal.os.ZygoteInit.main(ZygoteInit.java:858)处运行(RuntimeInit.java:493)I / Process:正在发送信号。PID:1780 SIG:9

展开
收起
Puppet 2020-01-18 22:09:34 617 分享 版权
1 条回答
写回答
取消 提交回答
  • 你不叫setContentView()在onCreate()的MenuActivity,所以你的findViewById()查询将会失败。结果logoutBtn是is null,因此NullPointerException尝试在其上调用方法时会崩溃。

    2020-01-18 22:09:47
    赞同 展开评论
问答分类:
问答地址: