我正在尝试使用 Facebook API 获取有关 Android 验证的一些数据。我可以登录我的 Facebook 帐户。这是我设置的从 Facebook 帐户获取数据的权限
loginButton.setReadPermissions(Arrays.asList( "public_profile", "email", "user_birthday", "user_friends"));
使用上述权限,我尝试获取配置文件名称和其他数据,但我收到以下代码 fragment 的空指针异常
public void getFacebookData(AccessToken accessToken, Profile profile) {
System.out.println("---------------------");
System.out.println("--Facebook Login Successful!");
System.out.println("--Logged in user Details : ");
System.out.println("---------------------");
System.out.println("--User ID : " + accessToken.getUserId());
System.out.println("--Authentication Token : " + accessToken.getToken());
System.out.println("---------------------");
System.out.println("--First Name : " + profile.getFirstName());
System.out.println("--Last Name : " + profile.getLastName());
System.out.println("--URL Perfil: " + profile.getLinkUri());
System.out.println("--URL Imagem: " + profile.getProfilePictureUri(500, 500));
System.out.println("---------------------");
System.out.println("--ID : " + profile.getId());
System.out.println("--Name : " + profile.getName());
System.out.println("---------------------");
TextView profile_name = (TextView)findViewById(R.id.profile);
profile_name.setText(profile.getName());
请问我使用的方法有什么问题。
最佳答案
NullPointerException is thrown when an application attempts to use an object reference that has the null value .
Getter for the profile that is currently logged in to the application.
public void getFacebookData(AccessToken accessToken, Profile profile) {
System.out.println("---------------------");
profile = Profile.getCurrentProfile(); // Add this
if (profile != null)
{
System.out.println("--First Name : " + profile.getFirstName());
System.out.println("--Last Name : " + profile.getLastName());
System.out.println("--URL Perfil: " + profile.getLinkUri());
System.out.println("--URL Imagem: " + profile.getProfilePictureUri(500, 500));
System.out.println("---------------------");
System.out.println("--ID : " + profile.getId());
System.out.println("--Name : " + profile.getName());
System.out.println("---------------------");
}
关于java - 由 : java. lang.NullPointerException 引起:尝试调用虚拟方法 'java.lang.String com.facebook.Profile.getFirstName()',我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45190323/