什么是“活动?”。在Kotlin中的意思?

发布于 2025-02-14 01:59:19 字数 578 浏览 7 评论 0原文

我正在尝试使用官方Android文档中的代码段( https> https:htttps:// devagenter。 android.com/training/printing/photos#kotlin )是dophotoprint()函数在我附加的代码中,以学习如何在Kotlin和Android Studio中使用打印机类。请参阅代码段的随附图像:

”

问题是当我将代码放入我的测试应用程序中的主要活动不断显示“活动?”。像红色。为什么这种情况?如何使代码工作以便为用户提供打印的选项?谢谢

I am trying to use a code snippet from the official android documentation (https://developer.android.com/training/printing/photos#kotlin) which is the doPhotoPrint() function in the code that I have attached, in order to learn how to use the PrintHelper class in Kotlin and Android Studio. See the attached image of the the code snippet:

doPhotoPrint() function

The problem is that when I put the code in Main Activity in my test app, it keeps showing "activity?." as red. Why is this the case and how can I get the code to work so that is provides the user with the option to print? Thanks

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

深居我梦 2025-02-21 01:59:19

您链接的代码只是一般如何使用库函数代码 shippet-均未在任何上下文中放置,但是我们可以假设它可能是用fragment编写的。片段具有a getActivity(getAccevity> getActivity() 返回活动的方法当前已连接到(或 null (如果不是))

Kotlin允许您从Java代码 > - 基本上,而不是必须做value = getthing()
setthing(newValue)您可以将其视为变量:value = thingthing = newValue等。 >活动属性在此代码中,它确实在调用getActivity()。而且,由于结果可能为null,因此您在尝试对其进行任何操作之前,请使用对其进行null检查。

该代码的说法确实是,该代码需要访问上下文,并且使用活动作为示例中的一个。如果您有一个fragment,则可以直接删除此代码。如果您在activity中一个 - 这个是您的活动!当然,您无需取消检查它。 (并且没有活动属性或getActivity()访问方法,这就是为什么它显示出红色和未被认可的原因)

,因此您可以替换这些东西和周围的检查代码它与:

private fun doPhotoPrint() {
    // using 'this' to pass the current activity as the context
    val printHelper = PrintHelper(this).apply {
        scaleMode = PrintHelper.SCALE_MODE_FIT
    }
    val bitmap = BitmapFactory.decodeResource(resources, R.drawable.droids)
    printHelper.printBitmap("droids.jpg - test print", bitmap)
}

The code you linked is just a general how to use a library function snippet - it's not put in any context, but we can assume it's probably written with a Fragment in mind. Fragments have a getActivity() method that returns the Activity it's currently attached to (or null if it's not)

Kotlin allows you to access getter and setter functions from Java code as if they were properties - so basically, instead of having to do value = getThing()
and setThing(newValue) you can treat it like a variable: value = thing, thing = newValue etc. So when you access the activity property in this code, it's really calling getActivity(). And because the result can be null, you use the ? to null-check it before trying to do anything with it.

Really what that snippet is saying is, this code needs access to a Context, and it's using an Activity as one in the example. If you have a Fragment, you can just drop this code right in. If you're in an Activity though, then you obviously don't need to get one - this is your Activity! And you don't need to null-check it either of course. (And there's no activity property or getActivity() method to access, which is why it's showing up red and unrecognised)

So you can just replace that stuff and the checking code around it with:

private fun doPhotoPrint() {
    // using 'this' to pass the current activity as the context
    val printHelper = PrintHelper(this).apply {
        scaleMode = PrintHelper.SCALE_MODE_FIT
    }
    val bitmap = BitmapFactory.decodeResource(resources, R.drawable.droids)
    printHelper.printBitmap("droids.jpg - test print", bitmap)
}
猛虎独行 2025-02-21 01:59:19

它显示红色,因为您在使用之前尚未声明无效的对象活动

It is showing red because you have not declared the nullable object activity anyway before you use it

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文