【问题标题】:Error parsing Colors from String从字符串解析颜色时出错
【发布时间】:2013-10-23 13:07:35
【问题描述】:

编辑:由我项目的相关部分组成的 Pastebin:

Here is the updated code

ColouredItem 也是一个包装器:

     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);
    }

这是我得到的例外......我发现 view.setBackgroundColor 只需要一个整数值:

         #ffffff 
         #ffffBB 
         #fff45f 
         #ffff00 
         Shutting down VM
         threadid=1: thread exiting with uncaught exception (group=0x4001d800)
         FATAL EXCEPTION: main
             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)
         at android.widget.AbsListView.obtainView(AbsListView.java:1315)
         at android.widget.ListView.makeAndAddView(ListView.java:1727)
         at android.widget.ListView.fillDown(ListView.java:652)
         at android.widget.ListView.fillFromTop(ListView.java:709)
         at android.widget.ListView.layoutChildren(ListView.java:1580)
         at android.widget.AbsListView.onLayout(AbsListView.java:1147)
         at android.view.View.layout(View.java:7035)
         at android.widget.FrameLayout.onLayout(FrameLayout.java:333)
         at android.view.View.layout(View.java:7035)
         at android.widget.LinearLayout.setChildFrame(LinearLayout.java:1249)
         at android.widget.LinearLayout.layoutVertical(LinearLayout.java:1125)
         at android.widget.LinearLayout.onLayout(LinearLayout.java:1042)
         at android.view.View.layout(View.java:7035)
         at android.widget.FrameLayout.onLayout(FrameLayout.java:333)
         at android.view.View.layout(View.java:7035)
         at android.view.ViewRoot.performTraversals(ViewRoot.java:1045)
         at android.view.ViewRoot.handleMessage(ViewRoot.java:1727)
         at android.os.Handler.dispatchMessage(Handler.java:99)
         at android.os.Looper.loop(Looper.java:123)
         at android.app.ActivityThread.main(ActivityThread.java:4627)
         at java.lang.reflect.Method.invokeNative(Native Method)
         at java.lang.reflect.Method.invoke(Method.java:521)
         at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868)
         at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)
         at dalvik.system.NativeStart.main(Native Method)

按照某些答案的建议添加 # 并不能解决问题:

          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("#"));

【问题讨论】:

  • android 提供了一个复杂的方法来从字符串 Color.parseColor("#ffffff") 中解析颜色,你试过了吗?
  • 我确实用过 int Color.parseColor(string)
  • 这个"item.colour="#"+cmenu[1];"有什么用什么时候可以使用 item.colour=list[i];
  • 我需要从较大的字符串中提取颜色,实际上list[i]返回a lot of words #ffffff
  • 你能给我们看看 ColouredMenuItem.java 文件吗?

标签: android colors numberformatexception


【解决方案1】:

使用此代码

row.setBackgroundColor(Color.parseColor("#424242"));

它对我也有帮助,不要删除“#”。

我使用了这个代码

private List<String> item;

item = new ArrayList<String>();
item.add("#424242");
row.setBackgroundColor(Color.parseColor(item.get(0)));

对我来说它的工作原理,可能是你的分裂的东西不好用

或为您的代码

Button btn;
ColouredMenuItem item;
ArrayList<ColouredMenuItem> menuList = new ArrayList<ColouredMenuItem>();
String[] list = new String[] { "Page1 #ffffff", "Page2 #ffffBB" };

@Override
protected void onCreate(Bundle savedInstanceState) {
    // TODO Auto-generated method stub
    super.onCreate(savedInstanceState);

    setContentView(R.layout.sample);

    try {
        btn = (Button) findViewById(R.id.button1);
        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);
        }



        btn.setBackgroundColor(Color.parseColor(menuList.get(1).color));
    } catch (Exception e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
}

这对我来说很好用

这是新代码

使用像这样的 getter 和 setter 将您的彩色项目作为 bean 类

public class ColouredMenuItem {// Only a wrapper class,no behaviour has been defined
                        // here
String name, colour;

List<ColouredMenuItem> list=new ArrayList<ColouredMenuItem>();

public List<ColouredMenuItem> getList() {
    return list;
}

public void setList(List<ColouredMenuItem> menuList) {
    this.list = menuList;
}

public String getName() {
    return name;
}

public void setName(String name) {
    this.name = name;
}

public String getColour() {
    return colour;
}

public void setColour(String colour) {
    this.colour = colour;
}

}

然后在您的适配器中使用此代码

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");
    }

试一试,它就在我身边

你的数组也只是这样的 na

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

【讨论】:

  • 如果你使用“#”这个,那么打印item.colour的值
  • logcat 的前四行看起来有点暗淡,实际上就是这样做的。 String[] cmenu =list[i].split("#"); item.name=cmenu[0]; item.colour="#"+cmenu[1];
  • 先尝试像我这样的硬编码值,如果它正常,那么您的拆分可能有问题。
  • 使用硬编码的值,有效,但即使添加 # 也不起作用。
  • 您是否使用 try-catch 以便捕获 NumberFormatException
【解决方案2】:

尝试Color.parseColor("#ffffff"); 而不是Color.parseColor("ffffff");

【讨论】:

  • 为了得到颜色值,我取了name color之类的东西,并在#处拆分:item.name=cmenu[0];item.colour="#"+cmenu[1]; logcat的前四行代表解析后得到的颜色字符串值
  • 因为字符串看起来像第 1 页 #ffffff
  • 您必须在颜色十六进制之前添加"#"。比如“ffffff"必须是"#ffffff"。否则它不会工作。
  • 我已经发布了添加 # 的结果。我认为我做错了一些简单的事情。我已经将整个代码发布在一个 pastebin 中...在帖子顶部编辑。
  • 是的,他们在 pastebin 中做了我已经粘贴了一个 logcat,其中字符串 #ffff45 在传递给 Color.parseColor 时返回 -187 的值
【解决方案3】:

看看它会告诉你的堆栈跟踪:

 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]

开头

【讨论】:

    【解决方案4】:

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

        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);
                }
    

    还有你的 ColouredMenuItem 类。

    public class ColouredMenuItem {
    
        public String color;
        public String name;
    
    }
    

    【讨论】:

    • 把它想象成String[] list= {"Page1 #ffffff","Page2 #ffffBB"};,而不是你拥有的变量。它适用于硬编码的字符串,但它似乎在这里不起作用。
    猜你喜欢
    • 2018-03-14
    • 2017-07-17
    • 2017-07-08
    • 2017-09-26
    • 1970-01-01
    • 2012-10-04
    • 2021-11-16
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多