2012-05-31 15 views
2

我擺弄MonoDroid(又名Mono for Android)試圖建立一個簡單的播客下載應用程序,目前只是測試RSS提要內容的檢索。MonoDroid ObjectDisposedException當加載XDocument的RSS

我簡單的應用程序是:

  • 編輯文本,其中一個RSS源的URL輸入
  • 一個按鈕,「下載」的RSS
  • 爲下載的輸出文本顯示操作

當用戶點擊下載,我獲取RSS內容,目前使用XDocument(我也試圖與just WebRequest/Streams etc.),這裏的短版本:

void button_Click(object sender, EventArgs e) 
{ 
    output.Text += "user input: " + rssUriInput.Text + "\n"; 

    try 
    { 
     output.Text += "### document ###"; 
     output.Text += XDocument.Load(rssUriInput.Text).ToString(); 
     output.Text += "### document ###"; 
    } 
    catch (Exception ex) 
    { 
     output.Text += "OOOPS something went wrong:\n" + ex.ToString(); 
    } 
} 

我試過多種方法,但不斷收到ObjectDisposedException時候我做XDocument.Load(/*my uri here*/);

我的AssemblyInfo.cs包括在

[assembly: UsesPermission(Android.Manifest.Permission.Internet)] 

現在,免責聲明,讓後的Hello World運行我有點自己的,所以我可能會缺少一些MonoDroid基礎,但這個代碼在一個正常的C#應用​​程序(我測試它)工作,但
爲什麼我在這裏做錯了?我是否必須在MonoDroid中使用xml/linq/network訪問做一些特殊的事情?

這裏是完整的活動代碼:

using System; 

using Android.App; 
using Android.Content; 
using Android.Runtime; 
using Android.Views; 
using Android.Widget; 
using Android.OS; 
using System.Xml.Linq; 

namespace RssDownlowd 
{ 
    [Activity(Label = "RssDownlowd", MainLauncher = true, Icon = "@drawable/icon")] 
    public class Activity1 : Activity 
    { 
     EditText rssUriInput; 
     Button button; 
     TextView output; 

     protected override void OnCreate(Bundle bundle) 
     { 
      base.OnCreate(bundle); 

      SetContentView(Resource.Layout.RssDownload); 

      rssUriInput = FindViewById<EditText>(Resource.Id.rssUriText); 
      button = FindViewById<Button>(Resource.Id.downloadButton); 
      output = FindViewById<TextView>(Resource.Id.outputTextView); 

      button.Click += new EventHandler(button_Click); 
     } 

     void button_Click(object sender, EventArgs e) 
     { 
      output.Text += "user input: " + rssUriInput.Text + "\n"; 

      try 
      { 
       output.Text += "### document ###"; 
       output.Text += XDocument.Load(rssUriInput.Text).ToString(); 
       output.Text += "### document ###"; 
      } 
      catch (Exception ex) 
      { 
       output.Text += "OOOPS something went wrong:\n" + ex.ToString(); 
      } 
     } 

    } 
} 

這裏是完全不同:

