Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Xamarin "attempt to invoke virtual method 'void android.view.View.unFocus (android.view.View)' on a null object reference"

I am developing an app using Xamarin.Forms for cross platform use. Recently I updated Xamarin Studio on Mac along with updating NuGet Packages.

I am now recieving this error: attempt to invoke virtual method 'void android.view.View.unFocus (android.view.View)' on a null object reference. (See stack trace below)

This occurs when navigating away from (or re-rendering) any page except the root view. I don't believe I've changed any part of my navigation or page rendering.

I'm sorry if this somewhat vague, it appears from the stack trace that this isn't starting from my code (in any traceable way). And I'm not sure where to begin debugging. Any advice would be very helpful!

Stack Trace

--- End of managed Java.Lang.NullPointerException stack trace ---
java.lang.NullPointerException: Attempt to invoke virtual method 'void android.view.View.unFocus(android.view.View)' on a null object reference
    at android.view.ViewGroup.removeViewInternal(ViewGroup.java:4937)
    at android.view.ViewGroup.removeViewAt(ViewGroup.java:4899)
    at android.support.v4.app.FragmentManagerImpl.moveFragmentToExpectedState(FragmentManager.java:1540)
    at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1595)
    at android.support.v4.app.BackStackRecord.executeOps(BackStackRecord.java:757)
    at android.support.v4.app.FragmentManagerImpl.executeOps(FragmentManager.java:2355)
    at android.support.v4.app.FragmentManagerImpl.executeOpsTogether(FragmentManager.java:2146)
    at android.support.v4.app.FragmentManagerImpl.optimizeAndExecuteOps(FragmentManager.java:2098)
    at android.support.v4.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:2008)
    at android.support.v4.app.FragmentManagerImpl$1.run(FragmentManager.java:710)
    at android.os.Handler.handleCallback(Handler.java:739)
    at android.os.Handler.dispatchMessage(Handler.java:95)
    at android.os.Looper.loop(Looper.java:158)
    at android.app.ActivityThread.main(ActivityThread.java:7224)
    at java.lang.reflect.Method.invoke(Native Method)
    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1230)
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1120)

EDIT: This only occurs on Android.

EDIT: Additional information: This occurs mainly when a call to Navigation.PushAsync or Navigation.PopAsync (including user back button press).

Here is the simplest example of this occurring in my app: Starting from the home screen, the user can take action to lead to a QR code scanning page. This QR code contains information on what load this driver will be delivering. After scanning, I save the information and pop the scanning page from navigation and then automatically push them to a loading screen. After the data loads I push them to a list of the information they need. (So now the navigation stack has [main, loading_screen, list_view]) If at this point any Navigation.PopAsync (or PushAsync) is called then the app crashes.

like image 862
Kallmanation Avatar asked Feb 21 '17 19:02

Kallmanation


2 Answers

This is a confirmed bug in Xamarin Forms 2.3.3.193:

Bug 53179 - PopAsync crashing after RemovePage when support packages are updated to 25.1.1

So what you did was not incorrect; however your answer is a valid workaround.

like image 157
VincentH_NET Avatar answered Sep 18 '22 15:09

VincentH_NET


Solution found. The simple answer is I was incorrectly manipulating the Navigation Stack. As per VincentH_NET's answer, I should be able to do what I originally attempted to do, but due to a bug in Xamarin I cannot. Here is the situation:

The app begins with only the main page on the Navigation stack, which I'll denote with M

[ M

Upon user interaction, I will first push a loading screen, denoted by L, onto the navigation stack, while I prepare a barcode scanning page.

[ M, L

Once the scanning page is ready, I automatically push the user forward, leaving the navigation stack like so:

[ M, L, S

Now the user will scan a QR code containing lookup information. After they successfully scan the code, I pop them back to the loading screen (which is now repurposed to be the loading screen while I make requests to the server for their data)

[ M, L

After I have loaded the corresponding information from the QR code, I push them to a list view of the Data, page D.

[ M, L, D

At this point I attempted to remove the Loading page L from the Navigation stack using Navigation.RemovePage(); since the loading page is no longer needed and would be confusing if a user pressed a back button to see a loading screen that is loading nothing. What I wanted was a Navigation stack that looked like this:

[ M, D

But instead it seems my page removal corrupted the Navigation stack, leading to the errors I have been experiencing.

My solution now is to, instead of attempting to remove the loading page from the stack to set the loading page to automatically pop itself off of the stack if it is not actually loading anything.

P.S. if anyone has more in depth understanding of this problem or better best practices for these types of UI situations, I would love to hear your answers.

like image 45
Kallmanation Avatar answered Sep 21 '22 15:09

Kallmanation