0

I'm creating a simple App in which TimePickers are used to capture the hour and the minute. I use code to do this like the following:

end.AddHours(view.FindViewById<TimePicker>(Resource.Id.endPicker).Hour);
end.AddMinutes(view.FindViewById<TimePicker>(Resource.Id.endPicker).Minute);

The problem is with the Hourand the Minute properties, while end is a DateTime.

On KitKat API lvl 19, Visual Studio gives me the following error:

NoSuchMethodError

On MarshMallow API lvl 23, however, the App runs very well. So the problem must be that the Java getCurrentMinute/Hour methods were exchanged in later APIs for the getMinute/Hour methods as it is written here. Is there a way to use the earlier methods in earlier APIs and use the most recent methods on later ones? Can I code this into my Xamarin.Android App? In Native Android I would of course check the SDK version and use Java methods accordingly, but I don't know if I can force the underlying Java code to behave this way.

UPDATE

Following the suggestion of Janmejoy , I created an if statement to check the Android version in problematic cases like this one and used deprecated code:

  if(((int)Android.OS.Build.VERSION.SdkInt) < 23)
  {
      start = new DateTime(1, 1, 1, (int)view.FindViewById<TimePicker>(Resource.Id.startPicker).CurrentHour,
      (int)view.FindViewById<TimePicker>(Resource.Id.startPicker).CurrentMinute, 0);
      end = new DateTime(1, 1, 1, (int)view.FindViewById<TimePicker>(Resource.Id.endPicker).CurrentHour,
      (int)view.FindViewById<TimePicker>(Resource.Id.endPicker).CurrentMinute, 0);
  }

This seemed to work, however, my App was extremely slow on the emulated KitKat device (it runs flawlessly on a 1 GB Marsh, but it appears to be buggy on an earlier device with 762 MB RAM). What is worse, the App now looks the same on both Marsh and KitKat exhibiting the same buggy behavior. I deleted the App from both devices, commented out the deprecated code and done a RebuildAll, but this strange behavior still persists. Has anyone seen any similar? What might be the problem?

oneManArmin
  • 606
  • 6
  • 24

1 Answers1

1

In Native Android I would of course check the SDK version and use Java methods accordingly,

Yes You are right ! You can try like this in Xamarin also

if (((int)Android.OS.Build.VERSION.SdkInt) >= 23){               
//
}
else{//}

@see https://developer.xamarin.com/api/property/Android.OS.Build+VERSION.SdkInt/

Janmejoy
  • 2,721
  • 1
  • 20
  • 38
  • Thank you for your answer, however, this does not answer my question regarding how I could somehow _force_ C# to compile to a now deprecated underlying Java code, if this is even possible. In other words, my problem is that I don't know how to _modify the Java code_ that the C# code compiles to. – oneManArmin Aug 18 '17 at 14:24