Adding sound resource to Android project as described in (https://developer.xamarin.com/recipes/android/media/audio/play_audio/) leads to NullReferenceException on LoadApplication().
(unclear if it has to be a sound file, but that's what I was using)
Application should build and run regardless of changes to the resources directory.
Application crashes in non-user code on launch.
@BioTurboNick What's the name of the .m4a file? (I ask because Android resource generation restricts the valid characters in a resource name.)
"dial_click.m4a" The Resource.Designer.cs generated correctly (AFAICT). I removed the underscore though and it still crashes.
For comparison, adding a sound as an Asset with build action AndroidAsset as described here (https://blog.xamarin.com/adding-sound-xamarin-forms-app/) works.
As, per step 0, you can trigger this issue on projects not involving Xamarin.Forms, please file this issue against Xamarin Android project.
@StephaneDelcroix
That is not what step 0 says. It is occurring in LoadApplication(new App()), which is Xamarin.Forms code. Please reopen this issue.
I read it wrong. Please attach a small reproduction project.
Reproduction project:
Another observation: installing the NuGet package XFShapeView, which amends Resource.Designer.cs, also causes this crash.
Same problem here but I don't think it's directly related to the resource sound file.
I don't have such file but I get the same error and I'm reading more and more people online having the same issue.
I suspect it's something related to Xamarin Forms and .Netstandard
I ran the reproduction project from VS 15.5.6 with the latest Xam Forms 2.5.0.122203 and didn't get any exceptions. Also tried on VS For Mac and didn't get any exceptions. I also installed XFShapeView and didn't see any issues
AndroidResourceBug.zip
I'm attaching a version of the repro that has the SLN file as the original seems to be missing it
Alright I spoke too soon. I was able to reproduce by adding and removing the m4a a few times. Though this still feels like it might be a tooling issue and not a Xamarin Forms issue. If I remove or add the m4a file and then run the project right away I get the nullreference exception. But if I just rebuild the project or delete the bin/obj folders then I don't get a null reference exception. So this seems to occur because of files that don't detect they need to be rebuilt
I can confirm that Rebuild appears to correct the problem. Thank you for pointing that out.
Workaround: Every time a resource is added, perform a Rebuild on the Android project (and/or delete bin and obj folders).
Adb Logcat output below.
So the null ref exception is happening at [1].
Looking at the Resource.Designer.cs file the raw output is correctly added and removed from that file when it is removed/add to/from the project. The file is correctly removed and added to the zip.
The assembly is also being fast deployed and does not contain the resource in its compiled code.
I cannot repo in a non Xamarin.Forms Android app. So I'm not sure what is happening here, the android build system, packaging and fast deployment all appear to be doing the right thing.
[1] https://github.com/xamarin/Xamarin.Forms/blob/e65b48b7194aff9287aae8619a3964fbf71993fe/Xamarin.Forms.Platform.Android/AppCompat/FormsAppCompatActivity.cs#L291
I/MonoDroid( 2774): UNHANDLED EXCEPTION:
I/MonoDroid( 2774): System.NullReferenceException: Object reference not set to an instance of an object
I/MonoDroid( 2774): at Xamarin.Forms.Platform.Android.FormsAppCompatActivity.InternalSetPage (Xamarin.Forms.Page page) [0x0006f] in:0
I/MonoDroid( 2774): at Xamarin.Forms.Platform.Android.FormsAppCompatActivity.SetMainPage () [0x0000c] in:0
I/MonoDroid( 2774): at Xamarin.Forms.Platform.Android.FormsAppCompatActivity.LoadApplication (Xamarin.Forms.Application application) [0x0025c] in:0
I/MonoDroid( 2774): at App1.Droid.MainActivity.OnCreate (Android.OS.Bundle bundle) [0x0002d] in:0
I/MonoDroid( 2774): at Android.Support.V4.App.FragmentActivity.n_OnCreate_Landroid_os_Bundle_ (System.IntPtr jnienv, System.IntPtr native__this, System.IntPtr native_savedInstanceState) [0x0000f] in <1e89c43d3c7d48b5ba69bfc0b1bdf114>:0
I/MonoDroid( 2774): at (wrapper dynamic-method) System.Object.f5ffd162-ce4a-4512-95d6-42c9a204eff2(intptr,intptr,intptr)
W/art ( 2774): JNI RegisterNativeMethods: attempt to register 0 native methods for android.runtime.JavaProxyThrowable
D/Mono ( 2774): DllImport searching in: '__Internal' ('(null)').
D/Mono ( 2774): Searching for 'java_interop_jnienv_throw'.
D/Mono ( 2774): Probing 'java_interop_jnienv_throw'.
D/Mono ( 2774): Found as 'java_interop_jnienv_throw'.
D/AndroidRuntime( 2774): Shutting down VM
--------- beginning of crash
E/AndroidRuntime( 2774): FATAL EXCEPTION: main
E/AndroidRuntime( 2774): Process: com.companyname.App1, PID: 2774
E/AndroidRuntime( 2774): android.runtime.JavaProxyThrowable: System.NullReferenceException: Object reference not set to an instance of an object
E/AndroidRuntime( 2774): at Xamarin.Forms.Platform.Android.FormsAppCompatActivity.InternalSetPage (Xamarin.Forms.Page page) [0x0006f] in:0
E/AndroidRuntime( 2774): at Xamarin.Forms.Platform.Android.FormsAppCompatActivity.SetMainPage () [0x0000c] in:0
E/AndroidRuntime( 2774): at Xamarin.Forms.Platform.Android.FormsAppCompatActivity.LoadApplication (Xamarin.Forms.Application application) [0x0025c] in:0
E/AndroidRuntime( 2774): at App1.Droid.MainActivity.OnCreate (Android.OS.Bundle bundle) [0x0002d] in:0
E/AndroidRuntime( 2774): at Android.Support.V4.App.FragmentActivity.n_OnCreate_Landroid_os_Bundle_ (System.IntPtr jnienv, System.IntPtr native__this, System.IntPtr native_savedInstanceState) [0x0000f] in <1e89c43d3c7d48b5ba69bfc0b1bdf114>:0
E/AndroidRuntime( 2774): at (wrapper dynamic-method) System.Object.f5ffd162-ce4a-4512-95d6-42c9a204eff2(intptr,intptr,intptr)
E/AndroidRuntime( 2774): at md58adf3f2592a7497b73592b5e766304c2.MainActivity.n_onCreate(Native Method)
E/AndroidRuntime( 2774): at md58adf3f2592a7497b73592b5e766304c2.MainActivity.onCreate(MainActivity.java:29)
E/AndroidRuntime( 2774): at android.app.Activity.performCreate(Activity.java:5937)
E/AndroidRuntime( 2774): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1105)
E/AndroidRuntime( 2774): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2251)
E/AndroidRuntime( 2774): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2360)
E/AndroidRuntime( 2774): at android.app.ActivityThread.access$800(ActivityThread.java:144)
E/AndroidRuntime( 2774): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1278)
E/AndroidRuntime( 2774): at android.os.Handler.dispatchMessage(Handler.java:102)
E/AndroidRuntime( 2774): at android.os.Looper.loop(Looper.java:135)
E/AndroidRuntime( 2774): at android.app.ActivityThread.main(ActivityThread.java:5221)
E/AndroidRuntime( 2774): at java.lang.reflect.Method.invoke(Native Method)
E/AndroidRuntime( 2774): at java.lang.reflect.Method.invoke(Method.java:372)
E/AndroidRuntime( 2774): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:899)
E/AndroidRuntime( 2774): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:694)
W/ActivityManager( 1512): Force finishing activity com.companyname.App1/md58adf3f2592a7497b73592b5e766304c2.MainActivity
Improvement have been made with Android resources and bin/obj problems in the last few months. I'm willing to bet that this is no longer an issue. If I am wrong and you are still experiencing this, please feel free to reopen it! Thank you!