我创建了一个使用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]
我现在正在努力解决这个问题。请帮忙!
答案 0 :(得分:0)
我找到了!这是一个混合的情况
我的调试器没有停在断点处(不知道为什么)
造成这个问题的原因是我用Login
方法用OnCreate()
方法创建了一个Object
然后我将EventHandler附加到该Object的事件
Authenticator从他的Intent回来的那一刻,我的对象绑定的Context已经消失了
理解它可能有点模糊,但也许一些代码会进一步澄清。
//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();
}
}