gpt4 book ai didi

java - 在 Android 应用程序中检索 Facebook 用户的个人资料图片时出现空指针

转载 作者:行者123 更新时间:2023-12-01 12:42:48 25 4
gpt4 key购买 nike

我有一个应用程序,可以让用户通过 Facebook SDK 登录。这很好用。我试图在欢迎 Activity 中显示该用户的个人资料图片及其姓名。我可以成功获取他们的名字并显示它,但当我尝试获取他们的个人资料图片时,我得到一个空指针。这是代码 fragment :

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());
}

我使用 welcome.setText(user.getName()); 成功检索并显示用户名,因此我知道用户 != null。错误就在这一行: userPicture.setProfileId(user.getId()); 有什么想法吗?谢谢。

完整代码

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)

编辑

我环顾四周,我想我可能需要请求权限才能访问用户的照片?如果是这种情况,我有什么想法吗?谢谢。

最佳答案

您已使用 setContentView 将布局设​​置为 activity_welcome_screen

但是您在该布局中没有 id imageview1

所以对于线来说,

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

userPicture 为空。

异常(exception)是当你这样做时,

userPicture.setProfileId(user.getId());

也就是说 - 您正在对 null 的内容调用方法 setProfileId

所以NullPointerException

关于java - 在 Android 应用程序中检索 Facebook 用户的个人资料图片时出现空指针,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24940816/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com