Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

null pointer exceptions in android 11 #136

Open
cosmopolit opened this issue Nov 16, 2020 · 13 comments · Fixed by ns0m/cordova-plugin-ns0m-x-toast#2 · May be fixed by #139
Open

null pointer exceptions in android 11 #136

cosmopolit opened this issue Nov 16, 2020 · 13 comments · Fixed by ns0m/cordova-plugin-ns0m-x-toast#2 · May be fixed by #139

Comments

@cosmopolit
Copy link

Since Android 11, custom toasts/ toast modifications are deprecated, according to Google to "protect users". Hence why your app in Android 30 is not able to display custom toasts.

From Android Developers documentation:

Custom toast views are deprecated. Apps can create a standard text toast with the makeText(android.content.Context, java.lang.CharSequence, int)

@claytonsyspan
Copy link

claytonsyspan commented Apr 30, 2021

I am with problem with the plugin. It close my apps after display the message. What is the solution? Just in devices using android 11.

@drewrygh
Copy link

drewrygh commented May 4, 2021

Toast.getView() returns null on API Level 30+. As a temporary workaround, it's possible to add null checks before any calls that expect Toast.getView to return a View object. This styling block in Toast.java relies on the custom Toast View. To prevent crashes, it's possible to add a check that will exclude the block from running on API 30+, eg:

Toast.java

if (styling != null && Build.VERSION.SDK_INT >= 16 && Build.VERSION.SDK_INT < 30) { ... }

This is a temporary fix to prevent crashing, though it prevents styling from being applied to toasts on Android 11+.

@EddyVerbruggen what is the recommended approach for styling toasts going forward? The Toast docs mention using Snackbar— is this something you think would be within the scope of this plugin? This seems like an important issue to resolve since Android 11 is gaining marketshare quickly.

@almothafar
Copy link

I got the same issue for my users, Crashlytics reports a lot of crashes recently because of this:

image

@EddyVerbruggen I think this is critical

@almothafar
Copy link

@claytonsyspan
Copy link

@EddyVerbruggen what do you think about a new version of this plugin?

@EddyVerbruggen
Copy link
Owner

Oh dear! Happy to merge a PR (I might have missed one?)

dpa99c added a commit to dpa99c/Toast-PhoneGap-Plugin that referenced this issue Jun 28, 2021
@gabn88
Copy link

gabn88 commented Sep 13, 2021

Would love to see this merged :)

@zommerfelds
Copy link

zommerfelds commented Sep 20, 2021

Is there a workaround for this? I don't need any styling, but just calling showShortBottom is making my app crash once I touch on the screen after a toast.

E AndroidRuntime: java.lang.NullPointerException: Attempt to invoke virtual method 'boolean android.view.View.isShown()' on a null object reference

@almothafar
Copy link

@zommerfelds at this point, I think the only workaround for this is just giving up Cordova and go flutter, Cordova is dying

Anyways, seriously, I think the only way now is just to fork and solve the issue and install the plugin from your repo, you can merge any PR you want as these PRs are already sent.

@gabn88
Copy link

gabn88 commented Sep 20, 2021

@zommerfelds We just started to show the toast alerts with javascript instead of through cordova for Android. There are many toast like javascript packages on npm, but it is also fairly simple to create your own.

@almothafar I'm also following flutter, but since it cannot be (reliably) used on the web I see it as a different tool, not perse a substitute for Cordova/ionic. But that's a different discussion ;)

@almothafar
Copy link

@gabn88 I'm using Ionic toast as well https://ionicframework.com/docs/api/toast instead of native toast, I removed the native one from my side because of this issue.

@123dma
Copy link

123dma commented Sep 23, 2021

The solution @dpa99c works.
Add this changes
dpa99c@7a61201

and
I need to add the condition (if (Build.VERSION.SDK_INT >= 30) {) to work api less 30

if (Build.VERSION.SDK_INT >= 30) {
toast.addCallback(new android.widget.Toast.Callback() {
public void onToastShown() {

      }

      public void onToastHidden() {
        returnTapEvent("hide", msg, data, callbackContext);
      }
    });

}

tested = api 31, 30 ,29, 28, 27 ... 23

@geshub
Copy link

geshub commented Sep 26, 2021

Would be great to update the plugin for Android 11

lukevital pushed a commit to SymetriaSpJ/phonegap-plugin-Toast-PhoneGap-Plugin that referenced this issue Nov 4, 2021
lukevital pushed a commit to SymetriaSpJ/phonegap-plugin-Toast-PhoneGap-Plugin that referenced this issue Nov 4, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
9 participants