"System.ObjectDisposedException: The object was used after being disposed. 

    at System.Net.WebConnection.BeginRead (System.Net.HttpWebRequest request, System.Byte[] buffer, Int32 offset, Int32 size, System.AsyncCallback cb, System.Object state) [0x00000] in <filename unknown>:0 

    at System.Net.WebConnectionStream.BeginRead (System.Byte[] buffer, Int32 offset, Int32 size, System.AsyncCallback cb, System.Object state) [0x00000] in <filename unknown>:0 

    at System.Net.WebConnectionStream.Read (System.Byte[] buffer, Int32 offset, Int32 size) [0x00000] in <filename unknown>:0 

    at System.Xml.XmlInputStream.Read (System.Byte[] buffer, Int32 offset, Int32 count) [0x00000] in <filename unknown>:0 

    at System.Xml.NonBlockingStreamReader.ReadBuffer() [0x00000] in <filename unknown>:0 

    at System.Xml.NonBlockingStreamReader.Read (System.Char[] dest_buffer, Int32 index, Int32 count) [0x00000] in <filename unknown>:0 

    at System.Xml.XmlStreamReader.Read (System.Char[] dest_buffer, Int32 index, Int32 count) [0x00000] in <filename unknown>:0 

    at Mono.Xml2.XmlTextReader.ReadTextReader (Int32 remained) [0x00000] in <filename unknown>:0 

    at Mono.Xml2.XmlTextReader.PeekChar() [0x00000] in <filename unknown>:0 

    at Mono.Xml2.XmlTextReader.ReadChar() [0x00000] in <filename unknown>:0 

    at Mono.Xml2.XmlTextReader.Expect (Int32 expected) [0x00000] in <filename unknown>:0 

    at Mono.Xml2.XmlTextReader.ReadEntityReference (Boolean ignoreEntityReferences) [0x00000] in <filename unknown>:0 

    at Mono.Xml2.XmlTextReader.ReadReference (Boolean ignoreEntityReferences) [0x00000] in <filename unknown>:0 

    at Mono.Xml2.XmlTextReader.ReadText (Boolean notWhitespace) [0x00000] in <filename unknown>:0 

    at Mono.Xml2.XmlTextReader.ReadContent() [0x00000] in <filename unknown>:0 

    at Mono.Xml2.XmlTextReader.Read() [0x00000] in <filename unknown>:0 

    at System.Xml.XmlTextReader.Read() [0x00000] in <filename unknown>:0 

    at Mono.Xml.XmlFilterReader.Read() [0x00000] in <filename unknown>:0 

    at System.Xml.Linq.XElement.LoadCore (System.Xml.XmlReader r, LoadOptions options) [0x00000] in <filename unknown>:0 

    at System.Xml.Linq.XNode.ReadFrom (System.Xml.XmlReader r, LoadOptions options) [0x00000] in <filename unknown>:0 

    at System.Xml.Linq.XContainer.ReadContentFrom (System.Xml.XmlReader reader, LoadOptions options) [0x00000] in <filename unknown>:0 

    at System.Xml.Linq.XElement.LoadCore (System.Xml.XmlReader r, LoadOptions options) [0x00000] in <filename unknown>:0 

    at System.Xml.Linq.XNode.ReadFrom (System.Xml.XmlReader r, LoadOptions options) [0x00000] in <filename unknown>:0 

    at System.Xml.Linq.XContainer.ReadContentFrom (System.Xml.XmlReader reader, LoadOptions options) [0x00000] in <filename unknown>:0 

    at System.Xml.Linq.XElement.LoadCore (System.Xml.XmlReader r, LoadOptions options) [0x00000] in <filename unknown>:0 

    at System.Xml.Linq.XNode.ReadFrom (System.Xml.XmlReader r, LoadOptions options) [0x00000] in <filename unknown>:0 

    at System.Xml.Linq.XContainer.ReadContentFrom (System.Xml.XmlReader reader, LoadOptions options) [0x00000] in <filename unknown>:0 

    at System.Xml.Linq.XElement.LoadCore (System.Xml.XmlReader r, LoadOptions options) [0x00000] in <filename unknown>:0 

    at System.Xml.Linq.XNode.ReadFrom (System.Xml.XmlReader r, LoadOptions options) [0x00000] in <filename unknown>:0 

    at System.Xml.Linq.XContainer.ReadContentFrom (System.Xml.XmlReader reader, LoadOptions options) [0x00000] in <filename unknown>:0 

    at System.Xml.Linq.XDocument.ReadContent (System.Xml.XmlReader reader, LoadOptions options) [0x00000] in <filename unknown>:0 

    at System.Xml.Linq.XDocument.LoadCore (System.Xml.XmlReader reader, LoadOptions options) [0x00000] in <filename unknown>:0 

    at System.Xml.Linq.XDocument.Load (System.String uri, LoadOptions options) [0x00000] in <filename unknown>:0 

    at System.Xml.Linq.XDocument.Load (System.String uri) [0x00000] in <filename unknown>:0 

    at RssDownlowd.Activity1.button_Click (System.Object sender, System.EventArgs e) [0x0004a] in C:\\Users\\khd483\\code\\HelloMonoDroid\\RssDownlowd\\Activity1.cs:47 " 

回答

1

當我想你的代碼,奇怪的是它的工作就好了:

Screen shot

你是否啓用鏈接我n你的項目在Mono for Android選項中?

還要考慮做一個異步下載的RSS,因爲它會阻止UI線程,直到它完成,使應用程序無響應。

+0

哇,很好!它似乎**鏈接是失蹤的一塊!** THANX我也同意異步下載,但現在我只是原型。是什麼讓你想到我連接?也許你可以鏈接(沒有雙關語意圖)相關的東西... – TJB

+1

ObjectDisposedException讓我想起它。郵件列表中的人員發生過這種情況。由於您不直接將XDocument.Load()結果保存到字段,因此鏈接器可能會將其刪除,因爲它將其視爲未被使用。所以如果你這樣做了:XDocument document = XDocument.Load(rssUriInput.Text);然後只是將document.ToString()轉儲到TextView中,它可能會啓用鏈接。這裏有一些關於它如何工作的更多信息:http://docs.xamarin.com/android/advanced_topics/linking#linkskip – Cheesebaron

+0

另外,似乎feedburner鏈接(出於某種原因)也會調用一個'ObjectDisposedException'我不確定是否因爲一些奇怪的重定向等原因這正在發生,或者有關android網絡堆棧的一些奇怪事情,但是它將我拋出了B/C,你的網址會起作用,而我的測試不會。再次感謝@ Cheesebaron – TJB