【问题标题】:resolveActivity works if I don't check if it is null如果我不检查它是否为空,resolveActivity 有效
【发布时间】:2020-11-04 02:27:56
【问题描述】:

我编写这段代码是为了从我在 android studio(google) 中的应用程序在浏览器中打开一个网站:

 String google = "http://www.google.com";
 Uri webAddress = Uri.parse(google);

Intent goToGoogle= new Intent(Intent.ACTION_VIEW, webAddress);


if(goToGoogle.resolveActivity(getPackageManager()) != null) {
                startActivity(goToGoogle);
            }

当我不放 if 语句时,该应用程序只会执行我希望它执行的操作,否则该按钮将不执行任何操作。这是为什么呢?

谢谢

【问题讨论】:

    标签: java android-studio android-intent


    【解决方案1】:

    resolveActivity() 方法返回 Activity 组件,用于处理 Intent,因此,如果有 Activity 处理该 Intent,它将返回 @987654322 @

    确保 Activity 正在处理您的意图,将此代码放入 Activity 的 java 类中。

    String google = "http://www.google.com";
     Uri webAddress = Uri.parse(google);
    
    Intent goToGoogle= new Intent(Intent.ACTION_VIEW);
    goToGoogle.setData(webAdress);
    
    
    if(goToGoogle.resolveActivity(getPackageManager()) != null) {
                    startActivity(goToGoogle);
                }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2022-12-12
      • 1970-01-01
      • 2020-11-09
      • 2013-05-09
      • 2014-04-13
      相关资源
      最近更新 更多