Reputation: 109
I am developing a contact displaying app which have different colored circle in the list. I have a list of dummy data that will be recycled into the view. But my problem here is that the error java.lang.IllegalArgumentException: Unknown color pops up in logcat when I try to run the app. Below are the codes:
DataManager.java:
import android.graphics.Color;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;
public class DataManager extends RecyclerView.Adapter<DataManager.RecyclerViewHolder> {
public static class RecyclerViewHolder extends RecyclerView.ViewHolder {
TextView mName, mPhone;
View mCircle;
RecyclerViewHolder(View itemView) {
super(itemView);
mName = (TextView) itemView.findViewById(R.id.CONTACT_name);
mPhone = (TextView) itemView.findViewById(R.id.CONTACT_phone);
mCircle = itemView.findViewById(R.id.CONTACT_circle);
}
}
@Override
public RecyclerViewHolder onCreateViewHolder(ViewGroup viewGroup, int i) {
View v = LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.contact_item, viewGroup, false);
return new RecyclerViewHolder(v);
}
@Override
public void onBindViewHolder(RecyclerViewHolder viewHolder, int i) {
// get the single element from the main array
final Contact contact = Contact.CONTACTS[i];
// Set the values
viewHolder.mName.setText(contact.get(Contact.Field.NAME));
viewHolder.mPhone.setText(contact.get(Contact.Field.PHONE));
// Set the color of the shape
// GradientDrawable bgShape = (GradientDrawable) viewHolder.mCircle.getBackground();
// bgShape.setColor(Color.parseColor(contact.get(Contact.Field.COLOR)));
viewHolder.mCircle.setBackgroundColor(Color.parseColor(contact.get(Contact.Field.COLOR)));
}
@Override
public int getItemCount() {
return Contact.CONTACTS.length;
}
}
Logs:
10-28 05:34:38.575 2567-2567/com.example.fadil.contactsmation E/AndroidRuntime: java.lang.IllegalArgumentException: Unknown color
10-28 05:34:38.575 2567-2567/com.example.fadil.contactsmation E/AndroidRuntime: at android.graphics.Color.parseColor(Color.java:225)
10-28 05:34:38.575 2567-2567/com.example.fadil.contactsmation E/AndroidRuntime: at com.example.fadil.contactsmation.DataManager.onBindViewHolder(DataManager.java:41)
10-28 05:34:38.575 2567-2567/com.example.fadil.contactsmation E/AndroidRuntime: at com.example.fadil.contactsmation.DataManager.onBindViewHolder(DataManager.java:10)
line 41 is the code : viewHolder.mCircle.setBackgroundColor(Color.parseColor(contact.get(Contact.Field.COLOR)));
line 10 is : public class DataManager extends RecyclerView.Adapter<DataManager.RecyclerViewHolder>
DummyData:
public class Contact {
public static final Contact[] CONTACTS = new Contact[]{
new Contact("John", "#33b5e5", "+01 123456789", "[email protected]", "Venice"),
new Contact("Valter", "#ffbb33", "+01 987654321", "[email protected]", "Bologna"),
new Contact("Eadwine", "#ff4444", "+01 123456789", "[email protected]", "Verona"),
new Contact("Teddy", "#99cc00", "+01 987654321", "[email protected]", "Rome"),
new Contact("Ives", "#33b5e5", "+01 11235813", "[email protected]", "Milan"),
new Contact("Alajos", "#ffbb33", "+01 123456789", "[email protected]", "Bologna"),
new Contact("Gianluca", "#ff4444", "+01 11235813", "[email protected]", "Padova"),
new Contact("Fane", "#99cc00", "+01 987654321", "[email protected]", "Venice"),
};
// The fields associated to the person
private final String mName, mPhone, mEmail, mCity, mColor;
Contact(String name, String phone, String color, String email, String city){
mName = name;
mPhone = phone;
mColor = color;
mEmail = email;
mCity = city;
}
// This method allows to get the item associated to a particular id,
// uniquely generated by the method getId defined below
public static Contact getItem(int id){
for(Contact item : CONTACTS){
if(item.getId() == id){
return item;
}
}
return null;
}
// since mName and mPhone combined are surely unique,
// we don't need to add another id field
public int getId(){
return mName.hashCode() + mPhone.hashCode();
}
public static enum Field {
NAME, COLOR, PHONE, EMAIL, CITY
}
public String get(Field f){
switch (f){
case COLOR: return mColor;
case PHONE: return mPhone;
case EMAIL: return mEmail;
case CITY: return mCity;
case NAME: default: return mName;
}
}
}
Upvotes: 0
Views: 3124
Reputation: 1026
The error is in the constructor: Contact(String name, String phone, String color, ... The second argument is the phone, the third is the color. When you are creating your dummy data, you pass the color as the second argument. So in the bindView method, you are actually passing the phone number instead of the color.
Upvotes: 2