未解决的参考:createPrintDocumentAdapter(Kotlin + Android)

我试图从这个链接使用kotlin实现代码,但是当我尝试使用webView的任何方法时,我得到的错误:

Error:(238, 17) Unresolved reference: webViewClient Error:(265, 43) Unresolved reference: PRINT_SERVICE Error:(268, 36) Unresolved reference: createPrintDocumentAdapter

奇怪的是,如果我注释掉代码,并运行应用程序,我使用评估表达式,我可以创建适配器实例。

有没有人有任何想法?

我也无法从webView访问方法,如webView.webViewClient

这里是上面链接中的java代码的kotlin版本

 private fun doWebViewPrint() { // Create a WebView object specifically for printing val webView = WebView(this) webView.webViewClient = object : WebViewClient() { override fun shouldOverrideUrlLoading(view: WebView, url: String): Boolean { return false } override fun onPageFinished(view: WebView, url: String) { createWebPrintJob(view) } } // Generate an HTML document on the fly: val htmlDocument = "<html><body><h1>Test Content</h1><p>Testing, " + "testing, testing...</p></body></html>" webView.loadDataWithBaseURL(null, htmlDocument, "text/HTML", "UTF-8", null) } private fun createWebPrintJob(webView: WebView) { // Get a PrintManager instance val printManager = this .getSystemService(Context.PRINT_SERVICE) as PrintManager // Get a print adapter instance val printAdapter = webView.createPrintDocumentAdapter("document") // Create a print job with name and adapter instance val jobName = getString(R.string.app_name) + " Document" val printJob = printManager.print(jobName, printAdapter, PrintAttributes.Builder().build()) } 

我发现这个问题可能是由于anko库中的一个bug,当我删除anko-appcompat-v7-coroutines代码的工作

  // Anko Layouts compile "org.jetbrains.anko:anko-sdk25:$anko_version" // sdk15, sdk19, sdk21, sdk23 are also available compile "org.jetbrains.anko:anko-appcompat-v7:$anko_version" // Coroutine listeners for Anko Layouts compile "org.jetbrains.anko:anko-sdk2-coroutines:$anko_version" compile "org.jetbrains.anko:anko-appcompat-v7-coroutines:$anko_version" 

我的anko版本是ext.anko_version = '0.10.0'因为我没有使用anko,但我选择删除它,事情开始按预期工作。