Unhandled Exception: System.InvalidCastException: Error in Visual Studio 2010

DsousaDsousa BRMember
edited October 2012 in Xamarin.Android

What's wrong in the code? This problem always occurs at compile time.
Sorry, my english is terrible, rsrs.

    var botao = FindViewById<Button>(Resource.Id.btnCalcular);
        var edtG = FindViewById<EditText>(Resource.Id.edtGasolina);
        var edtA = FindViewById<EditText>(Resource.Id.edAlcool);
        var txtR = FindViewById<TextView>(Resource.Id.txtResultado);
        double R, G, A;

        G = (double)(edtG);
        A = Convert.ToDouble(edtA); var botao = FindViewById<Button>(Resource.Id.btnCalcular);
        var edtG = FindViewById<EditText>(Resource.Id.edtGasolina);
        var edtA = FindViewById<EditText>(Resource.Id.edAlcool);
        var txtR = FindViewById<TextView>(Resource.Id.txtResultado);
        double R, G, A;

        G = (double)(edtG);
        A = Convert.ToDouble(edtA);

Thanks!

Posts

  • JonathanPryorJonathanPryor USXamarin Team Xamurai

    It would be useful if you provided the compiler error, but as a guess:

    G = (double)(edtG);

    This may not fail at compile time, but it will fail at runtime because the Java.Lang.Object-to-double conversion requires that EditText implement IConvertible, which it doesn't.

    Similarly, this will also fail at runtime:

    A = Convert.ToDouble(edtA);

    In both cases, what you want to do is use the Text property:

    G = Convert.ToDouble(edtG.Text);
    A = Convert.ToDouble(edtA.Text);
    

    The only other obvious error is that botao and other variables are declared twice, but I suspect that's a copy & paste error.

  • DsousaDsousa BRMember

    Yes, is just a copy & paste error.
    I did what you said

    G = Convert.ToDouble(edtG.Text);
    A = Convert.ToDouble(edtA.Text);

    but, now the error is: Unhandled Exception:

    System.FormatException:

    Are saying that culture is error. I live in Brazil and we use "," for decimal.

    You can tell me how to fix this?

    Thanks!

  • JonathanPryorJonathanPryor USXamarin Team Xamurai
    edited October 2012

    System.FormatException:

    I suspect that that your Thread.CurrentCulture property may be incorrect. What's the value of System.Threading.Thread.CurrentThread.CurrentCulture.DisplayName?

  • DsousaDsousa BRMember

    Sorry, but as I see it?

  • JonathanPryorJonathanPryor USXamarin Team Xamurai

    What do you get in your application output if you do:

    Console.WriteLine("DisplayName: {0}",
            System.Threading.Thread.CurrentThread.CurrentCulture.DisplayName);
    
  • DsousaDsousa BRMember

    10-26 16:33:43.421 I/ActivityThread( 1157): Pub GridView.GridView.mono.MonoRuntimeProvider.mono_init: mono.MonoRuntimeProvider

    10-26 16:33:43.491 D/dalvikvm( 1157): Trying to load lib /data/data/GridView.GridView/lib/libmonodroid.so 0x405137a0

    10-26 16:33:43.540 D/dalvikvm( 1157): Added shared lib /data/data/GridView.GridView/lib/libmonodroid.so 0x405137a0

    10-26 16:33:43.660 W/MonoDroid-Debugger( 1157): Trying to initialize the debugger with options: --debugger-agent=transport=dt_socket,loglevel=0,address=127.0.0.1:8853,server=y,embedding=1

    10-26 16:33:43.690 W/MonoDroid-Debugger( 1157): Accepted stdout connection: 34
    10-26 16:33:44.530 E/mono ( 1157): WARNING: The runtime version supported by this application is unavailable.

    10-26 16:33:44.530 E/mono ( 1157): Using default runtime: v2.0.50727
    10-26 16:33:45.790 W/monodroid-gc( 1157): GREF GC Threshold: 1800
    Loaded assembly: /data/data/GridView.GridView/files/.override/GridView.dll
    Loaded assembly: Mono.Android.dll [External]
    Loaded assembly: System.Core.dll [External]
    Loaded assembly: MonoDroidConstructors [External]

    10-26 16:33:52.840 I/mono-stdout( 1157): DisplayName: English (United States)

    DisplayName: English (United States)

    Unhandled Exception:

    System.FormatException:
    The program 'Mono' has exited with code 255 (0xff).
    In mgmain JNI_OnLoad

    is it?

  • JonathanPryorJonathanPryor USXamarin Team Xamurai

    Thank you.

    DisplayName: English (United States)

    That's the problem: English was detected as your culture, not Brazil.

    Please do the following:

    1. Open the Settings app.
    2. Tap Language & input.

    What Language is currently selected?

    If you changed your Language recently, you will need to restart the Mono for Android process for the changes to be visible: https://bugzilla.novell.com/show_bug.cgi?id=664573#c2

    You can restart the Mono for Android process by re-deploying your app to your device from within your IDE.

  • DsousaDsousa BRMember

    I don't quite understand.

    I have to change the language of ADV (Settings -> Language & keyboard -> Select
    language -> Inglês (U.S.) or Portuguese (Brazil)) and run the code by VS to recognize the modifications?

    The selected language was English (U.S.)

    Thanks!

  • JonathanPryorJonathanPryor USXamarin Team Xamurai

    Convert.ToDouble(string) uses the current culture to convert values. There is a Convert.ToDouble(string, IFormatProvider) overload that allows you to explicitly specify a culture:

    double a = Convert.ToDouble("3,14");
    // a == 314 for me, as my culture is en-US
    double b = Convert.ToDouble("3,14",
            new System.Globalization.CultureInfo("pt-BR"));
    // b == 3.14
    

    Because your Phone has English (U.S.) as the default Language setting, your app will follow English (U.S.) formatting rules, which includes using . as the decimal separator, not comma ,. This is why your Convert.ToDouble(edtA.Text) invocation is throwing a FormatException.

    There are two possible solutions:

    1. Change your devices default Language to Portuguese (Brazil) and restart your app. This should cause your app to properly interpret , as the decimal separator character.
    2. Explicitly override the current culture by using the Convert.ToDouble(string, IFormatProvider) overload, as demonstrated above.
  • DsousaDsousa BRMember

    I did what I said, but it did not work.
    The problem is in edtG.Text

    If I do

    var G = Convert.ToDouble ("2.90", new System.Globalization.CultureInfo ("pt-BR")); or

    var A = Convert.ToDouble ("2.90");

    works, but var G = Convert.ToDouble ("edtG.Text", new System.Globalization.CultureInfo ("pt-BR")), it does not accept.

    Thanks!

  • CheesebaronCheesebaron DKInsider, University mod

    G = Convert.ToDouble ("edtG.Text", new System.Globalization.CultureInfo ("pt-BR")); does not work because you have quotes around edtG.Text. So it should be:
    G = Convert.ToDouble (edtG.Text, new System.Globalization.CultureInfo ("pt-BR"));

  • DsousaDsousa BRMember
    edited October 2012

    Sorry, I wrote wrong.

    The code looks like this:

            var botao = FindViewById<Button>(Resource.Id.btnCalcular);
            var edtG = FindViewById<EditText>(Resource.Id.edtGasolina);
            var edtA = (EditText)FindViewById(Resource.Id.edAlcool);
            var txtR = (TextView)FindViewById(Resource.Id.txtResultado);
    
            var G = Convert.ToDouble(edtG.Text, new System.Globalization.CultureInfo("pt-BR"));
            var A = Convert.ToDouble("2.90");
    

    Thanks!

  • CheesebaronCheesebaron DKInsider, University mod

    I don't understand why you get this exception. Where is the code:

    var G = Convert.ToDouble(edtG.Text, new System.Globalization.CultureInfo("pt-BR"));
    var A = Convert.ToDouble("2.90");
    

    Invoked? In a button click delegate or somewhere else?

    I made a small sample which works as @jonp described:

    using System;
    using Android.App;
    using Android.Widget;
    using Android.OS;
    
    namespace EditTextTest
    {
        [Activity(Label = "EditTextTest", MainLauncher = true, Icon = "@drawable/icon")]
        public class Activity1 : Activity
        {
            protected override void OnCreate(Bundle bundle)
            {
                base.OnCreate(bundle);
    
                // Set our view from the "main" layout resource
                SetContentView(Resource.Layout.Main);
    
                var text = FindViewById<EditText>(Resource.Id.EditText);
                var tv1 = FindViewById<TextView>(Resource.Id.tv1);
                var tv2 = FindViewById<TextView>(Resource.Id.tv2);
    
                var button = FindViewById<Button>(Resource.Id.MyButton);
    
                button.Click += delegate
                                    {
                                        var val = text.Text;
                                        var dou = Convert.ToDouble(val);
                                        var G = Convert.ToDouble(text.Text, new System.Globalization.CultureInfo("pt-BR"));
                                        tv1.Text = string.Format("{0}", dou);
                                        tv2.Text = string.Format("{0}", G);
                                    };
            }
        }
    }
    
Sign In or Register to comment.