【问题标题】:Kotlin - Uncaught TypeError: X is not a function - JavascriptInterface with WebViewsKotlin - Uncaught TypeError: X is not a function - JavascriptInterface with WebViews
【发布时间】:2019-04-24 14:23:45
【问题描述】:

我有一个带有 cshtml 文件的后台应用程序,该文件基本上用于进行身份验证。

现在,在我的 Kotlin 安卓应用中,我使用 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)

你怎么看?我尝试了一些小改动并在互联网上搜索了它,但没有解决它......

感谢您的阅读, 唐吉。

【问题讨论】:

  • 知道如何解决这个问题吗?

标签: android kotlin webview


【解决方案1】:

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多