简体   繁体   English

在Android应用中检索Facebook用户的个人资料图片时为空指针

[英]Null pointer when retrieving a Facebook user's profile picture in Android app

I have an app that lets the user log in through Facebook SDK. 我有一个允许用户通过Facebook SDK登录的应用程序。 This works fine. 这很好。 I am trying to display that user's profile picture on a welcome activity along with their name. 我正在尝试在欢迎活动中显示该用户的个人资料图片及其姓名。 I can successfully get their name and display it, but I get a null pointer when I try to get their profile picture. 我可以成功获取并显示他们的名字,但是当我尝试获取他们的个人资料图片时,我得到了一个空指针。 This is the code snippet: 这是代码片段:

if (user != null) {
    TextView welcome = (TextView) findViewById(R.id.textView2);
    welcome.setText(user.getName());
    ProfilePictureView userPicture = (ProfilePictureView) findViewById(R.id.imageView1);
    userPicture.setProfileId(user.getId());
}

I am successfully retrieving and displaying the user's name by using welcome.setText(user.getName()); 我通过使用welcome.setText(user.getName());成功检索并显示了用户名welcome.setText(user.getName()); so I know that the user != null. 所以我知道用户!= null。 And the error is on this line: userPicture.setProfileId(user.getId()); 错误在这一行上: userPicture.setProfileId(user.getId()); Any ideas? 有任何想法吗? Thanks. 谢谢。

Full Code 完整代码

public class WelcomeScreen extends ActionBarActivity {

TextView tv;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_welcome_screen);

    tv = (TextView) findViewById(R.id.textView1);
    // Session session = Session.getActiveSession();
    if (Session.getActiveSession() != null) {
        Log.d("In?", String.valueOf(Session.getActiveSession().isOpened()));
    } else {
        Log.d("Session is ", "null");
    }

    if (Session.getActiveSession() != null
            || !Session.getActiveSession().isClosed()) {
        Session.openActiveSession(WelcomeScreen.this, true,
                new Session.StatusCallback() {

                    // callback when session changes state
                    @Override
                    public void call(Session session, SessionState state,
                            Exception exception) {
                        if (session.isOpened()) {

                            // make request to the /me API
                            Request.newMeRequest(session,
                                    new Request.GraphUserCallback() {

                                        // callback after Graph API
                                        // response with user
                                        // object
                                        @Override
                                        public void onCompleted(
                                                GraphUser user,
                                                Response response) {
                                            if (user != null) {
                                                TextView welcome = (TextView) findViewById(R.id.textView2);
                                                welcome.setText(user
                                                        .getName());
                                                ProfilePictureView userPicture = (ProfilePictureView) findViewById(R.id.imageView1);
                                                userPicture.setProfileId(user.getId());
                                            }
                                        }
                                    }).executeAsync();
                            Log.d("In?", String.valueOf(Session
                                    .getActiveSession().isOpened()));
                            session = Session.getActiveSession();
                            if (session == null) {
                                Log.d("Session is", "null");
                            }
                        }
                    }
                });
    }

}

}

Logcat logcat的

07-24 13:44:15.061: E/AndroidRuntime(10953): FATAL EXCEPTION: main
07-24 13:44:15.061: E/AndroidRuntime(10953): java.lang.NullPointerException
07-24 13:44:15.061: E/AndroidRuntime(10953):    at com.example.test.WelcomeScreen$1$1.onCompleted(WelcomeScreen.java:79)
07-24 13:44:15.061: E/AndroidRuntime(10953):    at com.facebook.Request$1.onCompleted(Request.java:303)
07-24 13:44:15.061: E/AndroidRuntime(10953):    at com.facebook.Request$4.run(Request.java:1726)
07-24 13:44:15.061: E/AndroidRuntime(10953):    at android.os.Handler.handleCallback(Handler.java:730)
07-24 13:44:15.061: E/AndroidRuntime(10953):    at android.os.Handler.dispatchMessage(Handler.java:92)
07-24 13:44:15.061: E/AndroidRuntime(10953):    at android.os.Looper.loop(Looper.java:137)
07-24 13:44:15.061: E/AndroidRuntime(10953):    at android.app.ActivityThread.main(ActivityThread.java:5289)
07-24 13:44:15.061: E/AndroidRuntime(10953):    at java.lang.reflect.Method.invokeNative(Native Method)
07-24 13:44:15.061: E/AndroidRuntime(10953):    at java.lang.reflect.Method.invoke(Method.java:525)
07-24 13:44:15.061: E/AndroidRuntime(10953):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:739)
07-24 13:44:15.061: E/AndroidRuntime(10953):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:555)
07-24 13:44:15.061: E/AndroidRuntime(10953):    at dalvik.system.NativeStart.main(Native Method)

EDIT 编辑

I've been looking around and I think I might need to request permission to access the user's photos? 我一直在环顾四周,我想我可能需要请求权限才能访问用户的照片? If this is the case, any ideas how I can? 如果是这样,我有什么想法吗? Thanks. 谢谢。

You have set the layout to activity_welcome_screen using setContentView . 您已设置布局activity_welcome_screen使用setContentView

But it is that you do not have the id imageview1 in that layout. 但这是因为您在该布局中没有id imageview1

So for line, 所以对于线,

ProfilePictureView userPicture = (ProfilePictureView) findViewById(R.id.imageView1);

The userPicture is null. userPicture为空。

And the exception is when you do, 而且例外是当您这样做时,

userPicture.setProfileId(user.getId());

That is - you are calling method setProfileId on something which is null. 也就是说-您正在对null的东西调用setProfileId方法。

So NullPointerException . 所以NullPointerException

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 检索Facebook个人资料图片 - Retrieving Facebook Profile Picture 从Facebook到Android应用的名称和个人资料图片 - Name and profile picture from Facebook into Android app 当用户使用其Facebook帐户登录时,如何获取用户的姓名,电子邮件和个人资料图片? - How do I get the user's name, email, and profile picture when the user logs in with their Facebook account? 尝试在Android中获取用户的Facebook个人资料图片时出现白色问号 - White question mark while trying to get user's Facebook profile picture in Android 在Android中获取Facebook个人资料图片 - Getting Facebook Profile Picture in Android Android应用程序Facebook登录:NULL指针异常 - Android app facebook login : NULL pointer exception 使用facebook sdk 4+在Android中加载facebook个人资料图片时遇到异常 - getting exceptions when loading facebook profile picture in android with facebook sdk 4+ 无法从Facebook获取用户的个人资料图片。 请查看详细信息 - Unable to fetch user's profile picture from facebook. Please see details 空指针异常:尝试从Facebook获取用户位置时 - Null Pointer Exception: when trying to get user location from Facebook 尝试显示获胜者的Facebook个人资料图片时,Andengine Game空指针异常 - Andengine Game null pointer exception while trying to show winner's facebook profile pic
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM