简体   繁体   中英

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):

 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. 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

Make sure that an Activity is handling your intent, placing this code into a java class of an Activity.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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