本文介绍了检查如果Facebook,Android的SDK调用之前的getProperty提供用户数据空值()的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我搜索我找不到这个问题的任何解决方案。
我已经做了Facebook SDK 3.5图形请求。
当我尝试开始使用的getProperty(PROPERTY_NAME)的数据,如果其值为null可能用户没有在他的个人资料设置喜爱的运动

I searched I can't find any solution for this problem.I have made graph-request in facebook SDK 3.5.When I try to get data using getProperty("Property_name") and if its value is null maybe user didn't set the favourite sports in his profile

例如:
code:对于buildUserInfoDisplay(GraphUser用户)方法

    // I have used all The mandatory permissions

    Map<String, String> map;

    map = new HashMap<String, String>();

    try {

        map.put("ID", user.getId().toString());

        String athletes = user.getProperty("favorite_athletes").toString();
        String fav_teams = user.getProperty("favorite_teams").toString();
        String sports = user.getProperty("sports").toString();
        String education = user.getProperty("education").toString();
        String work = user.getProperty("work").toString();

        map.put("athletes", athletes);
        map.put("fav teams", fav_teams);
        map.put("sports", sports);
        map.put("education", education);
        map.put("work", work);
        }
    } catch (Exception e) {
        // TODO: handle exception
        e.printStackTrace();
        Log.e(TAG + " Error:", e.toString());
    }

    //PassData is Interface object send data in Map<Strin String> format to Activity launching this Fragment
    try {
        passData(map); //Passing data through Interface
    } catch (Exception e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
        Log.e(TAG + " Error 2:", e.toString());         
    }
}`

Error
StackTrace : 

`java.lang.NullPointerException
at com.main.hotspot.MainFragment.buildUserInfoDisplay(MainFragment.java:177)
at com.main.hotspot.MainFragment.access$1(MainFragment.java:162)
at com.main.hotspot.MainFragment$2.onCompleted(MainFragment.java:77)
at com.facebook.Request$1.onCompleted(Request.java:269)
at com.facebook.Request$4.run(Request.java:1669)
at android.os.Handler.handleCallback(Handler.java:605)
at android.os.Handler.dispatchMessage(Handler.java:92)
at android.os.Looper.loop(Looper.java:137)
at android.app.ActivityThread.main(ActivityThread.java:4340)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:511)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:784)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551)
at dalvik.system.NativeStart.main(Native Method)

MainFragment Error:(646): java.lang.NullPointerException`

行177是我要求的运动数据:

line 177 is where I call for sports data:

字符串运动= user.getProperty(体育)的toString()。 //行177

如果运动属性不响应present这样的getProperty()会给NullPointerException异常。
并进一步我试图在数据格式的地图从片段传递到活动定义界面启动此片段。但由于异常的我不能传递数据的活动。我想对它进行处理活动。

if sports property is not present in response so getProperty() will give NullPointerException.and further I tried to pass data in Map format from fragment to Activity launching this fragment by defining Interface. But because of Exception I can't pass data to activity. I want to process it in Activity.

我怎么能检查调用的getProperty(),这个值在用户对象present之前。

how can I check before calling the getProperty() that this value is present in user object.

感谢。

推荐答案

的问题是,你是一个潜在的空对象调用的ToString,而不是真正相关的SDK的。常见的Java成语是保护和null检查您的来电。是这样的:

The issue is that you're calling .toString on a potentially null object, and is not really related to the SDK at all. Common java idiom is to protect your calls with null checks. Something like:

Object athletes = user.getProperty("favorite_athletes");
if (athletes != null) {
  map.put("athletes", athletes.toString());
}
...

这篇关于检查如果Facebook,Android的SDK调用之前的getProperty提供用户数据空值()的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-19 01:45