Bug 41426

Summary: DatePicker throws NullReference in combination with AppCompat
Product: Forms Reporter: Andreas <andreas>
Component: AndroidAssignee: Bugzilla <bugzilla>
Severity: normal CC: adrianknight89, bjorn.bentmar, ehart, jas, paul.dipietro
Priority: ---    
Version: 2.2.0   
Target Milestone: ---   
Hardware: PC   
OS: Windows   
Tags: Android ac Is this bug a regression?: ---
Last known good build:
Attachments: Repro project

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!