我正在尝试将Facebook Android SDK应用到我的游戏中,但是当我尝试使用Facebook登录时,它会崩溃并生成此错误
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)
这是我的主要活动课程
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);
}
}
这是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);
}
我试图让我有一个自定义按钮供玩家按,而不是使用提供的标准按钮登录,所以我调用以下功能来触发登录
public void LoginToFacebook ()
{
LoginManager.Instance.LogInWithReadPermissions (activity, new List<string> { "user_friends" });
#if DEBUG
Console.WriteLine ("Logging into facebook");
#endif
}
我按下自定义按钮登录时会导致开头显示Java.lang.NullPointerException错误。
我已经完成了所有必要的步骤,比如生成keyhash,在facebook上注册应用程序,将所有必要的东西添加到AndroidManifest中。
编辑:我忘了提到上面显示的主要活动是在与InitFacebookSDK不同的项目中,我只是引用它所在的项目并在MainActivity中调用它。我正在尝试将Facebook实施到一个项目中,我可以在下次尝试将Facebook实施到另一个项目时轻松参考。
编辑:当我第一次初始化Facebook SDK时,我传入一个android.activity,然后存储在Facebook管理器中,然后我会通过将其传递给LoginWithReadPermission来登录。
编辑:我尝试在主要活动中放置任何与Facebook相关的内容,但仍会返回Null Pointer Exception错误。就像注册回调一样,从IFacebookCallback引用MainActivity引用,然后在回调注册后登录。
编辑:这是我的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>
这是我的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>
答案 0 :(得分:1)
原来Facebook SDK没有读取我在String.XML中声明的Facebook App ID,您必须在使用FacebookSdk.ApplicationId =“Your ID HERE”初始化SDK后手动设置它;
答案 1 :(得分:0)
据我所知,你用来实现facebook回调的方法效果不理想。
更好的方法是在要获得回调函数的类中使用IFacebookCallback。之后创建一个配置文件跟踪器,帮助您了解用户登录是否发生更改。
/// <summary>
/// Test if current facebook token exist and reconnect to application if needed
/// </summary>
/// <param name="currentAccessToken">current facebook session</param>
private async void updateWithToken(AccessToken currentAccessToken)
{
if (currentAccessToken != null)
{
User.Instance.Facebook_Id = currentAccessToken.UserId;
//Save token for next connection
LocalSaving ls = new LocalSaving();
var sToken = ls.RetrieveToken();
//save token into singleton
Core.WService.Token.Instance.SaveTokenInfo(sToken);
FindViewById<LoginButton>(Resource.Id.login_button).Visibility = ViewStates.Gone;
progressBar.Visibility = ViewStates.Visible;
loadingTextView.Visibility = ViewStates.Visible;
await onLoadInformation();
progressBar.Visibility = ViewStates.Invisible;
loadingTextView.Visibility = ViewStates.Invisible;
}
}
目前我没有简单的代码示例,但您可以查看我推荐用于android xamarin developpment的youtube频道:
https://www.youtube.com/watch?v=OYTe0ZWKAOU&list=PLCuRg51-gw5VqYchUekCqxUS9hEZkDf6l&index=65
最后,我在我的应用程序中重新连接时遇到了一些问题,更好的是为此创建方法,这里是我重新连接的代码:
RewriteEngine on
RewriteRule ^(.*)\.php /$1.html [R=301,L]
我希望这会对你有所帮助。