I have an app that lets the user log in through 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());
so I know that the user != null. And the error is on this line: 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
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
.
But it is that you do not have the id imageview1
in that layout.
So for line,
ProfilePictureView userPicture = (ProfilePictureView) findViewById(R.id.imageView1);
The userPicture
is null.
And the exception is when you do,
userPicture.setProfileId(user.getId());
That is - you are calling method setProfileId
on something which is null.
So NullPointerException .
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.