I'm trying to implement Facebook Android SDK into my game but when I try to login using facebook, it would crash and generate this error
Java.Lang.NullPointerException: Attempt to invoke virtual method 'boolean java.lang.String.equals(java.lang.Object)' on a null object reference
at --- End of managed exception stack trace ---
at java.lang.NullPointerException: Attempt to invoke virtual method 'boolean java.lang.String.equals(java.lang.Object)' on a null object reference
at at com.facebook.login.LoginClient.getLogger(LoginClient.java:369)
at at com.facebook.login.LoginClient.logAuthorizationMethodComplete(LoginClient.java:419)
at at com.facebook.login.LoginClient.tryNextHandler(LoginClient.java:202)
at at com.facebook.login.GetTokenLoginMethodHandler.getTokenCompleted(GetTokenLoginMethodHandler.java:119)
at at com.facebook.login.GetTokenLoginMethodHandler$1.completed(GetTokenLoginMethodHandler.java:74)
at at com.facebook.internal.PlatformServiceClient.callback(PlatformServiceClient.java:157)
at at com.facebook.internal.PlatformServiceClient.handleMessage(PlatformServiceClient.java:141)
at at com.facebook.internal.PlatformServiceClient$1.handleMessage(PlatformServiceClient.java:62)
at at android.os.Handler.dispatchMessage(Handler.java:102)
at at android.os.Looper.loop(Looper.java:234)
at at android.app.ActivityThread.main(ActivityThread.java:5526)
at at java.lang.reflect.Method.invoke(Native Method)
at at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726)
at at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)
This is my Main Activity class
public class MainActivity : AndroidGameActivity
{
int count = 1;
protected override void OnActivityResult (int _requestCode, Result _resultCode, Intent _intent)
{
GooglePlayServicesManager.instance.OnActivityResult(_requestCode, _resultCode, _intent);
}
protected override void OnCreate (Bundle savedInstanceState)
{
base.OnCreate (savedInstanceState);
var g = new Game1();
Social.SocialManager.OnCreate_Android(this);
FacebookManager.mInstance.InitFacebookSDK (this.ApplicationContext, this);
SetContentView(g.Services.GetService<View>());
g.Run();
}
protected override void OnDestroy()
{
base.OnDestroy();
}
protected override void OnStart()
{
base.OnStart();
Console.WriteLine("activity::onstart");
}
protected override void OnStop()
{
base.OnStop();
Console.WriteLine("activity::onstop");
}
protected override void OnResume()
{
base.OnResume();
//SpringAdvert.AdvertManager.OnAppResume();
}
protected override void OnPause()
{
base.OnPause();
//SpringAdvert.AdvertManager.OnAppPause();
}
public override void OnConfigurationChanged(Android.Content.Res.Configuration newConfig)
{
base.OnConfigurationChanged(newConfig);
}
}
This is the InitFacebookSDK
public void InitFacebookSDK(Android.Content.Context Context, Android.App.Activity Activity)
{
if(!FacebookSdk.IsInitialized)
{
//Initialize the whole sdk
FacebookSdk.SdkInitialize(Context);
activity = Activity;
}
#if DEBUG
Console.WriteLine ("Facebook SDK Initialized: " + FacebookSdk.IsInitialized);
#endif
//Creates a callback manager to handle the callbacks facebook have
CallbackManager = CallbackManagerFactory.Create ();
var loginCallback = new FacebookCallback<LoginResult> {
HandleSuccess = loginResult => {
HandlePendingAction ();
},
HandleCancel = () => {
if (pendingAction != PendingAction.NONE) {
pendingAction = PendingAction.NONE;
}
},
HandleError = loginError => {
if (pendingAction != PendingAction.NONE
&& loginError is FacebookAuthorizationException) {
pendingAction = PendingAction.NONE;
}
}
};
LoginManager.Instance.RegisterCallback (CallbackManager, loginCallback);
}
I'm trying to make it that I have a custom button for players to press instead of using the standard button facebook provided to login so I call the following function to trigger the login
public void LoginToFacebook ()
{
LoginManager.Instance.LogInWithReadPermissions (activity, new List<string> { "user_friends" });
#if DEBUG
Console.WriteLine ("Logging into facebook");
#endif
}
The moment I press my custom button to log in it would cause the Java.lang.NullPointerException error shown at the start.
I've done all the necessary steps like generating the keyhash, registering the app on facebook, adding all the necessary things into AndroidManifest.
EDIT: I forgot to mention that the main activity shown above is in a different project from InitFacebookSDK,I just reference the project where it is located and called it in MainActivity. I'm trying to implement Facebook into a project where I could easily reference from next time when I'm trying to implement Facebook into another project.
EDIT: When I first initialize the Facebook SDK, I pass in an android.activity which is then stored in Facebook Manager, and I would then use it to log in by passing it into LoginWithReadPermission.
EDIT: I've tried just putting anything Facebook related in Main Activity, it still returns Null Pointer Exception error. Like registering the callbacks, making MainActivity reference from IFacebookCallback then signing in after the callback is registered.
EDIT: Here is my Android Manifest
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android" android:versionCode="1" android:versionName="1.0" package="com.test.testing">
<uses-sdk android:minSdkVersion="10" android:targetSdkVersion="23" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.INTERNET" />
<application android:allowBackup="true" android:icon="@mipmap/icon" android:label="@string/app_name">
<!-- To access Facebook APIS-->
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.INTERNET" />
<meta-deta android:name="com.facebook.sdk.ApplicationId" android:value="@string/facebook_app_id"/>
<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" />
</application>
</manifest>
and here is my string.xml
<?xml version="1.0" encoding="utf-8"?>
<resources>
<string name="hello">Hello World, Click Me!</string>
<string name="app_name">testing</string>
<string name="facebook_app_id">xxxxxxxxxxxx</string>
</resources>
Turns out Facebook SDK is not reading the Facebook App ID I had declared in String.XML, you have to manually set it after you have initialized the SDK using FacebookSdk.ApplicationId = "Your ID HERE";