需要数组类型;发现:'java.util.map

我的变量是第一类,例如:


MyClass{

    companion Object{

        @JvmField val foo = mapOf("a" to "b")

    }

}

当我从另一个类调用它时,例如:


setBackgroundColor(Color.parseColor(MyClass.foo["..."]));

出现错误“需要数组类型;发现:'java.util.map < java.lang.String,java.lang.String>'” 问题是什么?


请注意,如果我在 MyClass 中执行相同的调用,它会完美运行


这些是错误: 

https://img3.mukewang.com/6513f02b0001861a05130047.jpg

这是我的真实数组值:


companion object{

    @JvmField val darkMode = mapOf(

        "bgColor" to "#000000",

        "cardColor" to "#262626"

    )

}

当我从扩展的类中调用它时,会出现错误RecyclerView.ViewHolder

https://img3.mukewang.com/6513f03b0001c3f008450079.jpg

HUWWW
浏览 101回答 1
1回答

尚方宝剑之说

我刚刚尝试过这个:&nbsp; &nbsp; &nbsp; &nbsp; val foo = mapOf("a" to "Red")&nbsp; &nbsp; &nbsp; &nbsp; someView.setBackgroundColor(Color.parseColor(foo["a"]))它工作正常,您能分享有关异常的更多详细信息吗?更新我尝试完全按照所写的方式使用您的内容,只是我替换了byMyClass的值而不是您的字符串。aRedb你使用parseColor正确吗?&nbsp; &nbsp; /**&nbsp; &nbsp; &nbsp;* </p>Parse the color string, and return the corresponding color-int.&nbsp; &nbsp; &nbsp;* If the string cannot be parsed, throws an IllegalArgumentException&nbsp; &nbsp; &nbsp;* exception. Supported formats are:</p>&nbsp; &nbsp; &nbsp;*&nbsp; &nbsp; &nbsp;* <ul>&nbsp; &nbsp; &nbsp;*&nbsp; &nbsp;<li><code>#RRGGBB</code></li>&nbsp; &nbsp; &nbsp;*&nbsp; &nbsp;<li><code>#AARRGGBB</code></li>&nbsp; &nbsp; &nbsp;* </ul>&nbsp; &nbsp; &nbsp;*&nbsp; &nbsp; &nbsp;* <p>The following names are also accepted: <code>red</code>, <code>blue</code>,&nbsp; &nbsp; &nbsp;* <code>green</code>, <code>black</code>, <code>white</code>, <code>gray</code>,&nbsp; &nbsp; &nbsp;* <code>cyan</code>, <code>magenta</code>, <code>yellow</code>, <code>lightgray</code>,&nbsp; &nbsp; &nbsp;* <code>darkgray</code>, <code>grey</code>, <code>lightgrey</code>, <code>darkgrey</code>,&nbsp; &nbsp; &nbsp;* <code>aqua</code>, <code>fuchsia</code>, <code>lime</code>, <code>maroon</code>,&nbsp; &nbsp; &nbsp;* <code>navy</code>, <code>olive</code>, <code>purple</code>, <code>silver</code>,&nbsp; &nbsp; &nbsp;* and <code>teal</code>.</p>&nbsp; &nbsp; &nbsp;*/&nbsp; &nbsp; @ColorInt&nbsp; &nbsp; public static int parseColor(@Size(min=1) String colorString) {&nbsp; &nbsp; &nbsp; &nbsp; if (colorString.charAt(0) == '#') {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; // Use a long to avoid rollovers on #ffXXXXXX&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; long color = Long.parseLong(colorString.substring(1), 16);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if (colorString.length() == 7) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; // Set the alpha value&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; color |= 0x00000000ff000000;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; } else if (colorString.length() != 9) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; throw new IllegalArgumentException("Unknown color");&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; return (int)color;&nbsp; &nbsp; &nbsp; &nbsp; } else {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Integer color = sColorNameMap.get(colorString.toLowerCase(Locale.ROOT));&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if (color != null) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; return color;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; throw new IllegalArgumentException("Unknown color");&nbsp; &nbsp; }更新2:好吧,你说“一个扩展的类RecyclerView.ViewHolder”,但所说的类只是 RecyclerView 类中的一个抽象类,所以它没有setBackgroundColor,除非你正在做类似的事情:yourViewHolderInstance.itemView.setBackgroundColor.那么你的 setBackgroundColor 的签名是什么?看起来像吗public void setBackgroundColor(@ColorInt int color) {?我只是为了好玩才将其添加到我的应用程序中:&nbsp; &nbsp; override fun onBindViewHolder(holder: RecyclerView.ViewHolder, position: Int) {&nbsp; &nbsp; &nbsp; &nbsp; (holder as BaseViewHolder).bind(getItem(position))// For Filippo :)holder.itemView.setBackgroundColor(Color.parseColor(MyClass.foo["a"]))&nbsp; &nbsp; }嗯……现在一切看起来都很红。:)更新3好的,根据您的最新更新,您是从 Java 调用此函数,因此您不能使用 Kotlin 语法...做这个:&nbsp; &nbsp; &nbsp; &nbsp; final Map<String, String> foo = MyClass.foo;&nbsp; &nbsp; &nbsp; &nbsp; yourView.setBackgroundColor(Color.parseColor(foo.get("a")));显然,您可以避免中间分配并进行:&nbsp; &nbsp; &nbsp; &nbsp; v.setBackgroundColor(Color.parseColor(MyClass.foo.get("a")));我通常更喜欢前者,特别是如果您给它所有有意义的名称并且需要调试,但就我而言,没有真正的区别。
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Java