gpt4 book ai didi

android - 使用 toString() 将 URI 转换为 String 导致 Android 应用程序崩溃

转载 作者:行者123 更新时间:2023-12-02 04:04:00 24 4
gpt4 key购买 nike

我想设置通知铃声,这是我的代码:

Notification notification = new Notification();     

String ringName = RingtoneManager.getActualDefaultRingtoneUri(
MainActivity.this, RingtoneManager.TYPE_NOTIFICATION).toString();

notification.sound = Uri.parse(ringName);

但我发现 .toString() 会导致模拟器中的应用程序崩溃。

"RingtoneManager.getActualDefaultRingtoneUri( MainActivity.this, RingtoneManager.TYPE_NOTIFICATION)"返回 NULL ,因为模拟器设置通知铃声是静音的。

所以我想如果 URI 为 NULL ,我们可以使用 Uri.toString() 将 URI 转换为 String ?

是的,我做了一个测试来证明我的猜测。因为这个android模拟器将铃声、通知、闹钟设置为静音,并且模拟器中没有音乐文件。

所以我首先使用

abd to push 1.mp3 to sdcard/notification/

然后前往

"dev tool -->media provider-->scan sdcard --> insert abulm . and go to android --> system setting-->sound-->set notification to 1.mp3"

再次运行我的代码,就没有问题了。

因此,如果 URI 为 NULL ,我们就不能使用 Uri.toString() 将 uri 转换为字符串,这会出现空点错误。

但是为什么呢?希望有高人能详细解释一下!谢谢

最佳答案

有一个很好的解释Here关于 toString() 方法。您可以添加 if 语句。

 if (ringName != null) {
notification.sound = Uri.parse(ringName);
}

关于android - 使用 toString() 将 URI 转换为 String 导致 Android 应用程序崩溃,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28922904/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com