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.