Xamarin App crashes on Reopen NullReference

I have an app on Xamarin, it works fine, if I press back button, to move to main menu it hides in app stack, so when I open it back from app stack it suddenly crashes with NullReference

06-10 15:59:57.363 I/MonoDroid(14891): UNHANDLED EXCEPTION: 06-10 15:59:57.363 I/MonoDroid(14891): System.NullReferenceException: Object reference not set to an instance of an object 06-10 15:59:57.363 I/MonoDroid(14891): at Xamarin.Forms.Platform.Android.FormsApplicationActivity.OnPrepareOptionsMenu (Android.Views.IMenu) <IL 0x00007, 0x00050> 06-10 15:59:57.363 I/MonoDroid(14891): at Android.App.Activity.n_OnPrepareOptionsMenu_Landroid_view_Menu_ (intptr,intptr,intptr) [0x00011] in /Users/builder/data/lanes/monodroid-mavericks-monodroid-5.1-series/d419c934/source/monodroid/src/Mono.Android/platforms/android-21/src/generated/Android.App.Activity.cs:4151 06-10 15:59:57.363 I/MonoDroid(14891): at (wrapper dynamic-method) object.87f976f0-8c0f-4463-92f4-c8ed92b6336d (intptr,intptr,intptr) <IL 0x00017, 0x0004b> 06-10 15:59:57.383 D/AndroidRuntime(14891): Shutting down VM 06-10 15:59:57.383 W/dalvikvm(14891): threadid=1: thread exiting with uncaught exception (group=0x41b66c08) An unhandled exception occured.

Any ideas will be helpful, stuck with it for too long

Best Answer

