报错‘Cannot access '<init>': it is package-private in 'Icon'’的原因分析
报错原因分析
- 类混淆与参数缺失引发的匹配错误:你导入的是Compose Material3的
androidx.compose.material3.Icon组件,但在BottomAppBar的IconButton中,仅传入Icons.Filled.Edit这一个参数,未指定contentDescription参数。此时编译器未匹配到需要至少两个参数的Compose Icon组件,反而错误匹配了Android原生的android.graphics.drawable.Icon类:Cannot access '<init>': it is package-private in 'Icon':原生Icon的构造方法为包私有,外部无法直接实例化Too many arguments for public/*package*/ constructor Icon() defined in android.graphics.drawable.Icon:原生Icon的构造方法不接受你传入的ImageVector类型参数,参数数量与类型均不匹配
- FloatingActionButton中Icon无报错的原因:该Icon同时传入了
Icons.Default.Add和contentDescription = "Add"两个参数,编译器正确匹配到你导入的Compose Material3 Icon组件,因此可正常运行。
内容的提问来源于stack exchange,提问作者Kalcifer
相关产品推荐
相关产品推荐

