Android 分析字符串中的颜色时出错

Android 分析字符串中的颜色时出错,android,colors,numberformatexception,Android,Colors,Numberformatexception,编辑:由我的项目的相关部分组成的粘贴箱: 此外,colorEdItem是以下各项的包装: public class ColouredItem {//Only a wrapper class,no behaviour has been defined here String name,colour; } 当尝试使用以下代码从字符串中解析颜色时,会出现NumberFormatException: row.setBackgroundColor

编辑:由我的项目的相关部分组成的粘贴箱:

此外,colorEdItem是以下各项的包装:

     public class ColouredItem
     {//Only a wrapper class,no behaviour has been defined here
        String name,colour;
     }
当尝试使用以下代码从字符串中解析颜色时,会出现NumberFormatException:

     row.setBackgroundColor(Color.parseColor(item.colour));
我使用以下命令从资源创建项目列表:

    for(int i=0;i<list.length;i++)
    {
        item=new ColouredMenuItem();
        String[] cmenu =list[i].split("#");
        item.name=cmenu[0];
        item.colour="#"+cmenu[1];
        Log.d(TAG, item.colour);
        menuList.add(item);
    }
如一些答案所示,添加#并不能解决问题:

          java.lang.NumberFormatException: Invalid long: "#ffffff"
      at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2211)
      at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2261)
      at android.app.ActivityThread.access$600(ActivityThread.java:141)
      at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1256)
      at android.os.Handler.dispatchMessage(Handler.java:99)
      at android.os.Looper.loop(Looper.java:137)
      at android.app.ActivityThread.main(ActivityThread.java:5103)
      at java.lang.reflect.Method.invokeNative(Native Method)
这种实现也没有区别:

          String cmenu=list[i];
          item.name=cmenu.substring(0, cmenu.indexOf("#"));
          item.colour=cmenu.substring(cmenu.indexOf("#"));

尝试
Color.parseColor(#ffffff”)
而不是
Color.parseColor(“ffffff”)

查看堆栈跟踪,它将告诉您:

 java.lang.NumberFormatException: ffffff  
     at java.lang.Long.parse(Long.java:364)
     at java.lang.Long.parseLong(Long.java:354)
     at android.graphics.Color.parseColor(Color.java:207)
     at com.example.samplelistproject.MadAdapter.getView(MadAdapter.java:60)
逐行:

you are trying to format Hexadecimal (base 16) value "0xffffff" to a decimal (base 10) value
you're trying to parse hexadecimal string "ffffff" to type Long
same as above.
error is thrown when calling `Color.parseColor()`
error is thrown from your MadAdapter.java Class on line 60.
所以,您需要找到一种从十六进制而不是十进制值解析它的方法。十六进制值通常以0x[value]或#[value]开头

使用此代码

private List<String> item;

item = new ArrayList<String>();
item.add("#424242");
row.setBackgroundColor(Color.parseColor(item.get(0)));
try {
        Log.d(TAG, menuList.get(position).colour);
        textView.setText(menuList.get(position).getName());

        {
            row.setBackgroundColor(Color.parseColor(menuList.get(position).getColour()));
        }
    } catch (Exception ex) {
        Log.e(TAG, "Still does not work");
    }
row.setBackgroundColor(Color.parseColor(#424242”)

它也帮助了我,不要删除“#”

我用了这个密码

private List<String> item;

item = new ArrayList<String>();
item.add("#424242");
row.setBackgroundColor(Color.parseColor(item.get(0)));
try {
        Log.d(TAG, menuList.get(position).colour);
        textView.setText(menuList.get(position).getName());

        {
            row.setBackgroundColor(Color.parseColor(menuList.get(position).getColour()));
        }
    } catch (Exception ex) {
        Log.e(TAG, "Still does not work");
    }
试试看,它在我这边很管用

你的数组也是这样的

<string-array name="menu_array">
    <item>Page1 #ff7788</item>
    <item>Page1 #ff6688</item>
    <item>Page1 #424242</item>
</string-array>

第1页#ff7788
第1页#ff6688
第1页#4242

假设:在解析字符串中的颜色时,对象“item”不是从列表数组中获取的,而是它的实例变量coloureNuItem

    ColouredMenuItem item;
        ArrayList<ColouredMenuItem> menuList = new ArrayList<ColouredMenuItem>();
        String[] list = new String[]{"#ffffff","#00ffff"};





// parsing your string here, no change in this
for(int i=0;i<list.length;i++)
            {
                item=new ColouredMenuItem();
                String[] cmenu =list[i].split("#");
                item.name=cmenu[0];
                item.color="#"+cmenu[1];
                Log.d("colored", item.color);
                menuList.add(item);
            }

android提供了一种从字符串color解析颜色的复杂方法;我确实使用了int-Color.parseColor(string)当您可以使用item.Color=list[I]时,这个“item.Color=“#”+cmenu[1];”有什么用;我需要从一个更大的字符串中提取一种颜色,实际上list[I]返回了很多单词#ffffff
。你能给我们看一下coloredmenuItem.java文件吗?为了得到颜色值,我使用了类似name color的东西,并在#:
item.name=cmenu[0]处拆分它
item.color=“#”+cmenu[1]
logcat的前四行表示在解析后获得的颜色的字符串值,因为该字符串看起来像第1页#ffffff。您必须在颜色十六进制之前添加一个
“#”
。像“
ffffff”
必须是
“#ffffff”
。否则它不会工作。我已经发布了添加#的结果。我认为我做了一些简单的错误。我已经将整个代码发布在一个粘贴箱中…在帖子顶部编辑。是的,在粘贴箱中,我粘贴了一个logcat,其中字符串
#ffff45
在传递到
Color时返回值
-187
。如果使用“#”,则打印item.Color的值logcat的前四行看起来有点暗显,实际上就是这样做的<代码>字符串[]cmenu=list[i]。拆分(“#”);item.name=cmenu[0];item.Color=“#”+cmenu[1]首先尝试使用像我这样的硬代码值,如果它起作用,那么您的拆分可能会有问题。使用了一个硬编码值,工作了,但即使添加了#也不起作用。您是否使用try catch以便可以像
String[]list={“Page1#ffffff”,“Page2#ffffBB”一样捕获它的numberformatception链接而不是您拥有的变量。它和硬编码字符串一起工作,但在这里似乎不起作用。
    ColouredMenuItem item;
        ArrayList<ColouredMenuItem> menuList = new ArrayList<ColouredMenuItem>();
        String[] list = new String[]{"#ffffff","#00ffff"};





// parsing your string here, no change in this
for(int i=0;i<list.length;i++)
            {
                item=new ColouredMenuItem();
                String[] cmenu =list[i].split("#");
                item.name=cmenu[0];
                item.color="#"+cmenu[1];
                Log.d("colored", item.color);
                menuList.add(item);
            }
            for(int i=0;i<menuList.size();i++)
            {
                int color = Color.parseColor(menuList.get(i).color);
                Log.d("color",""+menuList.get(i).color);
            }
public class ColouredMenuItem {

    public String color;
    public String name;

}