agreeingly-ascyphous
agreeingly-ascyphous

Reputation: 691

SPAN_EXCLUSIVE_EXCLUSIVE spans cannot have a zero length error whenever an editText becomes empty

Test device: GSM galaxy nexus 4.2 (tested with both built in keyboard and swiftkey 3)

In my app I create a dialog to prompt the user for input. The dialog displays an EditText which the user is supposed to fill in a word in. Upon creation, as well as whenever the user erases everything from the EditText I get the following error message in logcat:

SPAN_EXCLUSIVE_EXCLUSIVE spans cannot have a zero length

Googling as well as searching here on stackOverflow tells me that the error can be avoided by using something like

inputBox.setInputType(InputType.TYPE_TEXT_FLAG_NO_SUGGESTIONS);

where inputBox is the EditText in question. I however, would like to keep auto complete functionality as the user is supposed to fill in words that will most likely be autocompletable. Here is the code used to create the dialog:

AlertDialog.Builder builder = new AlertDialog.Builder(this);
final EditText inputBox = new EditText(this);
inputBox.setHint("New keyword");
builder.setTitle(R.string.add_keyword_dialogue_header)
        .setView(inputBox)
        .setPositiveButton("Add",
            new DialogInterface.OnClickListener() {
                public void onClick(DialogInterface dialog, int id {
                            mListAdapter.addItem(inputBox.getText()
                                    .toString());
                        }
                    })
            .setNegativeButton("Cancel",
                new DialogInterface.OnClickListener() {
                    public void onClick(DialogInterface dialog,
                                int which) {
                        }
                    });
    final AlertDialog dialog = builder.create();
    dialog.getWindow().setSoftInputMode(
            WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_VISIBLE);
    dialog.show();
    inputBox.requestFocus();

My question is, how can I prevent the error from occuring while retaining auto complete functionality?

Upvotes: 1

Views: 6409

Answers (2)

Gaurav Arora
Gaurav Arora

Reputation: 8362

Just add this property to your edittext and your problem will definitely be resolved

android:inputType="textNoSuggestions"

Add the above property to each of your edit text used in the application, or simply where you get the error in that edit text only.

Upvotes: 1

Gabe Sechan
Gabe Sechan

Reputation: 93726

I've never seen that particular error before, but my guess is that its this line that causes it:

dialog.getWindow().setSoftInputMode(
        WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_VISIBLE);

That should open the soft keyboard even when the edit box isn't focused. So the input connection isn't correctly set up, leaving the keyboard with limited functionality. Setting the focus first might help it. So might using LayoutParams.SOFT_INPUT_STATE_VISIBLE without the always- it would prevent the keyboard from having a bad input connection due to not being tied to an editor.

Upvotes: 0

Related Questions