gpt4 book ai didi

java - 应用程序崩溃并且不显示 Toast 消息

转载 作者:行者123 更新时间:2023-11-29 07:24:28 24 4
gpt4 key购买 nike

我是 Android 开发新手。我想在未安装应用程序时显示 Toast 消息。当应用程序未安装时,例如 Facebook 应用程序,应用程序崩溃。我的代码有什么问题?

case R.id.Facebook:
Intent facebook = getPackageManager().getLaunchIntentForPackage("com.facebook.katana");
startActivity(facebook);
if (facebook != null) {
Toast.makeText(this,"Facebook is not installed ",Toast.LENGTH_LONG);
}
return true;

最佳答案

你检查 null 太晚了,试试这个:

case R.id.Facebook:
Intent facebook = getPackageManager().getLaunchIntentForPackage("com.facebook.katana");
if (facebook == null) {
Toast.makeText(this,"Facebook is not installed ",Toast.LENGTH_LONG).show();
} else {
startActivity(facebook);
}
return true;

关于java - 应用程序崩溃并且不显示 Toast 消息,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56899787/

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