0

I created a DatePicker in Xamarin, and I have a problem with it. I tried to make a code for calculating a person's age by adding the born year, month and day. However the code after debugging stopped at the point when I converted the number of years into string, giving me the error: "A Android.Content.Res.Resources+NotFoundException was thrown. String resource ID #0x2"

Xamarin code:

        base.OnCreate (bundle);

        SetContentView (Resource.Layout.AddDataLayout);

        Button regbutton = FindViewById<Button> (Resource.Id.regButton);
        DatePicker date = FindViewById<DatePicker> (Resource.Id.datePicker);

        //regbutton.Enabled = false;



        regbutton.Click += delegate 
        {
            DateTime zeroTime = new DateTime(1, 1, 1);
            DateTime BornDate = date.DateTime;
            DateTime sToday = DateTime.Today;
            TimeSpan span = sToday - BornDate;
            int years = (zeroTime + span).Year - 1;
            string a = Convert.ToString(years);
            Toast.MakeText(this,a , ToastLength.Long).Show();
            this.Finish();
        };
gepont
  • 5
  • 1
  • Will it throw an exception if you just do years.ToString()? Btw the check proper implementation on how to calc age based on birthdate - http://stackoverflow.com/questions/9/how-do-i-calculate-someones-age-in-c – Ivan Zub Sep 02 '14 at 06:32
  • 2
    Given the exception I think the question has the wrong title. – Dirk Sep 02 '14 at 06:38
  • I used the other method what Ivan mentioned, and now I get the problem at the end of the OnCreate method as "String resource ID #0x14". I also write a code in Visual Studio as a Consol App. and it just worked fine. – gepont Sep 02 '14 at 07:17

1 Answers1

-2
Calendar firstCalendar = Calendar.getInstance();        

Calendar secondCalendar = Calendar.getInstance();

long diff = currentCalendar.getTimeInMillis() - calendar.getTimeInMillis();
int sec = (int) (diff/1000);
int min = sec/60;
int hr = (int) (min/60);
int days = hr/24;
int month = days/30;
int years = month/12



if(sec>60){
        if(min>60){
            if(hr>24){
                if(days>30){
                    if(month>12){
                        submitDiffrence = Integer.toString(month)+" Years ago";
                    }else{
                        submitDiffrence = Integer.toString(month)+" month ago"; 
                    }
                }else{
                    submitDiffrence = Integer.toString(days)+" days ago";
                }
            }else{
                submitDiffrence = Integer.toString(hr)+" hour ago";
            }
        }else{
            submitDiffrence = Long.toString(min)+" min ago";
        }
    }else{
        submitDiffrence = Long.toString(sec)+" sec ago";
    }
rahultheOne
  • 154
  • 1
  • 8