简体   繁体   English

保存G +照片网址时,应用崩溃

[英]App crashes while saving G+ photo url

I am trying to save google email, name and profile photo url after a user signs in with Google account. 用户尝试使用Google帐户登录后,我正在尝试保存Google电子邮件,姓名和个人资料照片网址。 When a user with google's default profile picture signs in, the app crashes. 当使用google默认个人资料图片的用户登录时,该应用程序将崩溃。 It works with google accounts having a custom profile photo 它适用于具有自定义个人资料照片的Google帐户

Here's the relevant code: 以下是相关代码:

private void handleSignInResult(GoogleSignInResult result){

        Log.d(TAG,"handleSignInResult():"+result.isSuccess());
        if(result.isSuccess()){
            GoogleSignInAccount account = result.getSignInAccount();
            String pname = account.getDisplayName();
            String emailid = account.getEmail();
            String pic_url = new String(account.getPhotoUrl().toString());

            SharedPreferences userInfo = context.getSharedPreferences(getString(R.string.USER_INFO),Context.MODE_PRIVATE);
            SharedPreferences.Editor editor = userInfo.edit();
            editor.putString("name",pname);
            editor.putString("email",emailid);
            editor.putString("pic_url",pic_url);
            editor.commit();
            updateUI(true);
        } else {
            updateUI(false);
        }
    }

Here's is the part of logcat with exception: 这是logcat的一部分,有例外:

--------- beginning of crash
10-16 20:02:33.731 32355 32355 E AndroidRuntime: FATAL EXCEPTION: main
10-16 20:02:33.731 32355 32355 E AndroidRuntime: Process: org.csikjsce.csi_kjsceofficial, PID: 32355
10-16 20:02:33.731 32355 32355 E AndroidRuntime: java.lang.RuntimeException: Failure delivering result ResultInfo{who=null, request=7, result=-1, data=Intent { (has extras) }} to activity {org.csikjsce.csi_kjsceofficial/org.csikjsce.csi_kjsceofficial.LoginActivity}: java.lang.NullPointerException: Attempt to invoke virtual method 'java.lang.String android.net.Uri.toString()' on a null object reference
10-16 20:02:33.731 32355 32355 E AndroidRuntime:    at android.app.ActivityThread.deliverResults(ActivityThread.java:4112)
10-16 20:02:33.731 32355 32355 E AndroidRuntime:    at android.app.ActivityThread.handleSendResult(ActivityThread.java:4155)
10-16 20:02:33.731 32355 32355 E AndroidRuntime:    at android.app.ActivityThread.access$1600(ActivityThread.java:186)
10-16 20:02:33.731 32355 32355 E AndroidRuntime:    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1658)
10-16 20:02:33.731 32355 32355 E AndroidRuntime:    at android.os.Handler.dispatchMessage(Handler.java:111)
10-16 20:02:33.731 32355 32355 E AndroidRuntime:    at android.os.Looper.loop(Looper.java:238)
10-16 20:02:33.731 32355 32355 E AndroidRuntime:    at android.app.ActivityThread.main(ActivityThread.java:6016)
10-16 20:02:33.731 32355 32355 E AndroidRuntime:    at java.lang.reflect.Method.invoke(Native Method)
10-16 20:02:33.731 32355 32355 E AndroidRuntime:    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:937)
10-16 20:02:33.731 32355 32355 E AndroidRuntime:    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:798)
10-16 20:02:33.731 32355 32355 E AndroidRuntime: Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'java.lang.String android.net.Uri.toString()' on a null object reference
10-16 20:02:33.731 32355 32355 E AndroidRuntime:    at org.csikjsce.csi_kjsceofficial.LoginActivity.handleSignInResult(LoginActivity.java:179)
10-16 20:02:33.731 32355 32355 E AndroidRuntime:    at org.csikjsce.csi_kjsceofficial.LoginActivity.onActivityResult(LoginActivity.java:207)
10-16 20:02:33.731 32355 32355 E AndroidRuntime:    at android.app.Activity.dispatchActivityResult(Activity.java:6657)
10-16 20:02:33.731 32355 32355 E AndroidRuntime:    at android.app.ActivityThread.deliverResults(ActivityThread.java:4108)
10-16 20:02:33.731 32355 32355 E AndroidRuntime:    ... 9 more

Line no. 行号 207 calls handleSignResult() 207调用handleSignResult()

Line no. 行号 179 is String pic_url = new String(account.getPhotoUrl().toString()); 179是String pic_url = new String(account.getPhotoUrl().toString());

Commenting out line 179 fixes the issue but then I cannot save photo url that I need to show user's profile photo in my app. 注释掉第179行可以解决该问题,但是随后我无法保存需要在应用中显示用户个人资料照片的照片网址。

The error occurs only with users that have the default profile photo on their google account. 该错误仅发生在其Google帐户上具有默认个人资料照片的用户。

I deleted my google plus photo to test it. 我删除了Google plus照片进行测试。 The app crashed when I signed. 我签名时应用崩溃了。 Then I checked the photo url on my google plus account from chrome browser and I got this url https://lh3.googleusercontent.com/-eKoJemxhXNk/AAAAAAAAAAI/AAAAAAAAAAA/ACnBePZ9iFrmxpc1l5CPyKJ4DRH14A_Scg/s60-p-rw-no-mo/photo.jpg 然后我从chrome浏览器在我的google plus帐户上检查了照片网址,并获得了该网址https://lh3.googleusercontent.com/-eKoJemxhXNk/AAAAAAAAAAI/AAAAAAAAAAA/ACnBePZ9iFrmxpc1l5CPyKJ4DRH14A_Scg/s60-p-rw-no-mo/photo。 jpg

The url is similar to the one that gets saved in shared_pref.xml when signing in with an account that has a custom profile photo. 该网址类似于使用具有自定义个人资料照片的帐户登录时保存在shared_pref.xml中的网址。 The above url is what the getPhotoUrl() must return. 上面的网址是getPhotoUrl()必须返回的内容。 Its definitely not null, but still I get NPE on getPhotoUrl().toString() and app crashes. 它绝对不是null,但仍然在getPhotoUrl()。toString()上得到NPE,并且应用程序崩溃。

Can someone explain why this is happening? 有人可以解释为什么会这样吗?

Error is solved with this simple trick: 错误可以通过以下简单技巧解决:

String pic_url = ""+account.getPhotoUrl();

After hunting down for getPhotoUrl() method I found this 寻找getPhotoUrl()方法后,我发现了这个

I had configured the sign in with DEFAULT_SIGN_IN flag but the first condition ie, 我已经使用DEFAULT_SIGN_IN标志配置了登录,但第一个条件即

if user has a profile picture 如果用户有个人资料图片

is equivalent to user has default profile picture . 等同于用户具有默认的个人资料图片 So all users with default profile picture do not meet the first condition, thus method returns null. 因此,具有默认个人资料图片的所有用户均不满足第一个条件,因此方法返回null。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM