Kotlin-未捕获的TypeError:X不是函数-WebViews的JavascriptInterface

时间:2019-04-24 14:23:46

标签: android kotlin webview

我有一个带有cshtml文件的后台应用程序,该文件基本上用于获得认证。

现在,在Kotlin的Android应用中,我正在使用WebView和JavascriptInterface调用所需的URL,并在后面调用方法。

有一些有用的代码行:

登录活动:

val myWebView: WebView = findViewById(R.id.connexionWebview)
        myWebView.setWebViewClient(object : WebViewClient() {
            override fun shouldOverrideUrlLoading(view: WebView, url: String): Boolean {
                view.loadUrl(url)
                return false
            }
        })
        myWebView.settings.javaScriptEnabled = true
        myWebView.addJavascriptInterface(AuthenticatedHandler(this), "AndroidAuthenticatedManager")
        myWebView.loadUrl("HiddenURL")

AuthenticatedHandler.kt:

class AuthenticatedHandler(private val mContext: Context): AppCompatActivity() {
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        @JavascriptInterface
        fun onAuthenticated(token: String, email: String, userId: Int) {
            val intent = Intent(this, Home::class.java)
            val extras = Bundle()
            extras.putString("token", token)
            startActivity(intent)
        }
    }
}`

在我的身后,Authenticated.cshtml:

AndroidAuthenticatedManager.onAuthenticated('@token.AccessToken', '@email', @userId);

这是我得到的错误:

I/chromium: [INFO:CONSOLE(14)] "Uncaught TypeError: AndroidAuthenticatedManager.onAuthenticated is not a function", source: http://HidenURL:5000/Account/Authenticated (14)

您如何看待?我尝试了一些小的更改,并在互联网上进行了搜索,但没有任何解决方法...

感谢您的阅读, 探戈。

1 个答案:

答案 0 :(得分:0)

我认为@JavascriptInterface fun onAuthenticated()应该在override fun onCreate()之外,而不是里面。