This is my kotlin code:
class WebAppInterface(private val mContext: Context, private val myWebView: WebView) {
@JavascriptInterface
fun goToHello() {
val myWebView = myWebView
myWebView.loadUrl("file:///android_asset/hello.html")
}
}
class MainActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
val myWebView: WebView = findViewById(R.id.webview)
myWebView.settings.javaScriptEnabled = true
myWebView.addJavascriptInterface(WebAppInterface(this, myWebView), "Android")
myWebView.loadUrl("file:///android_asset/index.html")
}
}
But it's crash.
Caused by: java.lang.Throwable: A WebView method was called on thread 'JavaBridge'. All WebView methods must be called on the same thread.
How can I fix this? I found a solution by searching but this was java code. Kotlin and Java are compatible but did not run due to a syntax error. (I think it's because I don't know Kotlin and Java. Anyway)
I'm a JavaScript-based full stack developer and I don't have any Android knowledge so I only wanted to use webviews. But this is also very difficult for me. (It's like the period of chaos when I first learned JavaScript.)
Please tell me the solution. ps. If you have a good list of Kotlin's inheritance and scope concepts, please link.