EditText.setText(String)抛出数组越界

时间:2017-05-04 17:37:49

标签: java android multithreading google-geocoder

我有一个应用程序从地理编码器获取位置地址,它在一个线程内执行请求。  当调用getActivity()。runOnUiThread时,我已经从字符串变量中的地理编码器获得地址响应,并检查它是否为空并开始将其值分配给视图中的字段:

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

在这一行:

et.setText(strAddress);

错误

ArrayIndexOutOfBounds:set span -1

抛出是间歇性的,为什么会发生这种情况?

按要求编辑

  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

这是我为字符串变量赋值的方法,对不起延迟......

 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 个答案:

没有答案