2015-01-14 55 views
0

我在我的Xamarin.IOS統一應用程序中使用Nuget的LinqToTwitter v3.1.1和僅使用應用程序的身份驗證,以使用此良好庫的搜索選項顯示來自TwitterAccount的推文列表。但是,執行簡單搜索時,我在TwitterQueryProvider上收到未處理的NullReference異常。NullReferenceException使用LinqToTwitter搜索功能

我寫的代碼如下:下面的堆棧跟蹤的

var credentialStore = new InMemoryCredentialStore 
{ 
    ConsumerKey = "MyTwitterKey", 
    ConsumerSecret = "MyTwitterSecret" 
}; 

var authorizer = new ApplicationOnlyAuthorizer { CredentialStore = credentialStore }; 
await authorizer.AuthorizeAsync(); 

var twitterCtx = new TwitterContext(authorizer); 

var searchResponse = await (from search in twitterCtx.Search 
          where search.Type == SearchType.Search 
           && search.Query == "Microsoft" 
          select search) 
           .SingleOrDefaultAsync(); 

if (searchResponse != null && searchResponse.Statuses != null) 
{ 
    foreach (var tweet in searchResponse.Statuses) 
    { 
     Debug.WriteLine("User: {0}, Tweet: {1}", tweet.User.ScreenNameResponse, tweet.Text); 
    } 
} 

部分:在執行搜索並投入使用var searchResponse

{System.NullReferenceException: Object reference not set to an instance of an object 
at LinqToTwitter.TwitterQueryProvider+<ExecuteAsync>d__6`1[System.Collections.Generic.IEnumerable`1[LinqToTwitter.Search]].MoveNext() [0x00000] in <filename unknown>:0 
--- End of stack trace from previous location where exception was thrown --- 

at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw() [0x0000b] in [somepath]/System.Runtime.ExceptionServices/ExceptionDispatchInfo.cs:62 
at System.Runtime.CompilerServices.ConfiguredTaskAwaitable`1+ConfiguredTaskAwaiter[System.Object].GetResult() [0x00034] in [somepath]/System.Runtime.CompilerServices/ConfiguredTaskAwaitable_T.cs:62 
at LinqToTwitter.TwitterExtensions+<ToListAsync>d__11`1[LinqToTwitter.Search].MoveNext() [0x00000] in <filename unknown>:0 
--- End of stack trace from previous location where exception was thrown --- 

at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw() [0x0000b] in [somepath]/System.Runtime.ExceptionServices/ExceptionDispatchInfo.cs:62 
at System.Runtime.CompilerServices.TaskAwaiter`1[System.Collections.Generic.List`1[LinqToTwitter.Search]].GetResult() [0x00034] in [somepath]/System.Runtime.CompilerServices/TaskAwaiter_T.cs:59 
at [somepath].TwitterService+<GetTweets>d__2.MoveNext() [0x0014f] in [somepath]\TwitterService.cs:27 } 

的錯誤發生。我已驗證身份驗證已成功並設置了bearertoken。除了Joe Mayo自己在LINQ to Twitter Project site on Codeplex上提供的簡單例子外,沒有其他花哨的東西被完成。

我也試過一些不使用LinqToTwitter的工作(使用相同的憑據)直到接收推文列表,但由於序列化的原因,我選擇了LinqToTwitter。

感覺就像我錯過了一些明顯的東西,例如設置某個標記或授權某處,但無法找到它。包含來自codeplex源文件的演示控制檯項目似乎工作得很好。這裏的任何想法?

+0

任何人,任何人?仍然有效! – Hutjepower

回答

0

爲了歷史的目的,我欠你們一個可能的解決方案的答案。

簡而言之:手動編輯NuGet的packages.config爲linqtotwitter設置一個小寫的id似乎可以消除我所有的問題。

問題從來沒有真正能夠解決自己。我現在正在使用v3.1.2,並且還遇到了無法恢復的軟件包問題(注意:只有Mac上的Xamarin Studio區分大小寫)。但是,由於NuGet,我碰到了遇到類似問題的用戶this topic。我手動編輯packages.config(在PCL和iOS項目中)以聲明id =「linqtotwitter」而不是「LinqToTwitter」,它向我展示了我期待的魔力...

希望這會有所幫助...

-1

我將你的搜索代碼複製到我自己的應用程序中,它執行得很好。我在代碼中使用SingleUserAuthorizer而非ApplicationOnlyAuthorizer但唯一的區別,否則和我是我:

  • 包括accessTokenaccessTokenSecret
  • 我沒有行:await authorizer.AuthorizeAsync();

希望這有點幫助

+0

感謝您的評論,但是因爲我使用這個特定的ApplicationOnlyAuthorizer作爲應用程序,所以我無法創建一個幽靈用戶...... – Hutjepower