簡體   English   中英

保存G +照片網址時,應用崩潰

[英]App crashes while saving G+ photo url

用戶嘗試使用Google帳戶登錄后,我正在嘗試保存Google電子郵件,姓名和個人資料照片網址。 當使用google默認個人資料圖片的用戶登錄時,該應用程序將崩潰。 它適用於具有自定義個人資料照片的Google帳戶

以下是相關代碼:

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);
        }
    }

這是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

行號 207調用handleSignResult()

行號 179是String pic_url = new String(account.getPhotoUrl().toString());

注釋掉第179行可以解決該問題,但是隨后我無法保存需要在應用中顯示用戶個人資料照片的照片網址。

該錯誤僅發生在其Google帳戶上具有默認個人資料照片的用戶。

我刪除了Google plus照片進行測試。 我簽名時應用崩潰了。 然后我從chrome瀏覽器在我的google plus帳戶上檢查了照片網址,並獲得了該網址https://lh3.googleusercontent.com/-eKoJemxhXNk/AAAAAAAAAAI/AAAAAAAAAAA/ACnBePZ9iFrmxpc1l5CPyKJ4DRH14A_Scg/s60-p-rw-no-mo/photo。 jpg

該網址類似於使用具有自定義個人資料照片的帳戶登錄時保存在shared_pref.xml中的網址。 上面的網址是getPhotoUrl()必須返回的內容。 它絕對不是null,但仍然在getPhotoUrl()。toString()上得到NPE,並且應用程序崩潰。

有人可以解釋為什么會這樣嗎?

錯誤可以通過以下簡單技巧解決:

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

尋找getPhotoUrl()方法后,我發現了這個

我已經使用DEFAULT_SIGN_IN標志配置了登錄,但第一個條件即

如果用戶有個人資料圖片

等同於用戶具有默認的個人資料圖片 因此,具有默認個人資料圖片的所有用戶均不滿足第一個條件,因此方法返回null。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM