添加自定义类会导致Compose编译出错。

huangapple go评论54阅读模式
英文:

Adding a custom class breaks Compose compilation

问题

我只是在尝试使用Jetpack Compose开始进行Android开发和Kotlin。请注意,我是一个Kotlin新手,所以我是在不断学习的过程中。我来自JavaScript/TypeScript背景,因此我试图通过以JavaScript术语思考并通过在线搜索在Kotlin术语中实现来学习。

我正在尝试列出设备上安装的所有应用程序。虽然该应用程序到目前为止按预期运行,但我需要添加一个功能来对安装的应用程序名称进行排序。我参考了:https://www.bezkoder.com/kotlin-sort-list-objects/#Create_Class_for_handling_sorting。但是,当我添加一个自定义类来对 `List<ApplicationInfo>` 进行排序时,我的应用程序停止构建。

我在这里包含了我的存储库:https://github.com/Hrishikesh-K/TryKotlin

如果我注释掉 [这些行][1] 以及 [这一行][2],应用程序可以成功构建。但是,根据当前的设置,我收到一个错误:

调用 @Composable 函数的函数必须标记有 @Composable 注释


这指向第 21 行,第 18 个字符,即单词 `compare` 的开头。

我不明白为什么Compose会关心自定义类,毕竟它并不是一个Composable函数。我错过了什么吗?

  [1]: https://github.com/Hrishikesh-K/TryKotlin/blob/main/app/src/main/java/com/hrishikeshk/myapplication/MainActivity.kt#L19-L25
  [2]: https://github.com/Hrishikesh-K/TryKotlin/blob/main/app/src/main/java/com/hrishikeshk/myapplication/MainActivity.kt#L37
英文:

I'm just trying to get started with Android development and Kotlin using Jetpack Compose. Note that, I'm a Kotlin novice, so I'm trying to learn along the way. I come from JavaScript/TypeScript background, so I'm trying to learn by thinking in JavaScript terms and implement in Kotlin terms by searching online.

I'm trying to list all installed applications on the device. While the app was working as expected up till now, I needed to add a feature to sort the installed app names. I referred to: https://www.bezkoder.com/kotlin-sort-list-objects/#Create_Class_for_handling_sorting. As soon as I added a custom class to sort the List&lt;ApplicationInfo&gt;, my app stopped building.

I have included my repo here: https://github.com/Hrishikesh-K/TryKotlin

If I comment these lines and this line as well, the app builds fine. With the current setup, I get an error:

Functions which invoke @Composable functions must be marked with the @Composable annotation

which points to line 21, character 18, which is the start of the word compare.

I don't understand why Compose would care about a custom class, it's not a Composable function after all. What am I missing?

答案1

得分: 1

在 `compare` 方法中,你正在使用 `LocalContext.current`

    override fun compare(o1 : ApplicationInfo, o2 : ApplicationInfo): Int {
        return o1.loadLabel(LocalContext.current.packageManager).toString().compareTo(o2.loadLabel(LocalContext.current.packageManager).toString())
    }

如果该方法未标记为 `@Composable`,你不能使用 `@Composable` 函数。

可以尝试使用不同的方法,如:

    data class CompareApplicationNames(val context: Context) : Comparator&lt;ApplicationInfo&gt; {
    
        override fun compare(o1 : ApplicationInfo, o2 : ApplicationInfo): Int {
            return o1.loadLabel(context.packageManager).toString().compareTo(o2.loadLabel(context.packageManager).toString())
        }
    }

然后只需使用:

    Log.d(&quot;sorted:&quot;, listOfApplications.sortedWith(CompareApplicationNames(LocalContext.current)).toString())
英文:

In the compare method you are using LocalContext.current

override fun compare(o1 : ApplicationInfo, o2 : ApplicationInfo): Int {
    return o1.loadLabel(LocalContext.current.packageManager).toString().compareTo(o2.loadLabel(LocalContext.current.packageManager).toString())
}

You can't use a @Composable functions if the method is not marked with the @Composable.

Use something different like:

data class CompareApplicationNames(val context: Context) : Comparator&lt;ApplicationInfo&gt; {

    override fun compare(o1 : ApplicationInfo, o2 : ApplicationInfo): Int {
        return o1.loadLabel(context.packageManager).toString().compareTo(o2.loadLabel(context.packageManager).toString())
    }
}

Then just use:

Log.d(&quot;sorted:&quot;, listOfApplications.sortedWith(CompareApplicationNames(LocalContext.current)).toString())

huangapple
  • 本文由 发表于 2023年2月7日 02:54:11
  • 转载请务必保留本文链接:https://go.coder-hub.com/75365455.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定