简体   繁体   English

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

[英]Unresolved reference: createPrintDocumentAdapter (Kotlin + Android)

I'm trying to implement the code from this link using kotlin , but when I try to use any method of the webView I get errors: 我正在尝试使用kotlin从此链接中实现代码,但是当我尝试使用webView的任何方法时,都会出现错误:

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

The strange thing is if I comment out the code and while running the app I use the evaluate expression I can create the adapter instance. 奇怪的是,如果我注释掉代码,并且在运行应用程序时使用评估表达式,我可以创建适配器实例。

Has anyone any Idea? 有任何想法吗?

I also can't access methods from the webView , like webView.webViewClient 我也无法从webView访问方法,例如webView.webViewClient

Here is the kotlin version of the java code in the link above 这是上面链接中的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())

}

I found that the problem is probably due to a bug in the anko library, when I removed the anko-appcompat-v7-coroutines the code worked 我发现问题可能是由于anko库中的错误所致,当我删除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"

My anko version was ext.anko_version = '0.10.0' since I wans't using anko just yet I opted to remove it and things started to work as expected. 我的anko版本是ext.anko_version = '0.10.0'因为我不想使用anko,但我选择将其删除,并且一切正常。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM