0

我创建了一个使用 Facebook 登录的功能。

public void Login ()
{
    var ctx = Forms.Context as MainActivity;
    var accounts = 
        new List<Account>(AccountStore.Create (ctx).FindAccountsForService (SERVICE));
    if (accounts.Count == 1) {
        GetAccount (accounts [0]);
        return;
    }

    var auth = new OAuth2Authenticator (
        clientId: FBID,
        scope: string.Empty,
        authorizeUrl: new Uri (AUTH_URL),
        redirectUrl: new Uri (REDIRECT_URL));

    auth.Completed += (sender, eventArgs) => {
        AccountStore.Create (ctx).Save (eventArgs.Account, "Facebook");
        GetAccount (eventArgs.Account);
    };
    ctx.StartActivity (auth.GetUI (ctx));
}

问题是,在我在 FB 登录页面中输入我的凭据后,会在它到达Completed事件之前引发异常。
我已经从 GitHub 下载了Xamarin.Auth项目,试图调试该程序,但不幸的是它并没有在断点处中断。

Caused by: JavaProxyThrowable: System.NullReferenceException: Object reference not set to an instance of an object
at Xamarin.Auth.OAuth2Authenticator.OnRetrievedAccountProperties (System.Collections.Generic.IDictionary`2) [0x00017] in d:\Downloads\Xamarin.Auth-master\Xamarin.Auth-master\src\Xamarin.Auth\OAuth2Authenticator.cs:373
at Xamarin.Auth.OAuth2Authenticator.OnRedirectPageLoaded (System.Uri,System.Collections.Generic.IDictionary`2,System.Collections.Generic.IDictionary`2) [0x00016] in d:\Downloads\Xamarin.Auth-master\Xamarin.Auth-master\src\Xamarin.Auth\OAuth2Authenticator.cs:282
at Xamarin.Auth.WebRedirectAuthenticator.OnPageEncoun...[intentionally cut off]

我现在在这个问题上挣扎了一段时间。请帮忙!

4

1 回答 1

0

我找到了!这是多种情况。
我的调试器没有停在断点处(不知道为什么)。
导致问题的原因是我在方法中使用Login上述方法创建了一个对象OnCreate()
然后我将一个 EventHandler 附加到该对象的一个​​事件上。
Authenticator 从他的 Intent 返回的那一刻,我的对象绑定的上下文就消失了。
理解起来可能有点模糊,但也许一些代码会进一步澄清。

//Not working, causes the problem
public class MyActivity {
    MyAuthenticator auth; //the object containing Login();
    public void OnCreate() {
        auth=new MyAuthenticator();
        auth.LoggedIn += blabla;
    }
    public void SomeMethod() {
        auth.Login();
    }
}

解决方案:

//Working, own scope
public class MyActivity {
    public void OnCreate() {
        //ILB
    }
    public void SomeMethod() {
        var auth=new MyAuthenticator();
        auth.LoggedIn += blabla;
        auth.Login();
    }
}
于 2015-05-18T10:31:04.887 回答