2013-03-16 134 views
0

我通過Android的Facebook教程在這裏工作:https://developers.facebook.com/docs/howtos/androidsdk/3.0/fetch-user-data/Android的Facebook的API - StringBuilder的返回NULL

在此塊的代碼:

private String buildUserInfoDisplay(GraphUser user) { 

     StringBuilder userInfo = new StringBuilder(""); 
     Log.d("Graphuser:user",user.toString()); 
     Log.d("StringBuilder:userInfo",userInfo.toString()); 
     // Example: typed access (name) 
     // - no special permissions required 
     Log.d("Name: ",String.format("Name: %s\n\n", user.getName())); 

     userInfo.append(String.format("Name: %s\n\n", user.getName())); 

     // Example: typed access (birthday) 
     // - requires user_birthday permission 
     //userInfo.append(String.format("Birthday: %s\n\n", user.getBirthday())); 

     // Example: partially typed access, to location field, 
     // name key (location) 
     // - requires user_location permission 
     //userInfo.append(String.format("Location: %s\n\n", user.getLocation().getProperty("name"))); 

     // Example: access via property name (locale) 
     // - no special permissions required 
     userInfo.append(String.format("Locale: %s\n\n", user.getProperty("locale"))); 

    // Get a list of languages from an interface that 
    // extends the GraphUser interface and that returns 
    // a GraphObject list of MyGraphLanguage objects. 
    GraphObjectList<MyGraphLanguage> languages = (user.cast(MyGraphUser.class)).getLanguages(); 
    Log.d("Languages",languages.toString()); 
    if (languages.size() > 0) { 
     ArrayList<String> languageNames = new ArrayList<String>(); 
     // Iterate through the list of languages 
     for (MyGraphLanguage language : languages) { 
      // Add the language name to a list. Use the name 
      // getter method to get access to the name field. 
      languageNames.add(language.getName()); 
     }      

     //userInfo.append(String.format("Languages: %s\n\n", languageNames.toString())); 
    } 

     return userInfo.toString(); 
    } 

userInfo.append()函數的結果空指針異常。 有沒有人遇到過這個?我已經檢查過我有和本教程中相同的代碼。

/*更新 - 附加的logcat */

03-16 17:59:18.154: W/dalvikvm(1189): threadid=1: thread exiting with uncaught exception (group=0x40a13300) 
03-16 17:59:18.204: E/AndroidRuntime(1189): FATAL EXCEPTION: main 
03-16 17:59:18.204: E/AndroidRuntime(1189): java.lang.NullPointerException 
03-16 17:59:18.204: E/AndroidRuntime(1189):  at net.myname.myapp.MainFragment.buildUserInfoDisplay(MainFragment.java:84) 
03-16 17:59:18.204: E/AndroidRuntime(1189):  at net.myname.myapp.MainFragment.access$2(MainFragment.java:56) 
03-16 17:59:18.204: E/AndroidRuntime(1189):  at net.myname.myapp.MainFragment$2.onCompleted(MainFragment.java:125) 
03-16 17:59:18.204: E/AndroidRuntime(1189):  at com.facebook.Request$1.onCompleted(Request.java:264) 
03-16 17:59:18.204: E/AndroidRuntime(1189):  at com.facebook.Request$4.run(Request.java:1240) 
03-16 17:59:18.204: E/AndroidRuntime(1189):  at android.os.Handler.handleCallback(Handler.java:615) 
03-16 17:59:18.204: E/AndroidRuntime(1189):  at android.os.Handler.dispatchMessage(Handler.java:92) 
03-16 17:59:18.204: E/AndroidRuntime(1189):  at android.os.Looper.loop(Looper.java:137) 
03-16 17:59:18.204: E/AndroidRuntime(1189):  at android.app.ActivityThread.main(ActivityThread.java:4745) 
03-16 17:59:18.204: E/AndroidRuntime(1189):  at java.lang.reflect.Method.invokeNative(Native Method) 
03-16 17:59:18.204: E/AndroidRuntime(1189):  at java.lang.reflect.Method.invoke(Method.java:511) 
03-16 17:59:18.204: E/AndroidRuntime(1189):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:786) 
03-16 17:59:18.204: E/AndroidRuntime(1189):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553) 
03-16 17:59:18.204: E/AndroidRuntime(1189):  at dalvik.system.NativeStart.main(Native Method) 
03-16 17:59:18.354: D/dalvikvm(1189): GC_CONCURRENT freed 423K, 6% free 8428K/8903K, paused 107ms+76ms, total 316ms 
+0

哪個userInfo.append?我想說user.getProperty(「locale」)在這裏可能會因爲某些原因返回null。我還沒有嘗試過。我只是快速瀏覽一下這個問題:) – 2013-03-16 21:53:37

+0

在這裏添加你的logcat。 – Sajmon 2013-03-16 21:58:01

+0

@Sajmon - 增加了logcat – jwBurnside 2013-03-16 22:04:17

回答

1

當用戶沒有提供在Facebook上的信息的代碼失敗。用try和catch封裝代碼java.lang.NullPointerException

0

這可能是一些關係到你的buildUserInfoDisplay的線程安全的,因爲StringBuilder的不應該失敗這樣。可能有不同的線程同時訪問函數,這導致了NPE(StringBuilder內部?)。

嘗試同步該方法或使用StringBuffer而不是StringBuilder,這是線程安全的。

詩:哇,這就像寫榮耀:)

+0

嘿,對不起Kap,當我測試你的建議時,我實際上不知不覺地評論了有問題的代碼行,所以當我確認你的解決方案時我跳過了槍。 – jwBurnside 2013-03-22 02:05:36

+0

沒問題,很高興你解決了! – 2013-03-22 03:09:06

相關問題