android 如何解决合成中材料图标的“由于接收器类型不匹配,以下候选项均不适用”问题

4nkexdtk  于 2022-11-27  发布在  Android
关注(0)|答案(1)|浏览(119)

我在Gruda Linux上使用compose 3和海豚Android Studio。全部更新!
我有一个名为feed的@Composable函数,它只有一个FilledTonalButton
对于FilledTonalButton的内容我给出icons.rounded.Add
每当我建造的时候

e: /home/kumar-p/Desktop/AndroidStudio/<app_name>/app/src/main/java/com/f/rateme/MainActivity.kt: (95, 27): Unresolved reference. None of the following candidates is applicable because of receiver type mismatch: 
public val Icons.Rounded.Add: ImageVector defined in androidx.compose.material.icons.rounded

<app_name>=我的应用程序名称,由于保密原因已删除。
然而,当我用Text()替换Icon()时,它工作了!
我的相依性:

implementation 'androidx.core:core-ktx:1.7.0'
implementation 'androidx.lifecycle:lifecycle-runtime-ktx:2.3.1'
implementation 'androidx.activity:activity-compose:1.3.1'
implementation "androidx.compose.ui:ui:$compose_version"
implementation "androidx.compose.ui:ui-tooling-preview:$compose_version"
implementation 'androidx.compose.material3:material3:1.0.0-alpha02'
implementation "androidx.compose.material:material-icons-extended:$compose_version"
testImplementation 'junit:junit:4.13.2'
androidTestImplementation 'androidx.test.ext:junit:1.1.4'
androidTestImplementation 'androidx.test.espresso:espresso-core:3.5.0'
androidTestImplementation "androidx.compose.ui:ui-test-junit4:$compose_version"
debugImplementation "androidx.compose.ui:ui-tooling:$compose_version"
debugImplementation "androidx.compose.ui:ui-test-manifest:$compose_version"

我在MainActivity.kt上的导入

package com.<example>.rateme // <example> = my company name

import android.os.Bundle
import androidx.activity.ComponentActivity
import androidx.activity.compose.setContent
import androidx.compose.foundation.layout.fillMaxSize
import androidx.compose.material.*
import androidx.compose.material.icons.rounded.Add
import androidx.compose.material3.*
//import androidx.compose.material.icons.Icons
import androidx.compose.runtime.Composable
import androidx.compose.ui.Modifier
import androidx.compose.ui.tooling.preview.Preview

import com.<example>.<app_name>.ui.theme.AppTheme // <example> = my company name, <app_name> = name of curent app

功能:

@Composable
fun feed(page: String): String {

    FilledTonalButton(
        onClick = {/*TODO: Add onClick for feed - New Post*/},
        enabled = true) {
        Icon(
            imageVector = Add,
            "New Post"
        )
    }

    return page // TODO: implement the changes of page variable
}

如果你发现任何错字,请更新它或告诉我修复。
PS:这是我第一次来这里,我对Android开发和Kotlin几乎没有任何经验。我开发了终端应用程序,用Python、C++、C进行了ML工作。所以我可能需要更多的信息来解释。我一周前才开始学习Android开发。
编辑:你可以问我任何更多的信息。
平安

hc2pp10m

hc2pp10m1#

只需像这样指定实际的imageVector,

Icon(
    imageVector = Icons.Default.Add
    "New Post"
 )

但是你仍然会得到歧义错误。因为根据你的帖子看起来你想使用material3,所以删除这个导入

import androidx.compose.material.* // remove this

或者如果您仍然希望同时使用它们,那么您必须在使用时声明它们的完全限定名,

androidx.compose.material3.Icon(
     imageVector = Icons.Default.Add,
     "New Post"
)

相关问题