2
votes

When I try to place a String variable, with hexadecimal color inside to set the setbackgroundcolor(Color.parseColor(String s)) of a edittext on my adapter class, it says

StringIndexOutofBoundsException, length = 0, index = 0.

When I insert the String inside the parseColor by hand, for example parseColor("#ffffff");, It works!

My Adapter Class:

public class TasksAdapter extends ArrayAdapter<Tasks> {
    private Context sContext;
    private List<Tasks> taskData = new ArrayList<>();
    public TasksAdapter(@NonNull Context context, @SuppressLint("SupportAnnotationUsage") @LayoutRes ArrayList<Tasks> list){
        super(context, 0, list);
        sContext = context;
        taskData = list;
    }
    @NonNull
    @Override
    public View getView(int position, @Nullable View convertView, @NonNull ViewGroup parent){
        View listItem = convertView;
        if(listItem == null)
            listItem = LayoutInflater.from(sContext).inflate(R.layout.item_tasks, parent,false);
        final Tasks presenteTask = taskData.get(position);
        TextView taskTitle = (TextView) listItem.findViewById(R.id.tasksTitle);
        taskTitle.setText(presenteTask.getTitle());
        EditText taskColor = (EditText) listItem.findViewById(R.id.taskColor);
        taskColor.setBackgroundColor(Color.parseColor(presenteTask.getHexaColor()));
        return listItem;
    }
}

The error:

E/AndroidRuntime: FATAL EXCEPTION: main Process: com.example.mylcm, PID: 672 java.lang.StringIndexOutOfBoundsException: length=0; index=0 at java.lang.String.charAt(Native Method) at android.graphics.Color.parseColor(Color.java:1384) at com.example.mylcm.Utils.Adapters.TasksAdapter.getView(TasksAdapter.java:49) at android.widget.AbsListView.obtainView(AbsListView.java:2365) at android.widget.ListView.makeAndAddView(ListView.java:2052) at android.widget.ListView.fillDown(ListView.java:786) at android.widget.ListView.fillFromTop(ListView.java:847) at android.widget.ListView.layoutChildren(ListView.java:1826) at android.widget.AbsListView.onLayout(AbsListView.java:2164) at android.view.View.layout(View.java:19659) at android.view.ViewGroup.layout(ViewGroup.java:6075) at android.widget.LinearLayout.setChildFrame(LinearLayout.java:1791) at android.widget.LinearLayout.layoutHorizontal(LinearLayout.java:1780) at android.widget.LinearLayout.onLayout(LinearLayout.java:1546) at android.view.View.layout(View.java:19659) at android.view.ViewGroup.layout(ViewGroup.java:6075) at android.widget.RelativeLayout.onLayout(RelativeLayout.java:1083) at android.view.View.layout(View.java:19659) at android.view.ViewGroup.layout(ViewGroup.java:6075) at android.widget.FrameLayout.layoutChildren(FrameLayout.java:323) at android.widget.FrameLayout.onLayout(FrameLayout.java:261) at android.view.View.layout(View.java:19659) at android.view.ViewGroup.layout(ViewGroup.java:6075) at android.widget.LinearLayout.setChildFrame(LinearLayout.java:1791) at android.widget.LinearLayout.layoutVertical(LinearLayout.java:1635) at android.widget.LinearLayout.onLayout(LinearLayout.java:1544) at android.view.View.layout(View.java:19659) at android.view.ViewGroup.layout(ViewGroup.java:6075) at android.widget.FrameLayout.layoutChildren(FrameLayout.java:323) at android.widget.FrameLayout.onLayout(FrameLayout.java:261) at android.view.View.layout(View.java:19659) at android.view.ViewGroup.layout(ViewGroup.java:6075) at android.widget.LinearLayout.setChildFrame(LinearLayout.java:1791) at android.widget.LinearLayout.layoutVertical(LinearLayout.java:1635) at android.widget.LinearLayout.onLayout(LinearLayout.java:1544) at android.view.View.layout(View.java:19659) at android.view.ViewGroup.layout(ViewGroup.java:6075) at android.widget.FrameLayout.layoutChildren(FrameLayout.java:323) at android.widget.FrameLayout.onLayout(FrameLayout.java:261) at com.android.internal.policy.DecorView.onLayout(DecorView.java:761) at android.view.View.layout(View.java:19659) at android.view.ViewGroup.layout(ViewGroup.java:6075) at android.view.ViewRootImpl.performLayout(ViewRootImpl.java:2496) at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:2212) at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:1392) at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:6752) at android.view.Choreographer$CallbackRecord.run(Choreographer.java:911) at android.view.Choreographer.doCallbacks(Choreographer.java:723) at android.view.Choreographer.doFrame(Choreographer.java:658) at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:897) at android.os.Handler.handleCallback(Handler.java:790) at android.os.Handler.dispatchMessage(Handler.java:99) at android.os.Looper.loop(Looper.java:164) at android.app.ActivityThread.main(ActivityThread.java:6494) at java.lang.reflect.Method.invoke(Native Method) at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:438) at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:807)

2
I would guess that presenteTask.getHexaColor() returns an empty string. Tried debugging it, like: tracing the value that gets passed into the method?GhostCat
@GhostCat I tried debugging it, and the value that i see there is the correct string... i don't know why it still pass me that errorVitorlsa
Are you sure you went through every item in the adapter? It's definitely trying to parse a string that is empty. Maybe add log output so you don't have to step through every single item.dymmeh
Are you setting breakpoints inside getView()? You should verify the Task item right before the parse call.Jarvis
Pull the presenteTask.getHexaColor() out of the parseColor call and set the returned value. Then set a conditional breakpoint or add an if statement, to check the String contents, for debugging. It will be easier to see if every iteration is returning a valid string.Reger05

2 Answers

1
votes

Inside getView() of TaskAdapter one of your taskData item return empty hexaColor during execution of presenteTask.getHexaColor(). Try below code, it will stop your application being force closed and you will get the position of item causing exception inside catch.

public View getView(int position, @Nullable View convertView, @NonNull ViewGroup parent){
    View listItem = convertView;
    if(listItem == null)
        listItem = LayoutInflater.from(sContext).inflate(R.layout.item_tasks, parent,false);

    try {
        final Tasks presenteTask = taskData.get(position);
        TextView taskTitle = (TextView) listItem.findViewById(R.id.tasksTitle);
        taskTitle.setText(presenteTask.getTitle());
        EditText taskColor = (EditText) listItem.findViewById(R.id.taskColor);
        taskColor.setBackgroundColor(Color.parseColor(presenteTask.getHexaColor()));
    } catch(Exception ex) {
        Log.v(TasksAdapter.class.getSimpleName(), "Exception at: " + position);
    }

    return listItem;
}
0
votes

I suspect that this is your problem

taskColor.setBackgroundColor(Color.parseColor(presenteTask.getHexaColor()));

Not sure what is presenteTask.getHexaColor() returning, but as I read from your comments, you say that is a valid string. You need to specify exactly what is the format of your color string.

For example three digit color strings are not supported by Color.parseColor(). You can look at Android Platform API documentation and read about the Color class. Another information from the documentation:

Supported formats are: #RRGGBB #AARRGGBB

Make sure you use the correct color string format.