1

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

4 Answers 4

4

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.

Sign up to request clarification or add additional context in comments.

Comments

1

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.

Comments

0

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

Comments

0

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'.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.