Toast
Toast copied to clipboard
Crash when targeting Android 11
Toast getView() was deprecated on Android 11 and it returns null. So when targeting Android 11, the sample app crashes:
**System.NullReferenceException:** 'Object reference not set to an instance of an object.'
at Plugin.Toast.ShowToastPopUp.ShowMessage (System.String message, System.String backgroundHexColor, System.String textHexColor, Plugin.Toast.Abstractions.ToastLength toastLength) [0x0003d] in C:\Users\10061213\Toast\Toast.Plugin.Android\ShowToastPopUp.cs:79
at Plugin.Toast.ShowToastPopUp.ShowToastMessage (System.String message, Plugin.Toast.Abstractions.ToastLength toastLength) [0x00000] in C:\Users\10061213\Toast\Toast.Plugin.Android\ShowToastPopUp.cs:45
https://stackoverflow.com/questions/62884286/toast-getview-returns-null-on-android-11-api-30
My suggestion if you do not have the time to work on a full fix, would be to skip colors assignments on android 11, which would avoid the crash. I can do the PR if you want.
Hello , @tranb3r i will fix it as soon as possible. Thank you for your help.
@ishrakland Any update ? A quick fix to prevent the crash by ignoring colors would be good enough...
try custom toast you can specify the color you want. I will fix it : i have a lot of charge.
The crash occurs whatever color you specify. Again, a quick workaround to prevent the crash would be to skip color assignments on android 11. Let me know if you need some help with the PR.
Experiencing the same issue
@ishrakland I understand you may not have the time to work on this issue. Do you think you can provide a fix, or at least merge a quick fix to prevent the crash if I do the PR ? I really like this plugin, but considering the impact of this issue, I'm thinking about moving to xamarin-community-toolkit which also has a toast functionnality.
Same problem here.... and based on the time it got reported.. I assume it won't get fix soon
Hi , I was sick if you can fix it , and i will merge your code
From: douglassimaodev @.> Sent: Friday, July 9, 2021 1:04 AM To: ishrakland/Toast @.> Cc: El andaloussi Ishrak @.>; Mention @.> Subject: Re: [ishrakland/Toast] Crash when targeting Android 11 (#16)
Same problem here.... and based on the time it got reported.. I assume it won't get fix soon
— You are receiving this because you were mentioned. Reply to this email directly, view it on GitHubhttps://github.com/ishrakland/Toast/issues/16#issuecomment-876838647, or unsubscribehttps://github.com/notifications/unsubscribe-auth/AHNC2ZEZCPJXTWZYQCIGXMDTWZDKRANCNFSM4UHMXA3A.
GWS @ishrakland :D
It's free plugin, opensource and i don't get money from it . it's open for all users to contribute.
Maintaining this kind of plugin is too big a challenge for the long-term, and the xamarin community toolkit already has a toast functionality. I think you should consider merging your code into their repo if you think you can provide additional features.
@tranb3r Yes, thank you
Did you find any solution @tranb3r
Yes, I'm using xamarin community toolkit now, instead of this plugin.
I'm thinking that the following changes in ShowToastPopUp.cs in Android should fix it
private void ShowMessage(string message, string backgroundHexColor = null, string textHexColor = null, Abstractions.ToastLength toastLength = Abstractions.ToastLength.Short)
{
var length = toastLength == Abstractions.ToastLength.Short ? Android.Widget.ToastLength.Short : Android.Widget.ToastLength.Long;
// To dismiss existing toast, otherwise, the screen will be populated with it if the user do so
_instance?.Cancel();
_instance = Android.Widget.Toast.MakeText(Android.App.Application.Context, message, length);
if (Xamarin.Essentials.DeviceInfo.Version.Major <= 10)
{
View tView = _instance.View;
if (!string.IsNullOrEmpty(backgroundHexColor))
tView.Background.SetColorFilter(Color.ParseColor(backgroundHexColor), PorterDuff.Mode.SrcIn);//Gets the actual oval background of the Toast then sets the color filter
TextView text = (TextView)tView.FindViewById(Android.Resource.Id.Message);
if (!string.IsNullOrEmpty(textHexColor))
text.SetTextColor(Color.ParseColor(textHexColor));
}
_instance.Show();
}
Hi.
This issue is still repeating in Android 11.
When are you coming out with the updated plugin.toast to handle this issue?
Thanks.