android - 如何使用按钮从 Fragment 重定向到 URL

标签 android android-fragments kotlin

有一个similar question用于从 Activity 重定向,

但我无法从 Fragment 中找到这样做的方法。

    override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View? {

        ...

        view.myButton.setOnClickListener {
            val urlIntent = Intent("android.intent.action.VIEW", Uri.parse(WEBSITE_URL))

            startActivity(urlIntent)
        }

        return view
    }

以上代码产生异常:

android.content.ActivityNotFoundException: No Activity found to handle Intent { act=android.intent.action.VIEW dat="https://www.google.com/" }

我得到了 Main Activity 实例(如 MainActivity.instance)所以 我尝试使用 setClasssetComponent 来尝试处理 Intent,但没有成功。

最佳答案

试试这个:

String url = "https://www.google.com";
Intent i = new Intent(Intent.ACTION_VIEW);
i.setData(Uri.parse(url)); 
startActivity(i); 

关于android - 如何使用按钮从 Fragment 重定向到 URL,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54281219/

相关文章:

Android TextView setTextSize 错误地增加了文本大小

android - 后退按钮按下处理程序

android - 无法从 Firestore 获取嵌套数据并将其显示在 Recyclerview 中

kotlin - 伴生对象比普通对象有什么优势?

java - Android 中的相机应用程序不使用 Intent

android - 使用 Android 加速度计检测振动

android - 如何使用 DialogFragment 和 FragmentManager 制作 DatePicker?

android - 如何将工具栏添加到android中的 fragment ?

android - 刷新 MediaBrowserService 订阅内容

android - runtimeexception :unable to destroy activity and RuntimeException:. nosuchfieldexception: mChildfragmentManager