简体   繁体   中英

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:

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

Here is the kotlin version of the java code in the link above

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 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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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