2015-09-17 235 views
0

我正在使用SDK 4實現Facebook登錄,但只能使用一次。登錄管理器的登錄後Facebook登錄無法工作

當我安裝應用程序時,我可以完美地登錄Facebook。然而,當我退出並嘗試重新登錄時,我得到抱歉,發生了錯誤消息:

Sorry, something went wrong message

如何初始化SDK:

// Called in onCreate() 
private void initFacebook() { 
    FacebookSdk.sdkInitialize(getApplicationContext()); 
    callbackManager = CallbackManager.Factory.create(); 

    LoginManager.getInstance().registerCallback(callbackManager, 
      new FacebookCallback<LoginResult>() { 
       @Override 
       public void onSuccess(LoginResult loginResult) { 
        Log.d(TAG, "On success"); 
        getUserData(loginResult.getAccessToken()); 
       } 

       @Override 
       public void onCancel() { 
        Log.d(TAG, "On cancel"); 
       } 

       @Override 
       public void onError(FacebookException error) { 
        Log.d(TAG, error.toString()); 
       } 
      }); 
} 

@Override 
protected void onActivityResult(int requestCode, int resultCode, Intent data) { 
    if (callbackManager != null) { 
     callbackManager.onActivityResult(requestCode, resultCode, data); 
    } 
} 

private void getUserData(AccessToken accessToken) { 
    GraphRequest request = GraphRequest.newMeRequest(
      accessToken, 
      new GraphRequest.GraphJSONObjectCallback() { 
       @Override 
       public void onCompleted(
         JSONObject json, 
         GraphResponse response) { 
        Log.v(TAG, response.toString()); 
        if (response.getError() != null) { 
         // handle error 
         Log.d(TAG, "GraphRequest error"); 
        } else { 
         Log.d(TAG, "GraphRequest success"); 
         try { 

          String jsonresult = String.valueOf(json); 
          Log.d(TAG, "JSON Result" + jsonresult); 

          String str_email = json.getString("email"); 
          String str_id = json.getString("id"); 
          String str_name = json.getString("name"); 

         } catch (JSONException e) { 
          e.printStackTrace(); 
         } 
         onLoginSuccess(); 
        } 
       } 
      }); 
    Bundle parameters = new Bundle(); 
    parameters.putString("fields", "id, name, email"); 
    request.setParameters(parameters); 
    request.executeAsync(); 
} 

而且這裏的登錄和註銷:

@Override 
public void onLoginClicked() { 
    LoginManager.getInstance().logInWithReadPermissions(this, Arrays.asList("email")); 
} 

@Override 
public void onLogoutClicked() { 
    LoginManager.getInstance().logOut(); 
} 

怎麼回事?

謝謝!

回答

0

首先是初始化的Facebook SDK,就在setContentview();

FacebookSdk.sdkInitialize(getApplicationContext()); 
setContentView(R.layout.main); 

然後onResume()你應該叫

@Override 
    protected void onResume() { 
     super.onResume(); 
     AppEventsLogger.activateApp(this); 
    } 

onPause()調用以下:

@Override 
    public void onPause() { 
     super.onPause(); 
     //for facebook 
     AppEventsLogger.deactivateApp(this); 
    } 

除了這個代碼看起來很好,昨天ia也有這樣的問題,我猜這是Facebook的問題,因爲今天我的應用程序自動工作正常。

+0

謝謝,但這不適合我 –