亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

需要數組類型;發現:'java.util.map

需要數組類型;發現:'java.util.map

HUWWW 2023-09-27 17:03:42
我的變量是第一類,例如: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 中執行相同的調用,它會完美運行這些是錯誤: 這是我的真實數組值:companion object{    @JvmField val darkMode = mapOf(        "bgColor" to "#000000",        "cardColor" to "#262626"    )}當我從擴展的類中調用它時,會出現錯誤RecyclerView.ViewHolder
查看完整描述

1 回答

?
尚方寶劍之說

TA貢獻1788條經驗 獲得超4個贊

我剛剛嘗試過這個:


        val foo = mapOf("a" to "Red")

        someView.setBackgroundColor(Color.parseColor(foo["a"]))

它工作正常,您能分享有關異常的更多詳細信息嗎?


更新

我嘗試完全按照所寫的方式使用您的內容,只是我替換了byMyClass的值而不是您的字符串。aRedb


你使用parseColor正確嗎?


    /**

     * </p>Parse the color string, and return the corresponding color-int.

     * If the string cannot be parsed, throws an IllegalArgumentException

     * exception. Supported formats are:</p>

     *

     * <ul>

     *   <li><code>#RRGGBB</code></li>

     *   <li><code>#AARRGGBB</code></li>

     * </ul>

     *

     * <p>The following names are also accepted: <code>red</code>, <code>blue</code>,

     * <code>green</code>, <code>black</code>, <code>white</code>, <code>gray</code>,

     * <code>cyan</code>, <code>magenta</code>, <code>yellow</code>, <code>lightgray</code>,

     * <code>darkgray</code>, <code>grey</code>, <code>lightgrey</code>, <code>darkgrey</code>,

     * <code>aqua</code>, <code>fuchsia</code>, <code>lime</code>, <code>maroon</code>,

     * <code>navy</code>, <code>olive</code>, <code>purple</code>, <code>silver</code>,

     * and <code>teal</code>.</p>

     */

    @ColorInt

    public static int parseColor(@Size(min=1) String colorString) {

        if (colorString.charAt(0) == '#') {

            // Use a long to avoid rollovers on #ffXXXXXX

            long color = Long.parseLong(colorString.substring(1), 16);

            if (colorString.length() == 7) {

                // Set the alpha value

                color |= 0x00000000ff000000;

            } else if (colorString.length() != 9) {

                throw new IllegalArgumentException("Unknown color");

            }

            return (int)color;

        } else {

            Integer color = sColorNameMap.get(colorString.toLowerCase(Locale.ROOT));

            if (color != null) {

                return color;

            }

        }

        throw new IllegalArgumentException("Unknown color");

    }

更新2:

好吧,你說“一個擴展的類RecyclerView.ViewHolder”,但所說的類只是 RecyclerView 類中的一個抽象類,所以它沒有setBackgroundColor,除非你正在做類似的事情:


yourViewHolderInstance.itemView.setBackgroundColor.


那么你的 setBackgroundColor 的簽名是什么?


看起來像嗎public void setBackgroundColor(@ColorInt int color) {?


我只是為了好玩才將其添加到我的應用程序中:


    override fun onBindViewHolder(holder: RecyclerView.ViewHolder, position: Int) {

        (holder as BaseViewHolder).bind(getItem(position))


// For Filippo :)

holder.itemView.setBackgroundColor(Color.parseColor(MyClass.foo["a"]))

    }

嗯……現在一切看起來都很紅。:)


更新3

好的,根據您的最新更新,您是從 Java 調用此函數,因此您不能使用 Kotlin 語法...


做這個:


        final Map<String, String> foo = MyClass.foo;

        yourView.setBackgroundColor(Color.parseColor(foo.get("a")));

顯然,您可以避免中間分配并進行:


        v.setBackgroundColor(Color.parseColor(MyClass.foo.get("a")));

我通常更喜歡前者,特別是如果您給它所有有意義的名稱并且需要調試,但就我而言,沒有真正的區別。


查看完整回答
反對 回復 2023-09-27
  • 1 回答
  • 0 關注
  • 166 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號