user1672867
user1672867

Reputation: 75

Android Development in Xamarin - Error

I am using Xamrin to develop an Android application but have been having a problem. The code i have builds fine but when testing it in the android virtual device API10 I get the following error message: "Unable to convert instance type 'Android.Widget.TextView' to type 'android/widget/Button'"

As far as I know I am not trying to do that. Below is some code and the Main.axml. It states that the error occurs on line 23 which is a comment.

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>

Any help would be greatly appreciated. I figure it is something stupid I am doing wrong but it was working fine until suddenly it broke.

Thanks

Upvotes: 1

Views: 3107

Answers (4)

htam
htam

Reputation: 11

thank Ben Peretz, "Easier solution: Uninstall the app from the virtual device". I fixed the error: System.InvalidCastException: Unable to convert instance of type 'Android.Widget.Chronometer' to type 'Android.Support.V7.Widget.Toolbar'.

Upvotes: 0

Ben Peretz
Ben Peretz

Reputation: 49

Easier solution: Uninstall the app from the virtual device, and then just run it from Visual Studio as usual, which will make VS reinstall the app on the device.

Upvotes: 4

I guess if you close and relaunch the emulator, it should work. I faced this myself and after closing and relaunching the emulator, it work perfectly well. I think it is not updating the xml or so.

Upvotes: 1

user1672867
user1672867

Reputation: 75

I can now confirm that this was an issue with the android virtual device and not the code. After recreating a new virtual device on API12 this now works fine. Not sure why this happened.

Thanks

Upvotes: 0

Related Questions