Xamarin中的Android开发 - 错误

时间:2014-12-28 19:59:42

标签: c# android xamarin xamarin.android

我正在使用Xamrin开发Android应用程序,但一直有问题。我建立的代码很好,但在android虚拟设备API10中测试时,我收到以下错误消息:“无法将实例类型'Android.Widget.TextView'转换为'android / widget / Button'”

据我所知,我并不想这样做。下面是一些代码和Main.axml。它声明错误发生在第23行,这是一个评论。

using System;
using System.IO;
using System.Net;
using System.Net.Sockets;
using System.Text;
using System.Threading;
using Android.App;
using Android.Content;
using Android.Runtime;
using Android.Views;
using Android.Widget;
using Android.OS;

namespace Application
{
    [Activity (Label = "Application", 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.Main);

            // Get our buttons from the "main" layout resource,
            Button btnSendMessage = FindViewById<Button> (Resource.Id.btnSendMessage);
            Button btnListen = FindViewById<Button> (Resource.Id.btnListen);
            Button btnNetwork = FindViewById<Button> (Resource.Id.btnNetwork);

            // and attach an event to the buttons above
            btnSendMessage.Click += delegate 
            {
                var intent = new Intent(this, typeof(ListenActivity));
                StartActivity(intent);
            };

            btnListen.Click += delegate 
            {
                var intent = new Intent(this, typeof(SendActivity));
                StartActivity(intent);
            };

            btnNetwork.Click += delegate 
            {
                var intent = new Intent(this, typeof(NetworkActivity));
                StartActivity(intent);
            };
        }
    }
}






<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent">
    <TextView
        android:text="Description"
        android:textAppearance="?android:attr/textAppearanceMedium"
        android:layout_width="match_parent"
        android:layout_height="162.8dp"
        android:id="@+id/txtDescription" />
    <Button
        android:text="My Network Details"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:id="@+id/btnNetwork" />
    <Button
        android:text="Start TCP Listener"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:id="@+id/btnListen" />
    <Button
        android:text="Send TCP Message"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:id="@+id/btnSendMessage" />
</LinearLayout>

非常感谢任何帮助。我认为这是一个愚蠢的事我做错了但它工作正常,直到它突然崩溃。

由于

4 个答案:

答案 0 :(得分:3)

更简单的解决方案:从虚拟设备中卸载应用程序,然后像往常一样从Visual Studio运行它,这将使VS在设备上重新安装应用程序。

答案 1 :(得分:1)

我想如果你关闭并重新启动模拟器,它应该可以工作。 我自己面对这个问题,在关闭并重新启动模拟器之后,它运行得非常好。我认为它不是在更新xml左右。

答案 2 :(得分:0)

我现在可以确认这是android虚拟设备的问题,而不是代码。在API12上重新创建新的虚拟设备后,现在可以正常工作。不知道为什么会这样。

由于

答案 3 :(得分:0)

感谢Ben Peretz,“更简单的解决方案:从虚拟设备卸载应用程序”。我修正了错误:System.InvalidCastException: Unable to convert instance of type 'Android.Widget.Chronometer' to type 'Android.Support.V7.Widget.Toolbar'.