Reputation: 111
I'm creating a simple android app that contains simple controls/views like Label, Entry and Buttons
After archive and generate APK file, i successfully installed it in my MEmu emulator (Android Version: 5.1). But when i tried to open it, the app crashes.
This is the error i get from device log tool:
System.NullReferenceException: Object reference not set to an instance of an object
at Xamarin.Forms.Platform.Android.FormsAppCompatActivity.InternalSetPage (Xamarin.Forms.Page page) [0x0006f] in <99988d4ab8d144898ef5bc7586876d75>:0
at Xamarin.Forms.Platform.Android.FormsAppCompatActivity.SetMainPage () [0x0000c] in <99988d4ab8d144898ef5bc7586876d75>:0
at Xamarin.Forms.Platform.Android.FormsAppCompatActivity.LoadApplication (Xamarin.Forms.Application application) [0x0026f] in <99988d4ab8d144898ef5bc7586876d75>:0
at SampleApp.Droid.MainActivity.OnCreate (Android.OS.Bundle bundle) [0x00028] in <eaa39f9ef27d400ebfed424165f990c2>:0
at Android.App.Activity.n_OnCreate_Landroid_os_Bundle_ (System.IntPtr jnienv, System.IntPtr native__this, System.IntPtr native_savedInstanceState) [0x0000f] in <818821ea7e204c78a45bc29cdc69e744>:0
at (wrapper dynamic-method) System.Object.fe8f1617-954f-4716-901a-433b7f8b44bf(intptr,intptr,intptr)
I suspect that this is Xamarin.Forms.Platform.Android.FormsAppCompatActivity
issue. Because when i changed the MainActivity parent class to Xamarin.Forms.Platform.Android.FormsApplicationActivity
it runs without error.
But how can i fix this error without switching to FormsApplicationActivity
??
Edit: When i disable ProGuard in the "Packaging Properties", the app runs smoothly. (why?)
Upvotes: 2
Views: 1321
Reputation: 74134
It is a bug/issue with Forms' 3.0 (at least version 3.0.0.561731
) as there is something within the Java android.support.v7.widget
namespace that is being stripped and Xamarin is not generating a proper proguard configuration to automatically prevent the problem.
I'm not sure which actual Java classes and/or fields are the ones causing the issue, but you can do this to workaround around it:
Create a proguard configuration text-based file in your Xamarin.Android
application project and assign it a build type of ProguardConfiguration
, i.e.:
<ItemGroup>
<ProguardConfiguration Include="Proguard.txt" />
</ItemGroup>
And include the following:
# Proguard issue in Forms' version 3.0.0.561731
# MonoDroid: System.NullReferenceException: Object reference not set to an instance of an object
# MonoDroid: at Xamarin.Forms.Platform.Android.FormsAppCompatActivity.InternalSetPage (Xamarin.Forms.Page page) [0x0006f] in <09e4bdebfa024bfd9231e2318fd7c3d7>:0
# MonoDroid: at Xamarin.Forms.Platform.Android.FormsAppCompatActivity.SetMainPage () [0x0000c] in <09e4bdebfa024bfd9231e2318fd7c3d7>:0
-keep public class android.support.v7.widget.** { *; }
Delete the app from the device/emulator and perform a clean all/rebuild all and redeploy the app and that should solve the problem.
Upvotes: 1