android - 尝试在什么应用程序(未安装)上共享它时,我的android应用程序崩溃

标签 android android-studio crash whatsapp

 Button clickButton = (Button) findViewById(R.id.newbutton);

        assert clickButton != null;

        clickButton.setOnClickListener(new View.OnClickListener() {

            @Override

            public void onClick(View v) {


    Intent uriIntent = new Intent(Intent.ACTION_VIEW, Uri.parse("whatsapp://send?text=" + "Text you want to share"));

  startActivity(uriIntent);

 }

   });

最佳答案

这是预期的行为,因为某些用户没有安装某些应用程序绝对是合理的。您需要通过调用intent.resolveActivity()并检查结果是否不为空来检查是否有任何 Activity 可以处理该意图。

有关意图和意图过滤器的开发文档将其声明为here:

Caution: It's possible that a user won't have any apps that handle the implicit intent you send to startActivity(). Or, an app may be inaccessible because of profile restrictions or settings put into place by an administrator. If that happens, the call fails and your app crashes. To verify that an activity will receive the intent, call resolveActivity() on your Intent object. If the result is non-null, there is at least one app that can handle the intent and it's safe to call startActivity(). If the result is null, do not use the intent and, if possible, you should disable the feature that issues the intent. The following example shows how to verify that the intent resolves to an activity. This example doesn't use a URI, but the intent's data type is declared to specify the content carried by the extras.

关于android - 尝试在什么应用程序(未安装)上共享它时,我的android应用程序崩溃,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61078042/

相关文章:

crash - pyQt5 中退出时出现段错误,但 pyQt4 中没有

android - 使用 NavController 导航时只允许一个实例

android - 如何将文本放在android中单选按钮的左侧

android - 在 Android 的 Flash 中浏览多张照片

android - 错误应用插件 : 'com.google.gsm.google-services' also added classpath 'com.google.gms:google-services:4.2.0'

php - 从MySQL数据库检索简体中文到Android应用程序

android - 适用于Android OS的依赖项下载器

android - 单击TDateEdit(FMX Control)导致Android应用程序崩溃

windows-xp - 检测应用程序崩溃并重新启动的最佳方法?

java - 打开文件时出现异常