Android Studio 隐式意图基本问题

时间:2021-04-07 10:35:01

标签: android

我正在学习 Android 基础知识 02.3:隐式意图,我按照步骤操作但无法处理意图。 这是我写的:

   public void openWebsite(View view) {
    String url = mWebsiteEditText.getText().toString();
    Uri webpage = Uri.parse(url); 
    Intent intent = new Intent(Intent.ACTION_VIEW, webpage);
    if (intent.resolveActivity(getPackageManager()) != null) {  
    startActivity(intent);
    }else{
        Log.d("ImplicitIntents", "Can't handle this!");
    }
}

这是我被告知要写的内容:

public void openWebsite(View view) {
        // Get the URL text.
        String url = mWebsiteEditText.getText().toString();

        // Parse the URI and create the intent.
        Uri webpage = Uri.parse(url);
        Intent intent = new Intent(Intent.ACTION_VIEW, webpage);

        // Find an activity to hand the intent and start that activity.
        if (intent.resolveActivity(getPackageManager()) != null) {
            startActivity(intent);
        } else {
            Log.d("ImplicitIntents", "Can't handle this!");
        }
    }

但我就是跑不动!

D/ImplicitIntents: Can't handle this!

我尝试了什么:

当我下载官方项目并构建时,它可以工作。 当我删除“if”时,它起作用了:

public void openWebsite(View view) {
        String url = mWebsiteEditText.getText().toString();
        Uri webpage = Uri.parse(url); 
        Intent intent = new Intent(Intent.ACTION_VIEW, webpage); 
        startActivity(intent);
    }

那么问题来了,为什么会这样?

0 个答案:

没有答案
相关问题