Xamarin FindViewById returns null

后端 未结 3 626
有刺的猬
有刺的猬 2020-12-20 15:38

Hi guys I need a little advice. I trying to made an android app using Xamarin so C#. I made two layouts and in each one of them I made tow buttons to navigate between them.I

3条回答
  •  無奈伤痛
    2020-12-20 16:31

    Since it just happened to me and almost drove me up the wall, here is yet another way FindViewById can return null and how to fix it. Probably not a mistake a more experienced Android developer is likely to make, but still, it might prevent some frustration for someone like me in the future.

    My situation

    • My control was in the correct view (not a fragment), the ID was assigned correctly, everything used to work just a couple of days ago when I last worked on the project.

    • After building the project, the very first FindViewById call in OnCreate of my main view suddenly returned null when run on a physical device

    • It turns out I had just run some updates through the Android SDK Manager, which introduced a new Android Version (in this case Android 7) to my system, and my device simply did not have that version installed yet.

    The solution

    • In the properties of my Android project, the "Compile using Android version" drop down was set to "Use Latest Platform", which now was pointing to the newly installed Android 7 -- simply setting it to the version of Android running on my test device (in my case 6) and recompiling fixed the null return value.

    TL;DR Check that the version of Android you are compiling against is in fact supported by the device you are testing on.

提交回复
热议问题