2014-12-04 32 views
0

在我的應用程序中,我使用Parse.com和Facebook登錄,並且Facebook登錄正在運行,現在我正在嘗試保存簽名用戶的電子郵件。我這樣做是這樣的:Android - Parse.com保存對象時出現NullPointerException

Request.newMeRequest(ParseFacebookUtils.getSession(), new Request.GraphUserCallback() { 

     // callback after Graph API response with user object 
     @Override 
     public void onCompleted(GraphUser user, Response response) { 
      if (user != null) { 
       String email = user.getProperty("email").toString(); 
       Log.i(TAG, email); 
       ParseUser currentUser = ParseUser.getCurrentUser(); 
       currentUser.put("email", email); 
       currentUser.saveInBackground(); 

      } 
     } 
    }).executeAsync() 

但是當我跑我得到java.lang.NullPointerException在該行currentUser.put("email", email);但我在此之前登錄電子郵件,它不爲空,我有解析我的User類電子郵件屬性,所以我不知道這是爲什麼?

這裏的代碼來設置FB登錄你應該需要它,它正在工作,但以防萬一。

public void onLoginClick(View v) { 
    progressDialog = ProgressDialog.show(LoginActivity.this, "", "Logging in...", true); 

    final List<String> permissions = Arrays.asList("public_profile", "email"); 
    // NOTE: for extended permissions, like "user_about_me", your app must be reviewed by the Facebook team 
    // (https://developers.facebook.com/docs/facebook-login/permissions/) 

    ParseFacebookUtils.logIn(permissions, this, new LogInCallback() { 
     @Override 
     public void done(ParseUser user, ParseException err) { 
      progressDialog.dismiss(); 
      if (user == null) { 
       Log.d(TAG, "Uh oh. The user cancelled the Facebook login."); 
      } else if (user.isNew()) { 
       Log.d(TAG, "User signed up and logged in through Facebook!"); 
       showSelectSchoolActivity(); 
      } else { 
       Log.d(TAG, "User logged in through Facebook!"); 
       showMainActivity(); 
      } 
     } 
    }); 

感謝您的幫助:)

+0

'Parser.getCurrentUser()'返回null? – RandomQuestion 2014-12-04 04:30:45

+0

嘗試將ParseUser代碼放入try-catch塊。 – 2014-12-04 04:32:22

+0

@RandomQuestion我加了這行'Log.i(TAG,currentUser.toString());'並且錯誤在於它,所以我認爲你是對的,我該如何解決這個問題? – iqueqiorio 2014-12-04 04:33:52

回答

0

檢查你應該總是使用響應(ERR == NULL)作爲成功的條件。只有在您收到來自服務器的響應並且err == null後,您纔可以檢查當前用戶。你也應該檢查何時err!= null。然後從err.getCode()獲取錯誤消息並檢查錯誤代碼是什麼。

+0

有沒有err變量? – iqueqiorio 2014-12-04 04:31:59

相關問題