Bug 41426 - DatePicker throws NullReference in combination with AppCompat
Summary: DatePicker throws NullReference in combination with AppCompat
Alias: None
Product: Forms
Classification: Xamarin
Component: Android ()
Version: 2.2.0
Hardware: PC Windows
: --- normal
Target Milestone: ---
Assignee: Bugzilla
Depends on:
Reported: 2016-06-01 09:01 UTC by Andreas
Modified: 2016-10-21 03:11 UTC (History)
5 users (show)

Tags: Android ac
Is this bug a regression?: ---
Last known good build:

Repro project (81.14 KB, application/x-zip-compressed)
2016-06-03 15:34 UTC, E.Z. Hart [MSFT]

Notice (2018-05-24): bugzilla.xamarin.com is now in read-only mode.

Please join us on Visual Studio Developer Community and in the Xamarin and Mono organizations on GitHub to continue tracking issues. Bugzilla will remain available for reference in read-only mode. We will continue to work on open Bugzilla bugs, copy them to the new locations as needed for follow-up, and add the new items under Related Links.

Our sincere thanks to everyone who has contributed on this bug tracker over the years. Thanks also for your understanding as we make these adjustments and improvements for the future.

Please create a new report on Developer Community or GitHub with your current version information, steps to reproduce, and relevant error messages or log files if you are hitting an issue that looks similar to this resolved bug and you do not yet see a matching new report.

Related Links:

Description Andreas 2016-06-01 09:01:15 UTC
This is a tricky one...
I'm bringing up the DatePicker by clicking a regular Button and doing "myPicker.Focus()". So far so good.
Now, to reproduce this issue, you need a tabbed page with THREE tabs!
On the first tab, add the datepicker and a button. Clicking on the button should focus the datepicker.
The second and third tab-pages can be any kind of pages.

This still works great!

Now, start using AppCompat as described here: 
(Deriving MainActivity from FormsAppCompatActivity and using "@style/Theme.AppCompat.NoActionBar" as theme)

Start the app. Now, klicking the button will still work fine! The datepicker will show.
First navigating to the SECOND tab, then back to the first tab and clicking the button will ALSO work as expected.

However, if you navigate to the THIRD tab and then back to the first tab and click the button.... BOOM. You have a NullReferenceException.

Stacktrace from the exception:
  at Xamarin.Forms.Platform.Android.DatePickerRenderer.OnTextFieldClicked () [0x00007] in C:\BuildAgent2\work\aad494dc9bc9783\Xamarin.Forms.Platform.Android\Renderers\DatePickerRenderer.cs:114 
  at Xamarin.Forms.Platform.Android.DatePickerRenderer.OnFocusChangeRequested (System.Object sender, Xamarin.Forms.FocusRequestArgs e) [0x00010] in C:\BuildAgent2\work\aad494dc9bc9783\Xamarin.Forms.Platform.Android\Renderers\DatePickerRenderer.cs:75 
  at (wrapper delegate-invoke) System.EventHandler`1[Xamarin.Forms.VisualElement+FocusRequestArgs]:invoke_void_object_TEventArgs (object,Xamarin.Forms.VisualElement/FocusRequestArgs)
  at Xamarin.Forms.VisualElement.Focus () [0x00021] in C:\BuildAgent2\work\aad494dc9bc9783\Xamarin.Forms.Core\VisualElement.cs:444 
  at KommateckenTest.PageOne.Button_OnClicked (System.Object sender, System.EventArgs e) [0x00002] in C:\MyProjectPath\KommateckenTest\KommateckenTest\PageOne.xaml.cs:24
Comment 1 Paul DiPietro [MSFT] 2016-06-02 15:39:20 UTC
Could you put together a reproduction sample? It'd be appreciated.
Comment 2 Andreas 2016-06-03 06:17:49 UTC
Well, I don't have the time right now, but just follow the instructions in my first message and you will have the repro. 
- Three tabs
- AppCompat
- DatePicker and button on tab one
- The button should do "dp.Focus()" on it's click event
- The two other tabs can contain empty ContentPages
- Start App, select third tab, then first tab.
- Click the button, boom!
Comment 3 E.Z. Hart [MSFT] 2016-06-03 15:34:35 UTC
Created attachment 16188 [details]
Repro project
Comment 4 Björn Bentmar 2016-09-27 14:38:48 UTC
It could work based on the complexity of your view containing the date picker. The solution atm would be to crate your content in the Onappearing method (tab1 calls appearing when swiping to tab2 from tab3). appearing events are a bit broken in FormsAppcompatactivity but this works as a workaround for the repro. Create a new ContentPage class and set it to the first tab and override its onappearing. 

protected override void OnAppearing()

			var button = new Button { Text = "Focus DatePicker" };
			var dp = new DatePicker();
			button.Clicked += (sender, args) =>
				if (dp.IsFocused)

			Content = new StackLayout
				Children = { dp, button }
Comment 5 adrianknight89 2016-10-20 23:02:17 UTC
I ran the repro in but wasn't able to reproduce the issue.
Comment 6 E.Z. Hart [MSFT] 2016-10-21 03:11:52 UTC
Yes, it appears to have been fixed somewhere between and Thanks for the heads up!