Search code examples
c#xamarinxamarin.formsxamarin.android

Xamarin Plugin.Toast System.NullReferenceException


I'm trying to make a toast message in Xamarin.Forms with the Plugin.Toast Nuget-Package.

Class for calling Toast:

using Plugin.Toast;
using System;
using System.Collections.Generic;
using System.Text;
using Xamarin.Essentials;

namespace toast.service
{
    public class toast
    {

        public void toastWarning(string msg)
        {
          CrossToastPopUp.Current.ShowToastWarning(msg, Plugin.Toast.Abstractions.ToastLength.Short);
        }

    
    }
}

Calling toast code:

service.toast toast = new service.toast();
toast.toastWarning("test");

But I alwas get this error: System.NullReferenceException: 'Object reference not set to an instance of an object.'


Solution

  • There is no need to use a Plugin. Xamarin provide Android.Widget.Toast library to show message.

       Toast.MakeText(this, "Received intent!", ToastLength.Short).Show();
    

    enter image description here