简体   繁体   English

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

[英]app crash and doesn't display Toast message

I am new to Android development. 我是Android开发的新手。 I want to display a Toast message when the app is not installed. 我想在未安装应用程序时显示Toast消息。 When the app is not installed, for example the Facebook app, the app is crashing. 如果未安装该应用程序(例如Facebook应用程序),则该应用程序将崩溃。 What is the problem in my code? 我的代码有什么问题?

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;

You check for null too late, try this: 您为时过晚检查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;

要显示烤面包,您需要在烤面包对象上调用.show()

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM