xamarin.forms - MvvmCross and Xamarin Forms, Android app crashes with null reference error when getting accent color -


i've created simple xamarin forms app mvvmcross try , figure out error i'm having on project. simple app available here: https://github.com/selaromdotnet/xamformsapptest

it has single page toolbar item has icon on it.

if @ commit history, project loads fine , icon too.

but in recent commit, followed guide here (https://blog.xamarin.com/material-design-for-your-xamarin-forms-android-apps/) , post here (xamarin.forms null reference exception when applying material theme) update project use mvvmcross material design.

now on android app, oncreate method mvxformsappcompatactivity crashes error:

 system.nullreferenceexception: object reference not set instance of object.    @ xamarin.forms.forms.getaccentcolor () [0x00006] inc:\buildagent3\work\ca3766cfc22354a1\xamarin.forms.platform.android\forms.cs:159    @ xamarin.forms.forms.setupinit (android.content.context activity, system.reflection.assembly resourceassembly) [0x0000c] in c:\buildagent3\work\ca3766cfc22354a1\xamarin.forms.platform.android\forms.cs:118    @ xamarin.forms.forms.init (android.content.context activity, android.os.bundle bundle) [0x00006] in c:\buildagent3\work\ca3766cfc22354a1\xamarin.forms.platform.android\forms.cs:72    @ mvvmcross.forms.droid.mvxformsandroidsetup.createviewpresenter () [0x00000] in c:\projects\mvvmcross\mvvmcross-forms\mvvmcross.forms.droid\mvxformsandroidsetup.cs:55    @ mvvmcross.droid.platform.mvxandroidsetup.createviewdispatcher () [0x00000] in c:\projects\mvvmcross\mvvmcross\droid\droid\platform\mvxandroidsetup.cs:124    @ mvvmcross.core.platform.mvxsetup.initializeviewdispatcher () [0x00000] in c:\projects\mvvmcross\mvvmcross\core\core\platform\mvxsetup.cs:260     @ mvvmcross.core.platform.mvxsetup.initializesecondary () [0x000f8] in c:\projects\mvvmcross\mvvmcross\core\core\platform\mvxsetup.cs:91     @ mvvmcross.core.platform.mvxsetup.initialize () [0x00006] in c:\projects\mvvmcross\mvvmcross\core\core\platform\mvxsetup.cs:37     @ mvvmcross.droid.platform.mvxandroidsetupsingleton.ensureinitialized () [0x0004e] in  c:\projects\mvvmcross\mvvmcross\droid\droid\platform\mvxandroidsetupsingleton.cs:45    @ mvvmcross.forms.droid.mvxformsappcompatactivity.oncreate (android.os.bundle bundle) [0x00013] in c:\projects\mvvmcross\mvvmcross-forms\mvvmcross.forms.droid\mvxformsappcompatactivity.cs:64    @ xamformsapptest.droid.mainactivity.oncreate (android.os.bundle bundle) [0x00017] in d:\visualstudio\oss\xamformsapptest\xamformsapptest\xamformsapptest.android\mainactivity.cs:21    @ android.support.v4.app.fragmentactivity.n_oncreate_landroid_os_bundle_(system.intptr jnienv, system.intptr native__this, system.intptr native_savedinstancestate) [0x0000f] in 8b5e0f4c6f594871ab0afcbcb2317289>:0      @ (wrapper dynamic-method) system.object:6ce77b2f-ee95-4ad0-bd89-6baa2e4d7b05> (intptr,intptr,intptr) 

i'm guessing it's not finding color correctly in resources @ loss how troubleshoot problem. tried changing filename color.xml , tried defining accentcolor color instead of reference @color/accent...

i'm stuck here , have no idea how fix this. can tell me i'm doing wrong project?

object reference not set instance of object.

first, please make sure have read official tutorial create mvvmcross project xamarin.forms. here sample how create mvvmcross forms project. please modify mainpage.xaml code according document.

we find nullreferenceexception has order :

mainactivity.oncreate --> mvxformsappcompatactivity.oncreate --> .. --> init --> setupinit --> getaccentcolor 

it null when system call mainactivity.oncreate method, can infer not finding color correctly in resources because mainactivity hasn't been initialized. should call mainactivity's oncreate method after prepared. seems we must add splashscreen initialize resource.

here code :

[activity(    label = "mvxforms"    , mainlauncher = true    , icon = "@drawable/icon"    , nohistory = true    , screenorientation = screenorientation.portrait)] public class splashscreen : mvxsplashscreenactivity {     public splashscreen()         : base(resource.layout.splashscreen)     {     }      protected override void triggerfirstnavigate()     {         startactivity(typeof(mainactivity));         base.triggerfirstnavigate();     } }  

then, nullreferenceexception gone, after doing this, still have exception :

 fatal unhandled exception: mvvmcross.platform.exceptions.mvxexception:  problem seen creating view-viewmodel lookup table   - have more 1 view registered viewmodels: 2*mainviewmodel (mainactivity,mainpage)   ---> system.argumentexception: item same key has been added. 

if viewmodel called mainviewmodel , forms page too, might name conflict because mvvmcross have 2 view viewmodel lookups. can prevent naming activity differently "formsactivity.cs".

you rename mainviewmodel mvxmainviewmodel(whatever like), exception disappear.


Comments

Popular posts from this blog

android - InAppBilling registering BroadcastReceiver in AndroidManifest -

python Tkinter Capturing keyboard events save as one single string -

sql server - Why does Linq-to-SQL add unnecessary COUNT()? -