0

I have a class named MyWebview is used to load URL, and I call it in MainActivity. But something went wrong. I don't know where is the problem, only if MyWebview has parameters(URL and the page I wanna go back) there is an error:has no zero argument constructor. This is MyWebview.

class MyWebview(url:String, lastPage:Class<*>?) : AppCompatActivity() {
    val lastPage = lastPage
    val myURL : String = url
    val webview : WebView by lazy { findViewById(R.id.webview) }
    val closeButton : TextView by lazy { findViewById(R.id.close_webview) }


    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_web_view)

        webViewSetup()
        closeButton.setOnClickListener{
            val intent = Intent(this, lastPage)
            startActivity(intent)
        }
    }

    private fun webViewSetup() {
        webview.webViewClient = WebViewClient()
        webview.apply {
            loadUrl(myURL)
            settings.javaScriptEnabled = true
            settings.safeBrowsingEnabled = true
            settings.allowContentAccess = true
        }
    }

    override fun onBackPressed() {
        if(webview.canGoBack()) {
            webview.goBack()
        } else {
            super.onBackPressed()
        }
    }
}

This is MainActivity

iButton.setOnClickListener {
            val myWebview = MyWebview("https://www.google.com/", MainActivity::class.java)
            val intent = Intent(this, myWebview::class.java)
            startActivity(intent)
        }

And this is error message.

java.lang.RuntimeException: Unable to instantiate activity ComponentInfo{com.starlingmedical.pete/com.starlingmedical.pete.webview.MyWebview}: java.lang.InstantiationException: java.lang.Class<com.starlingmedical.pete.webview.MyWebview> has no zero argument constructor

Thank you in advance.

Harvey Xie
  • 81
  • 4
  • Get rid of the `MyWebview` constructor and get rid of the `val myWebview = MyWebview("https://www.google.com/", MainActivity::class.java)` statement. You can use [extras](https://developer.android.com/guide/components/activities/parcelables-and-bundles#sdba) to pass your URL along, and you do not need `lastPage`. It would be better if you had just one activity and used multiple fragments (or composables) for multiple screens. – CommonsWare Dec 05 '22 at 23:49
  • 1. If your class extends Activity you must have a zero-argument constructor that the Android system can call to build it, and 2. you should *never* instantiate an Activity yourself by calling its constructor (which you are doing with `val myWebview = MyWebview(...)`. If you want to pass arguments to it (like a URL), you should add them as extras on the intent instead. Have a look [here](https://stackoverflow.com/questions/2091465/how-do-i-pass-data-between-activities-in-android-application) for examples. – Tyler V Dec 06 '22 at 03:03
  • 3. Calling `::class` on an instance of a class still just returns the class, not anything having to do with an instance of it that has state like you were trying to pass to your forbidden constructor. – Tenfour04 Dec 06 '22 at 03:29

0 Answers0