无法通过facebook android sdk获取Facebook用户信息

时间:2014-12-20 08:45:33

标签: android facebook facebook-graph-api

我将我的主要活动onCreate方法设置为:

    protected void onCreate(Bundle savedInstanceState) {
    // TODO Auto-generated method stub
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    testtext = (TextView) findViewById(R.id.testtext);

    loginButton = (LoginButton) findViewById(R.id.authButton);
    loginButton.setUserInfoChangedCallback(new LoginButton.UserInfoChangedCallback() {
        @Override
        public void onUserInfoFetched(GraphUser user) {
            MainActivity.this.user = user;
            System.out.println("user:"+user.getFirstName());
            testGet();

        }
    });
}

testGet()方法是测试获取用户的新闻源:

public void testGet(){
    String[] requests = {"/me/home"};
    RequestBatch requestBatch = new RequestBatch();
    for (final String request : requests) {
        requestBatch.add(new Request(Session.getActiveSession(), 
                request, null, null, new Request.Callback() {
            public void onCompleted(Response response) {
                GraphObject graphObject = response.getGraphObject();
                String s = testtext.getText().toString();
                if (graphObject != null) {
                        s = s + String.format("%s\n%s\n%s\n%s\n\n", 
                                graphObject.getProperty("name"), 
                                graphObject.getProperty("description"),
                                graphObject.getProperty("create_time"),
                                graphObject.getProperty("type"));
                }
                s = s + "\n hello, " + user.getFirstName() + "!\n";
                testtext.setText(s);
                System.out.println(s);
                testtext.setText(s);
            }
        }));
    }
    requestBatch.executeAsync();
}

然而,在Android设备上运行应用程序后,甚至在我点击facebook登录按钮之前它就崩溃了。

错误是:

java.lang.NullPointerException: Attempt to invoke interface method 'java.lang.String com.facebook.model.GraphUser.getFirstName()' on a null object reference

这里有什么问题?为什么甚至在我登录之前调用onUserInfoFetched()?

顺便说一句,有没有人知道我的testGet()方法是否正确获取新闻源?

谢谢!

1 个答案:

答案 0 :(得分:0)

替换此

GraphObject graphObject = response.getGraphObject();

由此

JSONObject object = user.getInnerJSONObject();