簡體   English   中英

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

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

我編寫此代碼是為了在 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);
            }

當我不放 if 語句時,該應用程序只會執行我希望它執行的操作,否則該按鈕不執行任何操作。 這是為什么?

謝謝

resolveActivity()方法返回用於處理IntentActivity 組件,因此,如果有處理 Intent 的 Activity,它將返回true

確保 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