5

I have an app that gets the location address from geocoder it does the request inside a thread. When getActivity().runOnUiThread is called I already have the address response from geocoder inside a string variable and I check it to see if it is empty and start assign it's value to fields in the view:

  getActivity().runOnUiThread(new Runnable() {
                            @Override
                            public void run() {
                                if (isAdded())
                                    if (!TextUtils.isEmpty(strAddress)){
                                        et.setFocusable(false);
                                        et.setFocusableInTouchMode(false);
                                        et.setText(strAddress);
                                        et.setTextColor(getResources().getColor(
                                                android.R.color.black));
                                        et.setFocusable(true);
                                        et.setFocusableInTouchMode(true);
                                    } else {
                                        et.setTextColor(getResources().getColor(
                                                android.R.color.black));
                                    }
                                etSource.setEnabled(true);
                            }
                        });

on this line:

et.setText(strAddress);

The error

ArrayIndexOutOfBounds: set span -1

is throw and it happens intermittently, why is this happening ?

Edit as requested

  java.lang.IndexOutOfBoundsException: setSpan (-1 ... -1) starts before 0
                                                                         at android.text.SpannableStringBuilder.checkRange(SpannableStringBuilder.java:1270)
                                                                         at android.text.SpannableStringBuilder.setSpan(SpannableStringBuilder.java:684)
                                                                         at android.text.SpannableStringBuilder.setSpan(SpannableStringBuilder.java:677)
                                                                         at android.text.Selection.setSelection(Selection.java:76)
                                                                         at android.widget.Editor$SelectionModifierCursorController.resetDragAcceleratorState(Editor.java:5689)
                                                                         at android.widget.Editor$SelectionModifierCursorController.resetTouchOffsets(Editor.java:5679)
                                                                         at android.widget.Editor.sendOnTextChanged(Editor.java:1280)
                                                                         at android.widget.TextView.sendOnTextChanged(TextView.java:8220)
                                                                         at android.widget.TextView.setText(TextView.java:4494)
                                                                         at android.widget.TextView.setText(TextView.java:4348)
                                                                         at android.widget.EditText.setText(EditText.java:89)
                                                                         at android.widget.TextView.setText(TextView.java:4323)
                                                                         at library.fragments.MapFragment$17$1.run(MapFragment.java:2156)
                                                                         at android.os.Handler.handleCallback(Handler.java:751)
                                                                         at android.os.Handler.dispatchMessage(Handler.java:95)
                                                                         at android.os.Looper.loop(Looper.java:159)
                                                                         at android.app.ActivityThread.main(ActivityThread.java:6097)
                                                                         at java.lang.reflect.Method.invoke(Native Method)
                                                                         at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:865)
                                                                         at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:755)

as Requested

This is how I assign value to string Variable, sorry for delay...

 private void getAddressFromLocation(final LatLng latlng, final EditText et) {
        et.setText(getString(R.string.text_waiting_for_address));
        et.setTextColor(Color.GRAY);
        new Thread(new Runnable() {
            @Override
            public void run() {
                Geocoder gCoder = new Geocoder(getActivity(),Locale.ENGLISH);
                try {
                    final List<Address> list = gCoder.getFromLocation(
                            latlng.latitude, latlng.longitude, 1);
                    if (list != null && list.size() > 0) {
                        address = list.get(0);
                        StringBuilder sb = new StringBuilder();
                        Log.d("Endereço com cidade",address.toString());
                        preference.putCidade(address.getLocality());
                        preference.putEstado(address.getAdminArea().replace("State of ",""));
                        if (address.getAddressLine(0) != null) {
                            if (address.getMaxAddressLineIndex() > 0) {
                                for (int i = 0; i < address
                                        .getMaxAddressLineIndex(); i++) {
                                    sb.append(address.getAddressLine(i))
                                            .append("\n");
                                }
                                sb.append(",");
                                sb.append(address.getCountryName());
                            } else {
                                sb.append(address.getAddressLine(0));
                            }
                        }


                        strAddress = sb.toString();
                        strAddress = strAddress.replace(",null", "");
                        strAddress = strAddress.replace("null", "");
                        strAddress = strAddress.replace("Unnamed", "");
                    }
                    if (getActivity() == null)
                        return;

0 Answers0