Reputation: 3
I am trying to implement acr.userDialogs to my xamain.forms project, it is getting implemented perfectly in android but i am getting null object reference when i try to run it in iOS device.
Here is the library reference for the dialog that i am using:
https://github.com/aritchie/userdialogs
Here is my page.xaml.cs
file:
[XamlCompilation(XamlCompilationOptions.Compile)]
public partial class Page1 : ContentPage
{
public Page1 ()
{
InitializeComponent ();
UserDialogs.Instance.Alert("Demo Dialog", "Dialog", "ok");
}
}
Here is the android ManinActivity.cs
file:
[Activity(Label = "App3", Icon = "@mipmap/icon", Theme = "@style/MainTheme", MainLauncher = true, ConfigurationChanges = ConfigChanges.ScreenSize | ConfigChanges.Orientation)]
public class MainActivity : global::Xamarin.Forms.Platform.Android.FormsAppCompatActivity
{
protected override void OnCreate(Bundle bundle)
{
TabLayoutResource = Resource.Layout.Tabbar;
ToolbarResource = Resource.Layout.Toolbar;
UserDialogs.Init(() => (Activity)Forms.Context);
base.OnCreate(bundle);
global::Xamarin.Forms.Forms.Init(this, bundle);
LoadApplication(new App());
}
}
Here is my appDelegate.cs
file for iOS:
public override bool FinishedLaunching(UIApplication app, NSDictionary options)
{
global::Xamarin.Forms.Forms.Init();
LoadApplication(new App());
return base.FinishedLaunching(app, options);
}
Here is my Main.cs
file for iOS:
public class Application
{
// This is the main entry point of the application.
static void Main(string[] args)
{
// if you want to use a different Application Delegate class from "AppDelegate"
// you can specify it here.
UIApplication.Main(args, null, "AppDelegate");
}
}
I have also seen the library documentation and have found that you don't need to initialize userDialogs in iOS as it does automatically, however in android it is mandatory to initialize dialog in MainActivity.cs file which i have already did. Please check and let me know what am i missing in my code.
Upvotes: 0
Views: 237
Reputation: 2604
As per the FAQ for Acr.UserDialogs,
This happens when you run loading (or almost any dialog) from the constructor of your page or viewmodel. The view hasn't been rendered yet, therefore there is nothing to render to.
You are calling it in the Page's constructor.
Try to show alert on some button click and you should be able to do it.
Upvotes: 1