EDIT:A Pastebin consisting of the relevant parts of my project:
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("#"));
Try
Color.parseColor("#ffffff");
instead ofColor.parseColor("ffffff");
look at the Stack trace it will tell you:
Line by Line:
so, you need to find a way to parse it from Hexadecimal instead of decimal value. Hexadecimal values are usually preceeded by 0x[value] OR #[value]
Use this code
row.setBackgroundColor(Color.parseColor("#424242"));
it helped me too,dont remove "#".
i used this code
and its working gud for me,may be your split thing is not working good
or for your code
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
}
Then in your adapter use this code
Just give it a try,it works here at my side
Also your array is like this only na
Assuming: while parsing the color from string object "item" is not taken from array of list, rather its taking instance variable of ColoureMenuItem.
// confirming whether value are parsing or not.
and your ColouredMenuItem class.