Search code examples
androidcolorsnumberformatexception

Error parsing Colors from String


EDIT:A Pastebin consisting of the relevant parts of my project:

Here is the updated code

Also ColouredItem is a wrapper for:

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

I get a NumberFormatException when trying to parse a colour from a String using the following code:

     row.setBackgroundColor(Color.parseColor(item.colour));

I use the following to create a list of items from a resource:

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

This is the exception that I get...I have found that view.setBackgroundColor only takes an integer value:

         #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)

Adding the # as some of the answers suggest did not solve the issue:

          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)

No difference with this implementation either:

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

Solution

  • Use this code

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

    it helped me too,dont remove "#".

    i used this code

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

    and its working gud for me,may be your split thing is not working good

    or for your code

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

    this is working good at my side

    this is new code

    Make your colored item as a bean class with getter and setter like this

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

    }

    Then in your adapter use this code

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

    Just give it a try,it works here at my side

    Also your array is like this only na

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