Toast.getView() 在 Android 11 (API 30) 上返回 null [英] Toast.getView() returns null on Android 11 (API 30)
问题描述
我刚刚在模拟器中安装了 Android R (API 30) 图像来试用我的应用,但在尝试设置 Toast 的背景颜色时它崩溃了.
I just installed the Android R (API 30) image in my emulator to try my app, and it crashed when trying to set the Background color of a Toast.
Toast toast = Toast.makeText(ctxt, msg, duration);
View view = toast.getView();
view.setBackgroundColor(0xFF303030);
TextView tview = view.findViewById(android.R.id.message);
tview.setTextColor(Color.WHITE);
toast.show();
这真的很奇怪,因为在 Android Q (API 29) 中运行完美.
This is really strange as in Android Q (API 29) works perfectly.
我为 Android R (API 30) 更新了 build.gradle
My build.gradle updated for Android R (API 30)
compileSdkVersion 30
buildToolsVersion "30.0.1"
有新的方法吗??
推荐答案
自 Android 11 起,自定义 Toast/toast 修改已被弃用,据 Google 称以保护用户".因此,您在 Android 30 中的应用无法显示自定义 Toast.
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.
来自 Android 开发者文档:
From Android Developers documentation:
不推荐使用自定义 Toast 视图.应用程序可以使用 makeText(android.content.Context, java.lang.CharSequence, int) 来创建标准的文本吐司
Custom toast views are deprecated. Apps can create a standard text toast with the makeText(android.content.Context, java.lang.CharSequence, int)
这篇关于Toast.getView() 在 Android 11 (API 30) 上返回 null的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!