gpt4 book ai didi

c# - Xamarin 中的 Android 开发 - 错误

转载 作者:太空宇宙 更新时间:2023-11-03 20:01:31 24 4
gpt4 key购买 nike

我正在使用 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>

如有任何帮助,我们将不胜感激。我认为我做错了一些愚蠢的事情,但它工作正常,直到它突然坏了。

谢谢

最佳答案

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

关于c# - Xamarin 中的 Android 开发 - 错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27680092/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com