1. Are you ready for the Galaxy S20? Here is everything we know so far!

Type mismatch for StartActivity: Required Context, Found Intent

Discussion in 'Android Development' started by Daniel Fernandes, Nov 17, 2019.

  1. Daniel Fernandes

    Daniel Fernandes Android Enthusiast
    Thread Starter

    So I was following the official guide on how to use WebView and I found a piece of code that I just can't get to work. Basically, if a URL the user clicked belongs to a particular domain (www.example.com), the WebView should open the link within itself. But if the URL is not part of the domain, then the link should be opened in an external browser of the users choice (by showing the thing where you pick your browser, and have the options 'Always' and 'Just once').

    Here's my code:
    Code (Text):
    1. package com.cryptosonix.myapp
    3. import android.content.Intent
    4. import android.net.Uri
    5. import androidx.appcompat.app.AppCompatActivity
    6. import android.os.Bundle
    7. import android.view.View
    8. import android.webkit.*
    9. import androidx.core.content.ContextCompat.startActivity
    10. import kotlinx.android.synthetic.main.activity_main.*
    11. import kotlinx.android.synthetic.main.fragment_ig_user1.*
    14. class MainActivity : AppCompatActivity() {
    16.     override fun onCreate(savedInstanceState: Bundle?) {
    17.         super.onCreate(savedInstanceState)
    18.         setContentView(R.layout.activity_main)
    21.         web_user1.settings.javaScriptEnabled = true // web_user1 is the WebView
    22.         web_user1.settings.allowFileAccessFromFileURLs = true
    23.         web_user1.webViewClient = MyWebViewClient()
    24.         web_user1.loadUrl("https://example.com/page/")
    25.         //Starting another activity within the app works fine
    26.         settings_fab.setOnClickListener{startActivity(Intent(this, SettingsActivity::class.java))}
    27.     }
    29.     private class MyWebViewClient : WebViewClient() {
    31.         override fun shouldOverrideUrlLoading(view: WebView?, url: String?): Boolean {
    32.             if (Uri.parse(url).host == "www.example.com") {
    33.                 return false
    34.             }
    35.             Intent(Intent.ACTION_VIEW, Uri.parse(url)).apply {
    36.                 startActivity(this) //Here's the problem!
    37.             }
    38.             //When I hover my mouse over 'this', a popup appears with the type mismatch error.
    39.             return true
    40.         }
    41.     }
    42. }
    I've been stuck on this for two days, couldn't find anything that worked. So I decided to show my entire code here so that one can see exactly where I went wrong, I know a lot of you are experts here. Any help will be very much appreciated!

  2. Best Answer:
    Post #2 by Daniel Fernandes, Nov 17, 2019 (3 points)
  3. Daniel Fernandes

    Daniel Fernandes Android Enthusiast
    Thread Starter

    Ok I finally figured it out (yay)! To those who want to know, MyWebViewClient had to be placed inside onCreate, but above the line: web_user1.webViewClient = MyWebViewClient()
    codesplice and mikedt like this.

Share This Page