2015-05-29 103 views
2

當我運行我的Android應用程序並單擊批准以授予權限時,它不會重定向到MainActivity。 「登錄」消息沒有出現在Catlog中。我已閱讀Facebook開發人員指南,並在Stack中將我的代碼與不同的主題進行比較。我看不出我做錯了什麼。登錄按鈕facebook android不會重定向到新活動

我會很樂意的幫助。

public class Login extends Activity { 

/** 
* Called when the activity is first created. 
*/ 

    private CallbackManager callbackManager; 

@Override 
public void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    FacebookSdk.sdkInitialize(getApplicationContext()); 
    setContentView(R.layout.activity_login); 
    final CallbackManager callbackManager = CallbackManager.Factory.create(); 
    LoginButton loginButton = (LoginButton) findViewById(R.id.login_button); 
    loginButton.setReadPermissions("public_profile", "email", "user_friends"); 


    loginButton.registerCallback(callbackManager, new FacebookCallback<LoginResult>() { 


     @Override 
     public void onSuccess(LoginResult loginResult) { 

      Intent i = new Intent(Login.this, MainActivity.class); 
      startActivity(i); 
      System.out.print("Logged in"); 

     } 

     @Override 
     public void onCancel() { 
      // App code 

     } 

     @Override 
     public void onError(FacebookException exception) { 
      // App code 
      Log.i("Error" , "Error"); 
      } 


     }); 
    } 

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

} 

堆棧跟蹤

java.lang.RuntimeException: Failure delivering result ResultInfo{who=null, request=64206, result=-1, data=Intent { (has extras) }} to activity {org.MYapp.FBtestApp/org.MYapp.FBtestApp.Login}: java.lang.NullPointerException: Attempt to invoke interface method 'boolean com.facebook.CallbackManager.onActivityResult(int, int, android.content.Intent)' on a null object reference 
      at android.app.ActivityThread.deliverResults(ActivityThread.java:3974) 
     at android.app.ActivityThread.handleSendResult(ActivityThread.java:4017) 

Caused by: java.lang.NullPointerException: Attempt to invoke interface method 'boolean com.facebook.CallbackManager.onActivityResult(int, int, android.content.Intent)' on a null object reference 
     at org.MYapp.FBtestApp.Login.onActivityResult(Login.java:98) 
+0

當您僅提供代碼時,很難爲您提供幫助。 logcat中是否有錯誤?有參數是空的(特別是「resultCode」和「數據」)? –

+1

我建議你在問問題時不要*更新你的代碼。現在接受的答案不再有意義。 – tir38

回答

5

對於登錄工作,你必須從FacebookCallback anomymous類中刪除onActivityResult()方法。 正確的代碼應該是這樣的:

所有的
private CallbackManager callbackManager; 

@Override 
public void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_login); 
    FacebookSdk.sdkInitialize(getApplicationContext()); 

    callbackManager = CallbackManager.Factory.create(); 

    LoginButton loginButton = (LoginButton) findViewById(R.id.login_button); 
    loginButton.setReadPermissions("public_profile", "email", "user_friends"); 

    loginButton.registerCallback(callbackManager, new FacebookCallback<LoginResult>() { 
     @Override 
     public void onSuccess(LoginResult loginResult) { 
      Intent i = new Intent(Login.this, MainActivity.class); 
      startActivity(i); 
      System.out.print("Logged in"); 
     } 

     @Override 
     public void onCancel() { 
      // App code 

     } 

     @Override 
     public void onError(FacebookException exception) { 
      // App code 
      Log.i("Error" , "Error"); 
     } 
    }); 
} 

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

錯誤:無法解析符號callbackManager callbackManager.onActivityResult(requestCode,resultCode爲,數據) } 編輯:錯過了你在上面 – datasmurfen

+0

更新創建CallbackManager成員變量。看看我的代碼,我已經編輯它(第1行和第9行)。 –

+0

你錯過了我的代碼。更改該行:final CallbackManager callbackManager = CallbackManager.Factory.create(); to:callbackManager = CallbackManager.Factory.create();另外不要忘記super.onActivityResult(requestCode,resultCode,data);在onActivityResult()中。 –

1

首先,將私人private CallbackManager callbackManager; 到你的代碼。它應該是這個樣子:

callbackManager = CallbackManager.Factory.create(); 
LoginButton loginButton = (LoginButton) findViewById(R.id.login_button); 

然後加入FacebookSdk.sdkInitialize(getApplicationContext());

super.onCreate(savedInstanceState);

試試這個希望這個作品

+0

我沒有想到這會工作,我不明白爲什麼這個工作後移動'super.onCreate(savedInstanceState);' – mes

0

以下爲我工作:

補充:

FacebookSdk.sdkInitialize(getApplicationContext()); 

之前:這是奇怪

super.onCreate(savedInstanceState); 

,對一切都很好早期工作;但突然間,我有這個問題。現在,它解決了上述解決方案。