Toast-PhoneGap-Plugin icon indicating copy to clipboard operation
Toast-PhoneGap-Plugin copied to clipboard

null pointer exceptions in android 11

Open cosmopolit opened this issue 3 years ago • 13 comments

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)

cosmopolit avatar Nov 16 '20 19:11 cosmopolit

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.

claytonsyspan avatar Apr 30 '21 11:04 claytonsyspan

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.

drewrygh avatar May 04 '21 20:05 drewrygh

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 avatar May 06 '21 05:05 almothafar

@drewrygh there is other lines Here too https://github.com/EddyVerbruggen/Toast-PhoneGap-Plugin/blob/master/src/android/nl/xservices/plugins/Toast.java#L163

almothafar avatar May 06 '21 07:05 almothafar

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

claytonsyspan avatar May 10 '21 16:05 claytonsyspan

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

EddyVerbruggen avatar May 10 '21 19:05 EddyVerbruggen

Would love to see this merged :)

gabn88 avatar Sep 13 '21 13:09 gabn88

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

zommerfelds avatar Sep 20 '21 10:09 zommerfelds

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

almothafar avatar Sep 20 '21 12:09 almothafar

@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 ;)

gabn88 avatar Sep 20 '21 12:09 gabn88

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

almothafar avatar Sep 20 '21 13:09 almothafar

The solution @dpa99c works. Add this changes https://github.com/dpa99c/Toast-PhoneGap-Plugin/commit/7a6120142e156a156e04301016abe3b35ec94cbb

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

123dma avatar Sep 23 '21 17:09 123dma

Would be great to update the plugin for Android 11

geshub avatar Sep 26 '21 10:09 geshub