java - 由 : java. lang.NullPointerException 引起:尝试调用虚拟方法 'java.lang.String com.facebook.Profile.getFirstName()'

标签 java android facebook

我正在尝试使用 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 .

<强> getCurrentProfile()

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/

相关文章:

java - gradle 脚本中的实现错误 - android studio api28

android - KitKat Android GDK 预览版在哪里?

android - 检测网页是否在 iOS/Android 上的 native Facebook 应用程序中加载

android - 在 Titanium 应用程序中使用 Facebook 登录

html - Facebook Like 按钮在加载后将我的网站内容下推 1 个像素

Java,正则表达式捕获带有空格的字符串

java - 长时间发送请求时出现 org.springframework.transaction.CannotCreateTransactionException

java - JSTL 与 JSP 小脚本

android - 应用 ColorFilter 但在 PNG 文件中保留透明阴影

android - Grafika 和 OpenGL 在 android 上以方形录制视频