2015-04-05 57 views
8

我通過stackoverflow搜索了很多,但我找不到解決我的問題的答案。因此我張貼這個。facebook登錄空指針異常android

我有一個示例Android應用程序,我試圖使用Facebook登錄。下面是我遵循的步驟:

環境:Android的工作室 的Android SDK版本:22 Facebook的SDK:4.0

遵循的步驟,以在模擬器上安裝FB APK,所產生的開發哈希鍵和更新它在應用程序 - >設置中。 我的Android清單包含這些代碼

<uses-permission android:name="android.permission.INTERNET"/> 

<activity android:name="com.facebook.FacebookActivity" 
     android:configChanges="keyboard|keyboardHidden|screenLayout|screenSize|orientation" 
     android:theme="@android:style/Theme.Translucent.NoTitleBar" android:label="@string/app_name" /> 

<meta-data android:name="com.facebook.sdk.ApplicationId" android:value="@string/facebook_app_id"/> 

在我activity_main.xml中佈局文件,我有這樣的一段代碼

<com.facebook.login.widget.LoginButton 
    android:id="@+id/fblogin_button" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:layout_gravity="center_horizontal" 
    android:layout_marginTop="30dp" 
    android:layout_marginBottom="30dp" /> 

最後,在我的主要活動我有這個

public class MainActivity extends Activity { 
LoginButton loginButton; 
CallbackManager callbackManager; 
@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    FacebookSdk.sdkInitialize(getApplicationContext()); 
    callbackManager = CallbackManager.Factory.create(); 
    loginButton = (LoginButton) findViewById(R.id.fblogin_button); 
    loginButton.registerCallback(callbackManager, new FacebookCallback<LoginResult>() { 
     @Override 
     public void onSuccess(LoginResult loginResult) { 
      Toast.makeText(getApplicationContext(),"Fb Login Success",Toast.LENGTH_LONG); 
     } 

     @Override 
     public void onCancel() { 
      Toast.makeText(getApplicationContext(),"Fb on cancel",Toast.LENGTH_LONG); 
     } 

     @Override 
     public void onError(FacebookException e) { 
      Toast.makeText(getApplicationContext(),"Fb Login Error",Toast.LENGTH_LONG); 
     } 
    }); 
    setContentView(R.layout.activity_main); 

} 

@Override 
protected void onActivityResult(int requestCode, int resultCode, Intent data){ 
    super.onActivityResult(requestCode, resultCode, data); 
    Log.d("Inside Fb login","on Activiry result"); 
    callbackManager.onActivityResult(requestCode, resultCode, data); 
}} 

我收到一個空指針異常,如下所示

java.lang.RuntimeException: Unable to start activity componentInfo{PROJECT.MainActivity}: java.lang.NullPointerException: Attempt to invoke virtual method 'void com.facebook.login.widget.LoginButton.registerCallback(com.facebook.CallbackManager, com.facebook.FacebookCallback)' on a null object reference 
     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2298) 
     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2360) 
     at android.app.ActivityThread.access$800(ActivityThread.java:144) 
     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1278) 
     at android.os.Handler.dispatchMessage(Handler.java:102) 
     at android.os.Looper.loop(Looper.java:135) 
     at android.app.ActivityThread.main(ActivityThread.java:5221) 
     at java.lang.reflect.Method.invoke(Native Method) 
     at java.lang.reflect.Method.invoke(Method.java:372) 
     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:899) 
     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:694) 

我在這裏錯過了什麼?

回答

13

看起來像你得到一個NullPointerException因爲你打電話findViewById()你打電話之前setContentView(),所以loginButton爲空當你調用loginButton.registerCallback()

通話剛剛轉會setContentView()頂端:

@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 

    FacebookSdk.sdkInitialize(getApplicationContext()); 

    setContentView(R.layout.activity_main); //Moved up here to resolve NPE 

    callbackManager = CallbackManager.Factory.create(); 
    loginButton = (LoginButton) findViewById(R.id.fblogin_button); 
    loginButton.registerCallback(callbackManager, new FacebookCallback<LoginResult>() { 
     @Override 
     public void onSuccess(LoginResult loginResult) { 
      Toast.makeText(getApplicationContext(),"Fb Login Success",Toast.LENGTH_LONG); 
     } 

     @Override 
     public void onCancel() { 
      Toast.makeText(getApplicationContext(),"Fb on cancel",Toast.LENGTH_LONG); 
     } 

     @Override 
     public void onError(FacebookException e) { 
      Toast.makeText(getApplicationContext(),"Fb Login Error",Toast.LENGTH_LONG); 
     } 
    }); 

} 
+0

放置FacebookSdk.sdkInitialize(getApplicationContext());在 之前setContentView(R.layout.activity_main);訣竅。感謝您的幫助@Daniel – user2752910 2015-04-10 06:02:43

8

崩潰,似乎與我們在AndoridMenifest.xml文件中設置的applicationID。正如我想通了,當Facebook SDK是綁創建LoginFramment隨後的applicationID是作爲正在添加。這裏nullcom.facebook.login.LoginFragment

public void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     if(savedInstanceState != null) { 
      this.loginClient = (LoginClient)savedInstanceState.getParcelable("loginClient"); 
      this.loginClient.setFragment(this); 
     } else { 
      this.loginClient = new LoginClient(this); 
     } 

     this.loginClient.setOnCompletedListener(new OnCompletedListener() { 
      public void onCompleted(Result outcome) { 
       LoginFragment.this.onLoginClientCompleted(outcome); 
      } 
     }); 
     FragmentActivity activity = this.getActivity(); 
     if(activity != null) { 
      this.initializeCallingPackage(activity); 
      if(activity.getIntent() != null) { 
       //the applicationId in this.request object is null 
       this.request = (Request)activity.getIntent().getParcelableExtra("request"); 

      } 

     } 
    } 

正如varoius指出的onCreate方法張貼的applicationID來了空,因爲該字符串資源app_id未被android正確解析。 https://github.com/jamesward/AndroidStringBug

我通過在初始化facebook sdk後手動設置applicationId來解決這個問題,如下面onCreate方法中給出的。

FacebookSdk.sdkInitialize(getApplicationContext()); 
FacebookSdk.setApplicationId(getResources().getString(R.string.app_id)); 
mFacebookCallbackManager = CallbackManager.Factory.create(); 
+0

爲什麼元數據字符串不工作?我得到了同樣的問題...做了你的建議。 – behinddwalls 2017-01-10 04:05:57

+0

我有同樣的問題,現在我不知道如何解決它與最新的SDK,因爲它棄用sdkInitiliaze方法,因爲它不允許我設置ApplicationId witihin onCreate?任何建議? – batmaci 2017-11-12 02:56:51

+0

只需使用fb前綴元數據值,請參閱源代碼:https://github.com/facebook/facebook-android-sdk/blob/sdk-version-4.8.0/facebook/src/main/java/com /facebook/FacebookSdk.java#L598 – ballzak 2018-03-09 12:44:25

0

作爲anj指示的問題通常是與ApplicationId沒有讀取。在我的情況下,問題在於命名空間。確保你的類的名字空間是根名字空間。所以我的應用程序是一個Xamarin的Android應用程序,我使用的命名空間如下,但我的Android應用程序的名稱空間是myApp.Droid,這種方式ApplicaitonId不是從strings.xml讀取更改爲myApp.Droid解決了我的問題。我希望它可以幫助任何有類似問題的人

[assembly: MetaData("com.facebook.sdk.ApplicationId", Value = "@string/app_id")] 
namespace myApp.Droid.Renderer