简体   繁体   English

如果我不检查它是否是 null,resolveActivity 工作

[英]resolveActivity works if I don't check if it is null

I wrote this code to open a website in the browser from my app in android studio(google):我编写此代码是为了在 android 工作室(谷歌)中的应用程序中在浏览器中打开一个网站:

 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);
            }

the app just does what I want it to do when I do not put the if statement, otherwise the button does nothing.当我不放 if 语句时,该应用程序只会执行我希望它执行的操作,否则该按钮不执行任何操作。 Why is that?这是为什么?

thanks谢谢

The resolveActivity() method returns the Activity Component that is used to handle the Intent , so, if there is an Activity handling the intent, it will return true resolveActivity()方法返回用于处理IntentActivity 组件,因此,如果有处理 Intent 的 Activity,它将返回true

Make sure that an Activity is handling your intent, placing this code into a java class of an Activity.确保 Activity 正在处理您的意图,将此代码放入 Activity 的 java class 中。

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);
            }

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

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