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
    2.  
    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.*
    12.  
    13.  
    14. class MainActivity : AppCompatActivity() {
    15.  
    16.     override fun onCreate(savedInstanceState: Bundle?) {
    17.         super.onCreate(savedInstanceState)
    18.         setContentView(R.layout.activity_main)
    19.  
    20.  
    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.     }
    28.  
    29.     private class MyWebViewClient : WebViewClient() {
    30.  
    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. }
    43.  
    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 (4 points)

    1. Download the Forums for Android™ app!


      Download

       
  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()
     
    MrJavi, codesplice and mikedt like this.
  4. YAY!!! Thank you. Could not figure this out either. You are the bomb! :)
     
    MrJavi likes this.
Loading...
Similar Threads - Type mismatch StartActivity
  1. App Update
    Replies:
    0
    Views:
    194
  2. Preacher Al
    Replies:
    6
    Views:
    485
  3. jpg001
    Replies:
    2
    Views:
    401
  4. 14km
    Replies:
    2
    Views:
    337
  5. infomamun
    Replies:
    3
    Views:
    461
  6. dorlow
    Replies:
    3
    Views:
    387
  7. rlwolfgram
    Replies:
    2
    Views:
    307
  8. Novice App User
    Replies:
    3
    Views:
    371
  9. Pavel_47
    Replies:
    3
    Views:
    491
  10. HelpMe11
    Replies:
    3
    Views:
    894

Share This Page

Loading...