2015-05-17 37 views
0

我创建了一个使用Facebook登录的函数。Xamarin.Auth OAuth2Authenticator Facebook NullReferenceException

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

可能重复[?什么是一个NullReferenceException,我如何修复它(http://stackoverflow.com/questions/4660142/what-is- A-的NullReferenceException和知识-DO-I-FIX-IT) –

回答

0

我找到了!这是混合的情况。
我的调试器没有停在断点处(不知道为什么)。
造成问题的原因是我在OnCreate()方法中使用上述Login方法创建了一个对象。
然后,我将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(); 
    } 
} 
相关问题