android facebook sdk get email =>空指针异常

时间:2015-02-07 16:00:13

标签: android facebook email

我无法从facebook请求(sdk 3.23)获取用户电子邮件,我总是获得空指针异常: 我尝试:user.asMap()。get(“email”)。toString();和user.getProperty(“email”)。toString();但没有任何作用

LoginButton facebookLoginButton = (LoginButton) view.findViewById(R.id.connexion_facebook);
    facebookLoginButton.setFragment(this);

    facebookLoginButton.setUserInfoChangedCallback(new LoginButton.UserInfoChangedCallback() {

        @Override
        public void onUserInfoFetched(GraphUser user) {
            mFacebookUser = user;

            Log.v(this.getClass().getName(), "Facebook" + "onCompleted");
            if (user != null) {
                String firstName = user.getFirstName();
                String lastName = user.getLastName();
                String id = user.getId();
                String email=user.getProperty("email").toString();


                //String email2 = user.asMap().get("email").toString();

                Log.v(this.getClass().getName(), "Facebook" + "facebookid" + id);
                Log.v(this.getClass().getName(), "Facebook" + "firstName" + firstName);
                Log.v(this.getClass().getName(), "Facebook" + "lastName" + lastName);
                Log.v(this.getClass().getName(), "Facebook" + "email" + email);
                TelephonyManager telemamanger = (TelephonyManager) ContextContainer.getContext().getSystemService(
                        Context.TELEPHONY_SERVICE);
                String getSimSerialNumber = telemamanger.getSimSerialNumber();
                String mPhoneNumber = telemamanger.getLine1Number();
                Log.v(this.getClass().getName() + "Facebook" + "phone", mPhoneNumber);

            }
            else {
                Log.e(TAG, "No Facebook user");
            }
        }
    });
    facebookLoginButton.setReadPermissions(Arrays.asList(
            "email","user_location"));

总是空指针异常......

02-07 16:57:40.538      821-821/com.esirdreamteam.projetigr E/AndroidRuntime﹕ FATAL EXCEPTION: main
Process: com.esirdreamteam.projetigr, PID: 821
java.lang.NullPointerException
        at com.esirdreamteam.activities.ConnexionFragment$4.onUserInfoFetched(ConnexionFragment.java:165)
        at com.facebook.widget.LoginButton$2.onCompleted(LoginButton.java:771)
        at com.facebook.Request$1.onCompleted(Request.java:283)
        at com.facebook.Request$4.run(Request.java:1668)
        at android.os.Handler.handleCallback(Handler.java:733)
        at android.os.Handler.dispatchMessage(Handler.java:95)
        at android.os.Looper.loop(Looper.java:136)
        at android.app.ActivityThread.main(ActivityThread.java:5061)
        at java.lang.reflect.Method.invokeNative(Native Method)
        at java.lang.reflect.Method.invoke(Method.java:515)
        at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:812)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:628)
        at dalvik.system.NativeStart.main(Native Method)

全部谢谢

托马斯

2 个答案:

答案 0 :(得分:0)

String email=user.asMap().get("email").toString(); 

答案 1 :(得分:0)

我找到了答案:我的令牌在我的测试之间没有清理/清除,我添加了"电子邮件"仅在测试后才允许。 所以我在做测试之前就把它清理干净了,现在效果很好。