facebook登录null指针exceptionandroid

我通过stackoverflow搜索了很多,但我找不到解决我的问题的答案。 因此我发布了这个。

我有一个示例Android应用程序,我正在尝试使用Facebook登录。 以下是我遵循的步骤:

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

按照以下步骤在模拟器上安装FB APK,生成Developer哈希密钥并在app-> settings中更新它。 我的android清单有这些代码

   

在我的activity_main.xml布局文件中,我有这段代码

  

最后在我的主要活动中我有这个

 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() { @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); }} 

我得到一个空指针exception,如下所示

  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) 

我在这里想念的是什么?

看起来你得到一个NullPointerException因为你在调用setContentView()之前调用了findViewById() setContentView() ,所以当你调用loginButton.registerCallback()时, loginButton为null。

只需将调用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() { @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); } }); } 

崩溃似乎与我们在AndoridMenifest.xml文件中设置的AndoridMenifest.xml 。 我发现当facebook sdk正在创建LoginFramment然后ApplicationId将LoginFramment nullcom.facebook.login.LoginFragment类的onCreate方法

 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"); } } } 

正如varoiuspost所指出的那样,applicationId将变为null,因为android没有正确解析字符串资源app_idhttps://github.com/jamesward/AndroidStringBug

我通过在初始化facebook sdk之后手动设置applicationId解决了这个问题,如下面onCreate方法中给出的那样。

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

作为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