0

I recently developed a iOS and Android app with Xamarin. The app was used by a relative small number of users on a business event where I was personally present. Although the app ran find on all iOS devices and most Android I had a few Android devices where the app was consistently crashing at the same place. I decided to submit two crash reports which I later investigated in Goole Developer Console.

It turned out the app crashed on: java.lang.NoSuchMethodError: no non-static method "Landroid/view/View;.setForegroundTintList(Landroid/content/res/ColorStateList;)V" It looks like it's crashing on the call to setForegroundTintList. I've set my app for compatibility to API level 15 and this is documented to be only available from API level 21. So, here you go...

But wait, no, the devices from which I submitted the crash reports were running Android 5.0 and 5.1 and both should support API level 21 and up. One of the devices is a Samsung Galaxy Core Prime (Android 5.1) and the other is a Samsung Galaxy S5 (klte), supposedly running Android 5.0 according to the crash report, although the developer console list it (being shipped??) with Android 4.0.3 - 4.0.4.

I'm very new to Xamarin and Android development so can anybody explain this? And what should I do to prevent this in the future?

Set up more emulators for some key versions of Android and test in more emulators? Is there anything the compiler can do for me. Even when I lower the target API level to API level 15 it compiles and builds fine. Shouldn't it complain about using methods not available in the target API level?

SushiHangover
  • 73,120
  • 10
  • 106
  • 165
Marco Miltenburg
  • 6,123
  • 1
  • 29
  • 29

1 Answers1

2

The Target Framework is used at compile time, for example, set it to API19, then you would receive an compile error on your ForegroundTintList property:

Error CS1061: Type Android.Widget.Button does not contain a definition for ForegroundTintList

Of course, you will not be able to use any newer APIs as they will be compiler errors now.

Minimum Android Version is used at run-time and thus you would need to do runtime checking to avoid failing on older device API levels

if (Build.VERSION.SdkInt >= BuildVersionCodes.Lollipop)
{
    var x = button.ForegroundTintList;
}

Ref: Understanding Android API Levels

SushiHangover
  • 73,120
  • 10
  • 106
  • 165
  • I already tried that but it compiles just fine (with Visual Studio for Mac = Xamarin Studio). I'm using it on a `Android.Support.V7.Widget.Toolbar` and thus use the `Xamarin.Android.Support.v7.AppCompat` package. That probably explains why it compiles even down to API level 15. But it still does not explain why it wouldn't work on these two specific devices (or Android versions). – Marco Miltenburg Feb 09 '17 at 16:30
  • Sorry, just noticing the difference between **Target framework** and **Target Android version**. +1 for the link. – Marco Miltenburg Feb 09 '17 at 16:37