Bug 54717 - System.NullReferenceException Xamarin.Forms.Platform.Android.PlatformConfigurationExtensions.OnThisPlatform[T] (T element)
Summary: System.NullReferenceException Xamarin.Forms.Platform.Android.PlatformConfigur...
Status: VERIFIED FIXED
Alias: None
Product: Forms
Classification: Xamarin
Component: Android (show other bugs)
Version: 2.3.4
Hardware: PC Windows
: Normal major
Target Milestone: ---
Assignee: Stephane Delcroix
URL:
Depends on:
Blocks:
 
Reported: 2017-04-07 10:26 UTC by jerone
Modified: 2017-06-29 10:09 UTC (History)
9 users (show)

See Also:
Tags: ac
Is this bug a regression?: Yes
Last known good build: 2.3.3.193


Attachments
Reproducable project (98.51 KB, application/x-zip-compressed)
2017-04-09 17:41 UTC, jerone
Details

Description jerone 2017-04-07 10:26:41 UTC
When upgrading to XF 2.3.4.224 our app stopped working completely. There was no issue when run with XF 2.3.3.193.

No visual exception throw. Only in logcat we found the following exception being thrown:

```
Time	Device Name	Type	PID	Tag	Message
04-07 11:36:28.345	 5.7" Marshmallow (6.0.0) XHDPI Phone	Info	2975	MonoDroid	System.NullReferenceException: Object reference not set to an instance of an object.
  at Xamarin.Forms.Platform.Android.PlatformConfigurationExtensions.OnThisPlatform[T] (T element) [0x00000] in C:\BuildAgent\work\ca3766cfc22354a1\Xamarin.Forms.Platform.Android\PlatformConfigurationExtensions.cs:8 
  at Xamarin.Forms.Platform.Android.FormsAppCompatActivity.OnResume () [0x00006] in C:\BuildAgent\work\ca3766cfc22354a1\Xamarin.Forms.Platform.Android\AppCompat\FormsAppCompatActivity.cs:235 
  at Android.App.Activity.n_OnResume (System.IntPtr jnienv, System.IntPtr native__this) [0x00009] in /Users/builder/data/lanes/4468/b16fb820/source/monodroid/src/Mono.Android/platforms/android-25/src/generated/Android.App.Activity.cs:4191 
  at (wrapper dynamic-method) System.Object:2ef5ac0f-cae6-429e-acb2-8011b5d3b507 (intptr,intptr)
```

After some digging we found the root of our crash; which was the following code in our XAML contentpage view:
```
<ContentPage.Resources>
	<ResourceDictionary>
		<x:Array x:Key="SomeColors" Type="{x:Type Color}">
			<x:StaticResource Key="Color1" />
			<x:StaticResource Key="Color2" />
		</x:Array>
	</ResourceDictionary>
</ContentPage.Resources>
```

`<x:StaticResource>` reference to some resources (Color1 & Color2) we have defined in our `app.xaml`.
We needed an `x:Array` combined with a custom converter to select a color based on a condition.
My guess is that it doesn't appear to be related to `<x:Array>`, but to `<x:StaticResource>` within the `<ContentPage.Resources>` in a contentpage.

All our XAML is compiled (`[assembly: XamlCompilation(XamlCompilationOptions.Compile)]`).

I have to stress that above code worked perfectly in XF 2.3.3.193 and I can't find any issue related to this pointing out about a breaking change.
Comment 1 Paul DiPietro [MSFT] 2017-04-07 14:51:22 UTC
What have you tried to do to remedy the issue thus far? Have you attempted to wipe the bin/obj folders, remove the app from the emulator/device, and rebuild the solution itself? If you've done all of those things and are still experiencing the issue, can you reliably reproduce it in a separate project (with minimal code necessary) and provide it to us?
Comment 2 jerone 2017-04-09 17:41:19 UTC
Created attachment 21365 [details]
Reproducable project

Attached is a zipped folder, contains two identical projects for reproducing this bug:
* XF_bug_54717_A is a working project with XF 2.3.3.193.
* XF_bug_54717_B is a breaking project with XS 2.3.4.224.

The failing file is `XF_bug_54717\Views\HomeView.xaml`, lines 10 and 11.
Comment 3 jerone 2017-04-09 17:43:43 UTC
And yes, I wiped the bin/obj folders, removed the app form the emulator/device, rebuild/cleaned the solution, updated packages. The issue reproducible on 3 computers (all Windows 10 Pro x64).
Comment 4 jerone 2017-04-09 17:58:05 UTC
Steps to reproduce:
1. Download above attachment.
2. Open project XF_bug_54717_A.
3. Restore packages.
4. Start Android project.
5. See home view with "Hello World" text.
6. Open project XF_bug_54717_B.
7. Restore packages.
8. Start Android project.
9. See NO home view, only a white empty screen.
Comment 5 Tma 2017-04-10 15:13:46 UTC
I met the same issue. When I tried to start my custom activity through intent and get my app crush. I have added try/catch in custom activity. It is kind of working for me now.

protected override  void OnResume()
        {
         
            try
            {
                base.OnResume();
            }
            catch (Exception)
            {
                // ignored
            }
        }
Comment 6 jerone 2017-04-12 18:15:32 UTC
@PaulDiPietro Can you reproduce it with attached project? Do I have to deliver something else?
Comment 7 Paul DiPietro [MSFT] 2017-04-13 15:20:16 UTC
Yes, it seems like there's some kind of regression between the two versions and it occurs on the nightly build as well, but will require further investigation.
Comment 8 Chris King 2017-04-17 17:54:48 UTC
Verified the bug. Pinged Stephane. Regression is so old it would be extra painful to do a bisection; regression introduced before nightly builds.
Comment 9 Stephane Delcroix 2017-04-19 13:05:03 UTC
https://github.com/xamarin/Xamarin.Forms/pull/875
Comment 10 Samantha Houts [MSFT] 2017-04-27 19:39:42 UTC
Should be fixed in 2.3.5 stable. Thank you!
Comment 11 Saurabh Paunikar 2017-06-29 10:09:00 UTC
Fixed verified on 
Xamarin.Forms version 2.3.5-pre5

Microsoft Visual Studio Enterprise 2015
Version 14.0.25431.01 Update 3
Microsoft .NET Framework
Version 4.6.01586

Installed Version: Enterprise

Xamarin   4.6.0.279 (de55d19)
Xamarin.Android   7.4.0.13 (f6289cd)
Xamarin.iOS   10.12.0.12 (494fcbc)

Note You need to log in before you can comment on or make changes to this bug.