Xamarin.Forms.Platform.Android.FormsAppCompatActivity.SetMainPage - Null Reference Exception

Created on 21 Mar 2018  路  14Comments  路  Source: xamarin/Xamarin.Forms

Description

There appears to be a bug in Xamarin.Forms 2.5.0.280555 and up -- including 2.5.1.340284-pre2 and most likely (not yet confirmed) 3.0.0.296286-pre2 where SetMainPage() can encounter a Null Reference exception.

This bug has been previously mentioned here by someone else and includes the exact traceback I was able to produce consistently: https://bugzilla.xamarin.com/show_bug.cgi?id=36287#c14 . This comment appeared within the discussion of another bug so wasn't addressed directly.

Steps to Reproduce

I am able to reproduce this bug easily, provided that certain steps are followed. The Android app must be completely removed from the device. Then, it must be installed. The app must use PermissionsPlugin, this will cause the app to pop up an initial permissions request prior to app.MainPage being initialized. This will cause SetMainPage() to run, and the Null Reference Exception will be triggered.

If the app is already installed, or upgraded from an already existing version, the initial Android permissions request pop-up does not appear, everything works fine and the Null Reference exception does not occur. That is why this traceback has been so tricky to track down -- it only happens under the specific conditions described above.

Expected Behavior

SetMainPage() should just work under all conditions and not trigger a Null Reference Exception.

Basic Information

  • Version with issue: Based on initial report, at least XF 2.5.0.280555 and newer.
  • Last known good version: Unknown
  • IDE: Visual Studio Mac (latest stable)
  • Nuget Packages: Plugin.Permissions (2.2.1), possibly other versions or anything that will cause the permissions pop-up to appear.

Traceback

android.runtime.JavaProxyThrowable: at Xamarin.Forms.Platform.Android.FormsAppCompatActivity.InternalSetPage (Xamarin.Forms.Page page) [0x0006f] in <173f39d71f0d4d928f5bbea42e96ffa8>:0
at Xamarin.Forms.Platform.Android.FormsAppCompatActivity.SetMainPage () [0x0000c] in <173f39d71f0d4d928f5bbea42e96ffa8>:0
at Xamarin.Forms.Platform.Android.FormsAppCompatActivity.LoadApplication (Xamarin.Forms.Application application) [0x0025c] in <173f39d71f0d4d928f5bbea42e96ffa8>:0
at ddod_xamarin.Droid.MainActivity.OnCreate (Android.OS.Bundle bundle) [0x00098] in <1ef34a1d5efe4254ae2bcbf6d5b0d8b4>:0
at Android.App.Activity.n_OnCreate_Landroid_os_Bundle_ (System.IntPtr jnienv, System.IntPtr native__this, System.IntPtr native_savedInstanceState) [0x0000f] in <40c3dbb407c542888943108189b6e6d6>:0
at (wrapper dynamic-method) System.Object.f4495476-e68d-4c26-a881-ccd056b172c5(intptr,intptr,intptr)
at md55fb97b9b0cbefcefea98df467d0407a7.MainActivity.n_onCreate (Native Method)
at md55fb97b9b0cbefcefea98df467d0407a7.MainActivity.onCreate (MainActivity.java:30)
at android.app.Activity.performCreate (Activity.java:6698)
at android.app.Instrumentation.callActivityOnCreate (Instrumentation.java:1140)
at android.app.ActivityThread.performLaunchActivity (ActivityThread.java:2643)
at android.app.ActivityThread.handleLaunchActivity (ActivityThread.java:2755)
at android.app.ActivityThread.-wrap12 (ActivityThread.java)
at android.app.ActivityThread$H.handleMessage (ActivityThread.java:1495)
at android.os.Handler.dispatchMessage (Handler.java:102)
at android.os.Looper.loop (Looper.java:154)
at android.app.ActivityThread.main (ActivityThread.java:6196)
at java.lang.reflect.Method.invoke (Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run (ZygoteInit.java:888)
at com.android.internal.os.ZygoteInit.main (ZygoteInit.java:778)

Android bug

All 14 comments

This isn't directly related to the root cause of the issue but the NullReferenceException occurs because of what is fixed in this PR
https://github.com/xamarin/Xamarin.Forms/pull/2122

If something fails inside the OnCreate then the exception is swallowed and important layout code doesn't initialize. This causes the NullReferenceException later down the pipeline which overall masks the initial failure.

So when recreating this inside the ControlGallery on the code base be sure to turn on First chance exceptions or step through OnCreate to see where it fails.

Hitting this as well with XF 3.0.0.446417

I can confirm that this is still an issue in the latest prerelease 3.1.0.469394-pre1.

We regularly run into this when switching branches and then building

Some additional Info: Cleaning (Clean Solution, Close VS, Delete obj/bin-Folder, Load Project and Rebuild) does not help.

I got around it by unchecking "Enable ProGuard" (Project Properties --> Android Options --> Enable ProGuard checkbox)

@dbergan I found that using the accepted answer here https://forums.xamarin.com/discussion/92530/proguard-issues resolved the issue for me and allowed me to continue using ProGuard.

Links to https://pastebin.com/7rNJkns0

Still need to do more testing before considering it fixed.

If you pull down the latest 3.1 pre project it will give you a more accurate exception due to #2122

As far as ProGuard the latest Support Libraries plus Xamarin Forms requires a proguard file even for a fresh project

We're looking into removing that requirement.
Here's more info on how to fix a new Xamarin Forms project
https://github.com/xamarin/Xamarin.Forms/issues/2709

The original poster hasn't followed up and it sounds like the rest of you are having proguard issues. After installing the latest 3.1 pre nuget if you're still getting an error that seems unrelated to proguard please create an issue. If you're getting an issue about Resource not found try rebuilding the Android project

Seems not to be the problem at my side. I have the proguard-android.txt in my proguard folder.
Never the less. The latest 3.1.0.561732-pre4 seems to fix the problem.

Sorry, I was too fast with the comment that the issue is fixed. Actually it is not. It only happens if I publish it via Google Play store. If I install the APK manually, I have no issues with it.

Works for me! (Haven't tried Google Play store)

@Hunv when you install manually is it the identical APK that you have published to google play store? It seems odd the would act differently. For the issue you are seeing on google play store I would attach that device and watch the logcat logs to see what the exception is. That will probably tell you what needs to be resolved. I'm guessing there's an additional proguard exclusion you are going to need. If it still looks like a Xamarin Forms issue can you open a new issue with the exception you see from logcat and then if you can a repro would be helpful as well.

I was hitting this too, and found I forgot to call base.OnCreate in my activity. A better error message would have been helpful, so submitted a PR: https://github.com/xamarin/Xamarin.Forms/pull/3470

Enabling proguard cause this issue. Disabling proguard solves it.

Was this page helpful?
0 / 5 - 0 ratings