我尝试执行以下教程:
namespace App1
{
[Activity(Label = "App1", MainLauncher = true, Icon = "@drawable/icon")]
public class MainActivity : Activity
{
int count = 1;
protected override void OnCreate(Bundle bundle)
{
base.OnCreate(bundle);
// Set our view from the "main" layout resource
SetContentView(Resource.Layout.Main);
// Get the latitude/longitude EditBox and button resources:
EditText latitude = FindViewById<EditText>(Resource.Id.latText);
EditText longitude = FindViewById<EditText>(Resource.Id.longText);
Button button = FindViewById<Button>(Resource.Id.getWeatherButton);
// When the user clicks the button ...
button.Click += async (sender, e) => {
// Get the latitude and longitude entered by the user and create a query.
string url = "http://api.geonames.org/findNearByWeatherJSON?lat=" +
latitude.Text +
"&lng=" +
longitude.Text +
"&username=demo";
// Fetch the weather information asynchronously,
// parse the results, then update the screen:
JsonValue json = await FetchWeatherAsync(url);
// ParseAndDisplay (json);
};
}
// Gets weather data from the passed URL.
private async Task<JsonValue> FetchWeatherAsync(string url)
{
// Create an HTTP web request using the URL:
HttpWebRequest request = (HttpWebRequest)HttpWebRequest.Create(new Uri(url));
request.ContentType = "application/json";
request.Method = "GET";
// Send the request to the server and wait for the response:
using (WebResponse response = await request.GetResponseAsync())
{
// Get a stream representation of the HTTP web response:
using (Stream stream = response.GetResponseStream())
{
// Use this stream to build a JSON document object:
JsonValue jsonDoc = await Task.Run(() => JsonObject.Load(stream));
Console.Out.WriteLine("Response: {0}", jsonDoc.ToString());
// Return the JSON document:
return jsonDoc;
}
}
}
}
}
但是当我调试弹出窗口时会显示以下消息:“System.InvalidCastException:无法将类型'Android.Widget.EditText'的实例转换为'Android.Widget.Button'。”
我添加了主要的布局代码,因为Xamarin在编辑文本时显示了错误:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<LinearLayout
android:orientation="horizontal"
android:layout_marginTop="10dp"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center_horizontal"
android:id="@+id/latSection">
<TextView
android:text="Enter Latitude:"
android:textAppearance="?android:attr/textAppearanceMedium"
android:layout_width="140dp"
android:layout_height="40dp"
android:gravity="right"
android:id="@+id/latLabel" />
<EditText
android:text="47.7"
android:layout_width="150dp"
android:layout_height="50dp"
android:id="@+id/latText"
android:textAppearance="?android:attr/textAppearanceMedium"
android:layout_marginLeft="5dp" />
</LinearLayout>
<LinearLayout
android:orientation="horizontal"
android:layout_marginTop="10dp"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center_horizontal"
android:id="@+id/longSection">
<TextView
android:text="Enter Longitude:"
android:textAppearance="?android:attr/textAppearanceMedium"
android:layout_width="140dp"
android:layout_height="40dp"
android:gravity="right"
android:id="@+id/longLabel" />
<EditText
android:text="-122.5"
android:layout_width="150dp"
android:layout_height="50dp"
android:id="@+id/longText"
android:textAppearance="?android:attr/textAppearanceMedium"
android:layout_marginLeft="5dp" />
</LinearLayout>
<LinearLayout
android:orientation="vertical"
android:layout_marginTop="28dp"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center_horizontal"
android:id="@+id/getSection">
<Button
android:id="@+id/getWeatherButton"
android:layout_width="300dp"
android:layout_height="wrap_content"
android:gravity="center_horizontal"
android:textAppearance="?android:attr/textAppearanceLarge"
android:text="Get Weather" />
</LinearLayout>
<LinearLayout
android:orientation="horizontal"
android:layout_marginTop="30dp"
android:layout_marginLeft="40dp"
android:layout_marginRight="40dp"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/locSection">
<TextView
android:text="Location:"
android:textAppearance="?android:attr/textAppearanceMedium"
android:layout_width="120dp"
android:layout_height="30dp"
android:gravity="left"
android:id="@+id/locLabel" />
<TextView
android:text=""
android:textAppearance="?android:attr/textAppearanceMedium"
android:layout_width="wrap_content"
android:layout_height="60dp"
android:gravity="left"
android:id="@+id/locationText" />
</LinearLayout>
<LinearLayout
android:orientation="horizontal"
android:layout_marginTop="10dp"
android:layout_marginLeft="40dp"
android:layout_marginRight="40dp"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/tempSection">
<TextView
android:text="Temperature:"
android:textAppearance="?android:attr/textAppearanceMedium"
android:layout_width="120dp"
android:layout_height="30dp"
android:gravity="left"
android:id="@+id/tempLabel" />
<TextView
android:text=""
android:textAppearance="?android:attr/textAppearanceMedium"
android:layout_width="wrap_content"
android:layout_height="30dp"
android:gravity="left"
android:id="@+id/tempText" />
</LinearLayout>
<LinearLayout
android:orientation="horizontal"
android:layout_marginTop="10dp"
android:layout_marginLeft="40dp"
android:layout_marginRight="40dp"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/humidSection">
<TextView
android:text="Humidity:"
android:textAppearance="?android:attr/textAppearanceMedium"
android:layout_width="120dp"
android:layout_height="30dp"
android:gravity="left"
android:id="@+id/humidLabel" />
<TextView
android:text=""
android:textAppearance="?android:attr/textAppearanceMedium"
android:layout_width="wrap_content"
android:layout_height="30dp"
android:gravity="left"
android:id="@+id/humidText" />
</LinearLayout>
<LinearLayout
android:orientation="horizontal"
android:layout_marginTop="10dp"
android:layout_marginLeft="40dp"
android:layout_marginRight="40dp"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/condSection">
<TextView
android:text="Conditions:"
android:textAppearance="?android:attr/textAppearanceMedium"
android:layout_width="120dp"
android:layout_height="30dp"
android:gravity="left"
android:id="@+id/condLabel" />
<TextView
android:text=""
android:textAppearance="?android:attr/textAppearanceMedium"
android:layout_width="wrap_content"
android:layout_height="60dp"
android:gravity="left"
android:id="@+id/condText" />
</LinearLayout>
感谢您的帮助。
---- ----- EDIT
我完成了解决我的问题。
namespace App1
{
[Activity(Label = "App1", MainLauncher = true, Icon = "@drawable/icon")]
public class MainActivity : Activity
{
private Button button;
private EditText latitude;
private EditText longitude;
protected override void OnCreate(Bundle bundle)
{
base.OnCreate(bundle);
// Set our view from the "main" layout resource
SetContentView(Resource.Layout.Main);
// Get the latitude/longitude EditBox and button resources:
latitude = FindViewById<EditText>(Resource.Id.latText);
longitude = FindViewById<EditText>(Resource.Id.longText);
button = FindViewById<Button>(Resource.Id.getWeatherButton);
答案 0 :(得分:2)
将<Edittext>
替换为<TextView>
latitude = FindViewById<TextView>(Resource.Id.latText);
longitude = FindViewById<TextView>(Resource.Id.longText);