Bug 20887 - Form.Context returning null
Summary: Form.Context returning null
Status: RESOLVED INVALID
Alias: None
Product: Forms
Classification: Xamarin
Component: Forms ()
Version: 1.0.6188
Hardware: PC Mac OS
: --- normal
Target Milestone: ---
Assignee: Bugzilla
URL:
Depends on:
Blocks:
 
Reported: 2014-06-25 14:58 UTC by Allie Miller
Modified: 2014-06-25 15:59 UTC (History)
4 users (show)

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

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:
Status:
RESOLVED INVALID

Description Allie Miller 2014-06-25 14:58:16 UTC
Overview
When attempting to get the SharedPreferences in the attached project, Form.Context is returning null resulting in an error:
[MonoDroid] UNHANDLED EXCEPTION: System.NullReferenceException: Object reference not set to an instance of an object
[MonoDroid] at KawaiiNihongo.Android.DatabaseHandlerAndroid.CreateOrReadDatabase (DeckType deckType) [0x00001] in e:\Eigene Datein\Development\Apps\KawaiiNihongo\Android\InterfaceImplementation\DatabaseHandlerAndroid.cs:28 
[MonoDroid] at KawaiiNihongo.UI.MainPage.loadContent () [0x00001] in e:\Eigene Datein\Development\Apps\KawaiiNihongo\KawaiiNihongo\Layouts\MainPage.xaml.cs:31 
[MonoDroid] at KawaiiNihongo.UI.MainPage.<initForm>b__0 () [0x00000] in e:\Eigene Datein\Development\Apps\KawaiiNihongo\KawaiiNihongo\Layouts\MainPage.xaml.cs:24 
[MonoDroid] at System.Threading.Tasks.TaskActionInvoker+ActionInvoke.Invoke (System.Threading.Tasks.Task owner, System.Object state, System.Threading.Tasks.Task context) [0x00000] in <filename unknown>:0 
[MonoDroid] at System.Threading.Tasks.Task.InnerInvoke () [0x00000] in <filename unknown>:0 
[MonoDroid] at System.Threading.Tasks.Task.ThreadStart () [0x00000] in <filename unknown>:0 
[MonoDroid] --- End of stack trace from previous location where exception was thrown ---
[MonoDroid] at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw () [0x00000] in <filename unknown>:0 
[MonoDroid] at System.Runtime.CompilerServices.TaskAwaiter.GetResult () [0x00000] in <filename unknown>:0 
[MonoDroid] at KawaiiNihongo.UI.MainPage+<initForm>d__1.MoveNext () [0x000b6] in e:\Eigene Datein\Development\Apps\KawaiiNihongo\KawaiiNihongo\Layouts\MainPage.xaml.cs:24

Prior to the Xamarin.Forms 1.1.0.6201 update, it was not returning null.

Steps to reproduce
1) Without running the debugger, deploy the attached project.

Expected
The project should deploy and run with no issue

Actual
The app closes with the above error. The exception should occur in the DatabaseHandlerAndroid.cs file, line 28 under the Forms.Context.GetSharedPreferences with the context being null throwing the exception.

This issue is not reproducible in a new project.

Versioning Information
Xamarin.Forms: 1.1.0.6201
Xamarin.Android: 4.12.3
Xamarin Studio: 5.0.1
Comment 2 Jason Smith [MSFT] 2014-06-25 15:03:37 UTC
Forms.Context wont be valid until you make the SetPage call on android (and the first renderer's are created). We had to make some changes so that the context was being updated when the activity changes.
Comment 3 Maru 2014-06-25 15:12:16 UTC
The SetPage is done in the MainActivity while the Page in the parameters is trying in the code behind to get the SharedPrefs.
So the SetPage call is before trying to access Forms.Context.
Comment 4 Jason Smith [MSFT] 2014-06-25 15:31:00 UTC
It looks like you are doing it in a place in the codebehind which gets called in new MainPage(DeckType.Kana), which happens before SetPage (though you are directly passing it to SetPage).
Comment 5 Maru 2014-06-25 15:34:37 UTC
Is there a way to get the Context while initializing the Page or do I must call a method after SetPage is finished and then initialize my Page with the content I got from the SharedPrefs?

I'm asking because the content that is shown in MainPage depends on the settings within the SharedPrefs.
Comment 6 Jason Smith [MSFT] 2014-06-25 15:46:09 UTC
Currently we don't expose the context out until after SetPage is called because we form an association between pages and the Context. However you are in the android codebase there, you can easily pass the context to your service, make your own static property, whatever you want. Keep in mind that if you make a static property you need to make sure you track the activity changes/etc. Thats why Forms.Context is null until post SetPage.
Comment 7 Maru 2014-06-25 15:49:34 UTC
But how should I pass the Context when I can't get it? Sorry, but maybe I simply overseeing something.

Do you have a (simple) code sample to clarify what you exactly mean?
Comment 8 Jason Smith [MSFT] 2014-06-25 15:59:02 UTC
The Activity is the context :)