Notice (2018-05-24): bugzilla.xamarin.com is now in
Please join us on
Visual Studio Developer Community and in the
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
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.
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 184.108.40.20601 update, it was not returning null.
Steps to reproduce
1) Without running the debugger, deploy the attached project.
The project should deploy and run with no issue
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.
Xamarin Studio: 5.0.1
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.
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.
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).
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.
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.
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?
The Activity is the context :)