jamesmontemagno / Xam.NavDrawer

Navigation Drawer Sample + MvvmCross Sample for Xamarin.Android
122 stars 96 forks source link

OnCreateView Null Reference Exception with Developer Options "Don't Keep Activities" #2

Open benhysell opened 10 years ago

benhysell commented 10 years ago

Steps to Reproduce

  1. Turn on 'Don't keep activities' on android device under developer options menu
  2. Run MvxSample.Droid on device
  3. Select Friends from the slide out menu
  4. Pick a friend, monkey picture is shown
  5. Click on the Money's name to return to the view with all of the friends being shown
  6. Crash

The ViewModel.FriendsAllViewModel is null in FriendsView.cs line 51.

I've been chasing a similar issue in my code using this project as a base, http://stackoverflow.com/questions/24145410/mvvmcross-android-null-reference-for-viewmodel-when-reloading-fragments

Not sure if this is a mvvmcross issue or an implementation with the navDrawer. Thoughts?

jamesmontemagno commented 10 years ago

It is an issue with Don't keep activities for development. It would Kill the main activity when you move away from it. I wouldn't really worry about that though.

benhysell commented 10 years ago

I realize the switch is what is causing this :-)

What it is allowing me to do is simulate an issue that is experienced in the field with my beta testers. For some reason every beta tester with a Samsung device triggers this null reference exception. These are people without developer options turned on.

I can't simulate it with a N5 attached to the debugger, but if I turn on the developer options and flip the switch I can recreate what they are seeing...hence it felt like 'a something'.

Would love to track this one down if you had an idea on where to start.

jamesmontemagno commented 10 years ago

I can take a look at it. What samsung device?

benhysell commented 10 years ago

Appreciate it!

tldr - S3 and S5 are crashing during beta for enterprise app. Crash logs report a null ref exception in a fragment for OnCreate(), see http://pastebin.com/3t7MQnhr for crash log of app in beta on a S3. I'm not apart of the Xamarin.TestCloud so I can't see it run myself, just getting crash logs via HockeyApp. Null reference is caused by attempting to access a ViewModel for a Fragment while it is being created, and the ViewModel is null.

Things I've Tried - I can't reproduce the crash on a Nexus 5...no matter what I try, just the S3 and S5, and very consistently. If I turn on 'Don't keep activities' on the Nexus 5, and I can create a crash log exactly like S3 and S5.

Root Cause, I believe - With the switch on, OnDestory() is called on HomeView() removing it and all of the fragments once the FriendsView is shown. This feels like it could happen in the wild, for one reason or another...hence I'm simulating it with the switch seems like a valid test path.

My current thinking, I'm not sure if there is something wrong with mvvmcross and fragments or with the implementation of fragments/drawer in Xam.NavDrawer. My thought is, even if HomeView is destroyed, for whatever reason, that when there is a call to bring it back it should come back to life without a null reference exception.

benhysell commented 10 years ago

If this helps: http://stackoverflow.com/a/24156426/7561

benhysell commented 10 years ago

I took a stab at fixing this...not sure that this is the best approach but I have eliminated the null reference exceptions: http://benjaminhysell.com/archive/2014/06/mvvmcross-flyoutnavigation-hamburger-menu-sliding-menu-for-android-null-reference-exception-on-fragment-shown-fix/