【发布时间】:2014-08-04 13:35:47
【问题描述】:
我遇到了一个奇怪的问题。我正在编写应用程序,它使用来自 Web 的 XML。在模拟器上一切正常,但在手机上它崩溃(点击按钮后关闭,没有任何警报)。
代码如下:
using System;
using Android.App;
using Android.Content;
using Android.Runtime;
using Android.Views;
using Android.Widget;
using Android.OS;
using System.Xml;
namespace KursyWalut
{
[Activity (Label = "KursyWalut", MainLauncher = true, Icon = "@drawable/icon")]
public class MainActivity : Activity
{
protected override void OnCreate (Bundle bundle)
{
base.OnCreate (bundle);
// Set our view from the "main" layout resource
SetContentView (Resource.Layout.Popularne);
// Get our button from the layout resource,
// and attach an event to it
Button button = FindViewById<Button> (Resource.Id.przycisk);
TextView data = FindViewById<TextView> (Resource.Id.data);
TextView kurs = FindViewById<TextView> (Resource.Id.kurs);
RadioGroup radioGroup = FindViewById<RadioGroup> (Resource.Id.radioGroup);
RadioButton dolar = FindViewById <RadioButton> (Resource.Id.rbDolar);
RadioButton euro = FindViewById <RadioButton> (Resource.Id.rbEuro);
RadioButton frank = FindViewById <RadioButton> (Resource.Id.rbFrank);
int checkedRadioButton;
XmlDocument dokument = new XmlDocument ();
// --------------------------------------------------------------------------
button.Click += delegate {
try{
dokument.Load("http://nbp.pl/kursy/xml/LastA.xml");
}
catch(Exception ex){
Toast.MakeText(this,"Brak połaczenia z Internetem!",ToastLength.Long);
}
data.Text = "Data: " + getDocumentDate(dokument);
checkedRadioButton = radioGroup.CheckedRadioButtonId;
switch(checkedRadioButton){
case Resource.Id.rbDolar:
kurs.Text = "Kurs USD: " + getRate(dokument,"USD");
break;
case Resource.Id.rbEuro:
kurs.Text = "Kurs EUR: " + getRate(dokument,"EUR");
break;
case Resource.Id.rbFrank:
kurs.Text = "Kurs CHF: " + getRate(dokument,"CHF");
break;
}
};
}
String getDocumentDate(XmlDocument xml){
return xml.SelectSingleNode("/tabela_kursow/data_publikacji").InnerText;
}
String getRate(XmlDocument xml, String currency){
foreach (XmlNode xmlNode in xml.DocumentElement) {
if (xmlNode.Name == "pozycja") {
if (xmlNode.SelectSingleNode ("kod_waluty").InnerText == currency)
return xmlNode.SelectSingleNode ("kurs_sredni").InnerText;
}
}
return "Nie znaleziono kursu";
}
}
}
【问题讨论】:
-
你是否在清单中设置了互联网权限?
-
是的,它是在android manifest中设置的
-
如果在
Toast.MakeText(this,"Brak połaczenia z Internetem!",ToastLength.Long);处下断点,Exception ex是什么异常?
标签: android xamarin xamarin.android emulation device