Answers

  • adamkempadamkemp USInsider, Developer Group Leader mod

    Could you share an example project that reproduces this?

  • IgorLimankyIgorLimanky RUMember
    edited June 2015

    hi, Adam, thank you for your response, sorry I couldn't create a sample project that can reproduce it, I can share with you my project, but it is not open source, so I can send it to your email, if you'll share it(email) with me.
    write me on i.e.limansky\at\gmail\dot\com please

  • adamkempadamkemp USInsider, Developer Group Leader mod

    Post it on Dropbox or something and send me a private message with a link.

  • IgorLimankyIgorLimanky RUMember

    thank you for response, yep, too bad it relaunches again, at least background thread is still working.

  • DanielLDanielL PLInsider ✭✭✭✭
    edited July 2015

    Did you try if this would work? I had a similar problem some time ago.

    public class MainActivity : FormsApplicationActivity
    {
        public override bool OnKeyDown(Keycode keyCode, KeyEvent e)
        {
            if (this.ActionBar.Title == "YourMainMenuTitle")
            {
                if (keyCode == Keycode.Back && e.RepeatCount == 0)
                {
                    MoveTaskToBack(true);
                    return true;
                }
            }
    
            return base.OnKeyDown(keyCode, e);
        }
    
        ...
    }
    
  • GeoffArmstrongGeoffArmstrong CAMember ✭✭

    Looks like I'm getting this too.

  • AnthonyRamirezAnthonyRamirez USUniversity ✭✭✭
    edited September 2015

    I too am getting this. Should this bug https://bugzilla.xamarin.com/show_bug.cgi?id=29275 be a high priority issue? Or is there an easy workaround?

    Thanks

  • adamkempadamkemp USInsider, Developer Group Leader mod

    The workaround was in the accepted answer.

  • AnthonyRamirezAnthonyRamirez USUniversity ✭✭✭

    Thanks for the fast reply @adamkemp :smiley:

  • PatrickMcCurleyPatrickMcCurley AUMember ✭✭
    edited December 2015

    this can also happen if you stupidly remove

    SetPage(Page page); or LoadApplication(Application app) in MainActivity.OnCreate()

    as i just did

  • AnthonyRamirezAnthonyRamirez USUniversity ✭✭✭

    haha

    @PatrickMcCurley hopefully you didn't lose much time on that one ;)

  • CurtisMullinCurtisMullin USMember

    Is this still an open issue. I recently downloaded the trial of Xamarin and I'm using it with visual studio to try to build an app. I would like to purchase the business license for myself but this issue is very frustrating and not giving me very much confidence in its abilities.

    I'm on Xamarin.Forms 2.0.1.6505 with xamarin

    Adam's answer above is not working for me...

    It still crashes with this stack trace, every single time I go to run the app from VS 2015 Update 1 or Xamarin Studio build 5.10.2:

    01-30 17:50:47.772 W/dalvikvm( 732): JNI WARNING: JNI method called with exception pending
    01-30 17:50:47.792 W/dalvikvm( 732): in Lmd5ac9c9ee4e02079305c8be59c63a7bb93/MainActivity;.n_onPrepareOptionsMenu:(Landroid/view/Menu;)Z (CallStaticLongMethod)
    01-30 17:50:47.792 W/dalvikvm( 732): Pending exception is:
    01-30 17:50:47.792 I/dalvikvm( 732): md52ce486a14f4bcd95899665e9d932190b.JavaProxyThrowable: System.NullReferenceException: Object reference not set to an instance of an object
    01-30 17:50:47.792 I/dalvikvm( 732): at Xamarin.Forms.Platform.Android.FormsApplicationActivity.OnPrepareOptionsMenu (IMenu menu) [0x00000] in :0
    01-30 17:50:47.792 I/dalvikvm( 732): at Android.App.Activity.n_OnPrepareOptionsMenu_Landroid_view_Menu_ (IntPtr jnienv, IntPtr native__this, IntPtr native_menu) [0x00011] in /Users/builder/data/lanes/2692/e98e9627/source/monodroid/src/Mono.Android/platforms/android-23/src/generated/Android.App.Activity.cs:4301
    01-30 17:50:47.792 I/dalvikvm( 732): at (wrapper dynamic-method) System.Object:647c7cf4-69ee-4f99-928e-68e914f7bccd (intptr,intptr,intptr)
    01-30 17:50:47.792 I/dalvikvm( 732): at md5ac9c9ee4e02079305c8be59c63a7bb93.MainActivity.n_onPrepareOptionsMenu(Native Method)
    01-30 17:50:47.792 I/dalvikvm( 732): at md5ac9c9ee4e02079305c8be59c63a7bb93.MainActivity.onPrepareOptionsMenu(MainActivity.java:38)
    01-30 17:50:47.792 I/dalvikvm( 732): at android.app.Activity.onPreparePanel(Activity.java:2462)
    01-30 17:50:47.792 I/dalvikvm( 732): at com.android.internal.policy.impl.PhoneWindow.preparePanel(PhoneWindow.java:416)
    01-30 17:50:47.792 I/dalvikvm( 732): at com.android.internal.policy.impl.PhoneWindow.invalidatePanelMenu(PhoneWindow.java:739)
    01-30 17:50:47.792 I/dalvikvm( 732): at com.android.internal.policy.impl.PhoneWindow$1.run(PhoneWindow.java:2833)
    01-30 17:50:47.792 I/dalvikvm( 732): at android.os.Handler.handleCallback(Handler.java:605)
    01-30 17:50:47.792 I/dalvikvm( 732): at android.os.Handler.dispatchMessage(Handler.java:92)
    01-30 17:50:47.802 I/dalvikvm( 732): at android.os.Looper.loop(Looper.java:137)
    01-30 17:50:47.802 I/dalvikvm( 732): at android.app.ActivityThread.main(ActivityThread.java:4424)
    01-30 17:50:47.802 I/dalvikvm( 732): at java.lang.reflect.Method.invokeNative(Native Method)
    01-30 17:50:47.802 I/dalvikvm( 732): at java.lang.reflect.Method.invoke(Method.java:511)
    01-30 17:50:47.802 I/dalvikvm( 732): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:784)
    01-30 17:50:47.802 I/dalvikvm( 732): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551)
    01-30 17:50:47.802 I/dalvikvm( 732): at dalvik.system.NativeStart.main(Native Method)
    01-30 17:50:47.822 I/dalvikvm( 732): "main" prio=5 tid=1 NATIVE
    01-30 17:50:47.822 I/dalvikvm( 732): | group="main" sCount=0 dsCount=0 obj=0x409c0460 self=0x12810
    01-30 17:50:47.822 I/dalvikvm( 732): | sysTid=732 nice=0 sched=0/0 cgrp=default handle=1074082952
    01-30 17:50:47.822 I/dalvikvm( 732): | schedstat=( 7754937562 2898855485 1711 ) utm=664 stm=111 core=0
    01-30 17:50:47.822 I/dalvikvm( 732): at md5ac9c9ee4e02079305c8be59c63a7bb93.MainActivity.n_onPrepareOptionsMenu(Native Method)
    01-30 17:50:47.822 I/dalvikvm( 732): at md5ac9c9ee4e02079305c8be59c63a7bb93.MainActivity.onPrepareOptionsMenu(MainActivity.java:38)
    01-30 17:50:47.822 I/dalvikvm( 732): at android.app.Activity.onPreparePanel(Activity.java:2462)
    01-30 17:50:47.822 I/dalvikvm( 732): at com.android.internal.policy.impl.PhoneWindow.preparePanel(PhoneWindow.java:416)
    01-30 17:50:47.822 I/dalvikvm( 732): at com.android.internal.policy.impl.PhoneWindow.invalidatePanelMenu(PhoneWindow.java:739)
    01-30 17:50:47.822 I/dalvikvm( 732): at com.android.internal.policy.impl.PhoneWindow$1.run(PhoneWindow.java:2833)
    01-30 17:50:47.822 I/dalvikvm( 732): at android.os.Handler.handleCallback(Handler.java:605)
    01-30 17:50:47.822 I/dalvikvm( 732): at android.os.Handler.dispatchMessage(Handler.java:92)
    01-30 17:50:47.832 I/dalvikvm( 732): at android.os.Looper.loop(Looper.java:137)
    01-30 17:50:47.832 I/dalvikvm( 732): at android.app.ActivityThread.main(ActivityThread.java:4424)
    01-30 17:50:47.832 I/dalvikvm( 732): at java.lang.reflect.Method.invokeNative(Native Method)
    01-30 17:50:47.832 I/dalvikvm( 732): at java.lang.reflect.Method.invoke(Method.java:511)
    01-30 17:50:47.832 I/dalvikvm( 732): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:784)
    01-30 17:50:47.832 I/dalvikvm( 732): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551)
    01-30 17:50:47.832 I/dalvikvm( 732): at dalvik.system.NativeStart.main(Native Method)

  • adamkempadamkemp USInsider, Developer Group Leader mod

    If it crashes on first launch then you don't have the same issue.

  • AnthonyRamirezAnthonyRamirez USUniversity ✭✭✭
    edited January 2016

    @CurtisMullin I agree with @adamkemp it does look like a different issue, unfortunately, I can't figure out what is the cause based on the stack trace.

    Not sure if you already tried it, but enable Common Language Runtime Exceptions under Debug --> Exceptions then debug it. Maybe that will give you more info what is going on.

    Good Luck!

  • hvaughanhvaughan USMember ✭✭✭

    My issue ended up being having a different version of Xamarin Forms installed in one project.

Sign In or Register to comment.