2016-09-10 3 views
0

私はAndroid用アプリケーションでFacebook Loginを使用しています。私は正常にログインし、TextViewにユーザー名を表示することができますが、私は以下の問題に直面していますAndroidのアクティビティでFacebookフレンドリストを取得するその他のユーザーログインしている場所

私はアプリケーションを閉じて再び開くと、フェイスブックのログイン後、私はログアウトボタンを取得しています。私は、アクティビティが再びfacebook APIを呼び出してユーザー名を取得する必要があります。

私は私のOnCreateイベントで 以下のコードを使用しています

FacebookSdk.sdkInitialize(getApplicationContext()); 
    AppEventsLogger.activateApp(this); 
    setContentView(R.layout.activity_login); 
    callbackManager = CallbackManager.Factory.create(); 
    LoginButton loginButton = (LoginButton) findViewById(R.id.connectWithFbButton); 
    loginButton.setReadPermissions("email", "user_friends"); 
    loginButton.registerCallback(callbackManager, nCallback); 



protected void onActivityResult(int requestCode, int resultCode, Intent data) 
    { 
     super.onActivityResult(requestCode, resultCode, data); 
     callbackManager.onActivityResult(requestCode,resultCode,data); 
    } 

private FacebookCallback<LoginResult> nCallback = new FacebookCallback<LoginResult>() { 
      @Override 
      public void onSuccess(LoginResult loginResult) { 
       AccessToken accessToken = loginResult.getAccessToken(); 
       Profile profile = Profile.getCurrentProfile(); 
       if (profile!= null) 
       { 
        String FirstName = ""; 
        String LastName = ""; 
        String ProfilePic = ""; 
        String PId = ""; 
         FirstName = URLEncoder.encode(profile.getFirstName(), "utf-8"); 
         LastName = URLEncoder.encode(profile.getLastName(), "utf-8"); 
message.setText("Hello " + profile.getFirstName()); 
       } 
    } 

      @Override 
      public void onCancel() { 

      } 

      @Override 
      public void onError(FacebookException error) { 

      } 
     }; 

答えて

0

は、私はあなたがする必要があるので、あなたがログインしたときにのみ呼び出され、この

Profile profile; 
    String name; 

    try{ 
     profile = Profile.getCurrentProfile(); 
     name = profile.getName(); 
    }catch(NullPointerException npe){ 
     npe.printStackTrace(); 
    } 

onSuccessような何かを探しています信じてgetCorrentProfileoutsideonSucessあなたがログインしていない場合、これはヌルを返します。

したがって、onCreateでgetCorrentProfileがnullであるかどうかを確認します。ユーザーがログインすると、onSuccessで結果が得られます。プロファイルを使用して名前やその他の情報を取得する場合

関連する問題