Toast icon indicating copy to clipboard operation
Toast copied to clipboard

Crash when targeting Android 11

Open tranb3r opened this issue 3 years ago • 17 comments

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 

tranb3r avatar Nov 30 '20 10:11 tranb3r

https://stackoverflow.com/questions/62884286/toast-getview-returns-null-on-android-11-api-30

tranb3r avatar Nov 30 '20 11:11 tranb3r

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.

tranb3r avatar Dec 08 '20 09:12 tranb3r

Hello , @tranb3r i will fix it as soon as possible. Thank you for your help.

ishrakland avatar Dec 08 '20 13:12 ishrakland

@ishrakland Any update ? A quick fix to prevent the crash by ignoring colors would be good enough...

tranb3r avatar Jan 04 '21 08:01 tranb3r

try custom toast you can specify the color you want. I will fix it : i have a lot of charge.

ishrakland avatar Jan 05 '21 13:01 ishrakland

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.

tranb3r avatar Jan 05 '21 14:01 tranb3r

Experiencing the same issue

jpveldtman avatar Jan 13 '21 13:01 jpveldtman

@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.

tranb3r avatar Jan 18 '21 09:01 tranb3r

Same problem here.... and based on the time it got reported.. I assume it won't get fix soon

douglassimaodev avatar Jul 09 '21 01:07 douglassimaodev

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.

ishrakland avatar Jul 09 '21 08:07 ishrakland

GWS @ishrakland :D

sunkerGit avatar Jul 29 '21 07:07 sunkerGit

It's free plugin, opensource and i don't get money from it . it's open for all users to contribute.

ishrakland avatar Jul 29 '21 09:07 ishrakland

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 avatar Jul 29 '21 11:07 tranb3r

@tranb3r Yes, thank you

ishrakland avatar Jul 29 '21 11:07 ishrakland

Did you find any solution @tranb3r

Yes, I'm using xamarin community toolkit now, instead of this plugin.

tranb3r avatar Aug 25 '21 07:08 tranb3r

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();
        }

iane87 avatar Oct 07 '21 05:10 iane87

Hi.

This issue is still repeating in Android 11.

When are you coming out with the updated plugin.toast to handle this issue?

Thanks.

aproko234 avatar Jul 01 '22 12:07 aproko234