java.lang.RuntimeException | java.lang.NullPointerException Google登录

时间:2017-08-24 21:19:55

标签: java android google-signin

我目前正在尝试将Google登录到我的应用程序中,当我按下登录谷歌意图打开并且我可以选择一个谷歌帐户但现在每次按下按钮时它都会崩溃...我试过我发现的解决方案互联网,但他们没有帮助我..按下"登录"我有以下错误:

E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.name, PID: 14299
java.lang.RuntimeException: Failure delivering result ResultInfo{who=null, request=9001, result=-1, data=Intent { (has extras) }} to activity {com.name/com.name.LoginActivity}: java.lang.NullPointerException: Attempt to invoke virtual method 'java.lang.String android.net.Uri.toString()' on a null object reference
at android.app.ActivityThread.deliverResults(ActivityThread.java:4053)
at android.app.ActivityThread.handleSendResult(ActivityThread.java:4096)
at android.app.ActivityThread.-wrap20(ActivityThread.java)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1516)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:154)
at android.app.ActivityThread.main(ActivityThread.java:6077)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:866)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:756)
 Causeed by: java.lang.NullPointerException: Attempt to invoke virtual method 'java.lang.String android.net.Uri.toString()' on a null object reference
at com.name.LoginActivity.handleSignInResult(LoginActivity.java:125)
at com.name.LoginActivity.onActivityResult(LoginActivity.java:112)
at android.app.Activity.dispatchActivityResult(Activity.java:6915)
at android.app.ActivityThread.deliverResults(ActivityThread.java:4049)
at android.app.ActivityThread.handleSendResult(ActivityThread.java:4096) 
at android.app.ActivityThread.-wrap20(ActivityThread.java) 
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1516) 
at android.os.Handler.dispatchMessage(Handler.java:102) 
at android.os.Looper.loop(Looper.java:154) 
at android.app.ActivityThread.main(ActivityThread.java:6077) 
at java.lang.reflect.Method.invoke(Native Method) 
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:866) 
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:756) 

我的方法是:

public class LoginActivity extends AppCompatActivity implements View.OnClickListener, GoogleApiClient.OnConnectionFailedListener {

private static final String TAG = "SignInActivity";
private static final int RC_SIGN_IN = 9001;

private TextView mStatusTextView;
private ProgressDialog mProgressDialog;

private TextView Name;
private TextView Email;
private ImageView ProfilePic;
private RelativeLayout Account_layout;
private SignInButton signInButton;
private Button signOutButton;
private GoogleApiClient mGoogleApiClient;

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

    //findViewById(R.id.sign_in_button).setOnClickListener((View.OnClickListener) this);


    final PacketBuilder pb = new PacketBuilder();
    final Context context = this;
    signInButton = (SignInButton) findViewById(R.id.sign_in_button);
    signInButton.setSize(SignInButton.SIZE_STANDARD);
    signOutButton = (Button) findViewById(R.id.sign_out_button);
    Name = (TextView) findViewById(R.id.account_name);
    Email = (TextView) findViewById(R.id.account_email);
    ProfilePic = (ImageView) findViewById(R.id.account_picture);
    Account_layout = (RelativeLayout) findViewById(R.id.account_layout);
    Account_layout.setVisibility(View.GONE);

    signInButton.setOnClickListener(this);
    signOutButton.setOnClickListener(this);


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

    mGoogleApiClient = new GoogleApiClient.Builder(this).enableAutoManage(this, this).addApi(Auth.GOOGLE_SIGN_IN_API, signInOptions).build();


}

@Override
public void onClick(View v) {
    switch (v.getId()) {
        case R.id.sign_in_button:
            signIn();
            break;
        case R.id.sign_out_button:
            signOut();
            break;
    }
}

private void signIn()
{
    Intent signInIntent = Auth.GoogleSignInApi.getSignInIntent(mGoogleApiClient);
    startActivityForResult(signInIntent, RC_SIGN_IN);
}

private void signOut()
{
    Auth.GoogleSignInApi.signOut(mGoogleApiClient).setResultCallback(new ResultCallback<Status>() {
        @Override
        public void onResult(@NonNull Status status) {
            updateUI(false);
        }
    });
}

@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
    super.onActivityResult(requestCode, resultCode, data);

    // Result returned from launching the Intent from GoogleSignInApi.getSignInIntent(...);

        if (requestCode == RC_SIGN_IN) {
            GoogleSignInResult result = Auth.GoogleSignInApi.getSignInResultFromIntent(data);
            handleSignInResult(result);
        }

}

private void handleSignInResult(GoogleSignInResult result) {
    Log.d(TAG, "handleSignInResult:" + result.isSuccess());
    if (result.isSuccess()) {
        // Signed in successfully, show authenticated UI.
        GoogleSignInAccount acc = result.getSignInAccount();

        String name = acc.getDisplayName();
        String email = acc.getEmail();
        String picture_url = acc.getPhotoUrl().toString();
        Name.setText(name);
        Email.setText(email);
        Glide.with(this).load(picture_url).into(ProfilePic);
        updateUI(true);

        //mStatusTextView.setText(getString(R.string.signed_in_fmt, acc.getDisplayName()));

    }
    else
    {
        // Signed out, show unauthenticated UI.
        updateUI(false);
    }
}

private void updateUI(boolean signedIn) {
    if (signedIn) {
        findViewById(R.id.sign_in_button).setVisibility(View.GONE);
        findViewById(R.id.account_layout).setVisibility(View.VISIBLE);
    } else {
        //mStatusTextView.setText(R.string.signed_out);

        findViewById(R.id.sign_in_button).setVisibility(View.VISIBLE);
        findViewById(R.id.account_layout).setVisibility(View.GONE);
    }
}

@Override
public void onConnectionFailed(@NonNull ConnectionResult connectionResult) {

}

我无法找到问题:(希望有人可以帮助我。

1 个答案:

答案 0 :(得分:0)

acc.getPhotoUrl().toString()替换为acc.getPhotoUrl() != null ? user.getPhotoUrl().toString() : null因为问题是从网址获取图片。

记住

getPhotoUrl ()获取已登录用户的照片网址。

它会返回Google帐户的照片网址。如果配置了requestProfile()且用户确实拥有Google+个人资料图片,则仅为非null。 请检查您的Google帐户是否有Google+个人资料图片。

P / S:有时,如果已经创建了Google+个人资料图片,但是在您的设备中添加Google帐户之后,您可能需要从设备中删除现有的Google帐户,然后重新添加。

希望这可行。