[英]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.