207

I have the following layout (virtually empty):

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/set_layout"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:contentDescription="content desc"
    android:orientation="vertical" >

    <TextView android:id="@+id/text"
          android:layout_width="wrap_content"
          android:layout_height="wrap_content"
          android:text="Hello, I am a TextView" />
</LinearLayout>

The Activity class contains the following:

public class TestActivity extends Activity {
  public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_test);   
  }
}

When I run this on my mobile device I get the following error:

SpannableStringBuilder
SPAN_EXCLUSIVE_EXCLUSIVE spans cannot have a zero length

I have tried this with and without the TextView and the error still remains, I must be doing something fundamentally wrong for such a basic layout to cause this.

Does anyone have any ideas on how I can get this to load without the error?

Adinia
  • 3,722
  • 5
  • 40
  • 58
DMac
  • 2,103
  • 2
  • 14
  • 7
  • Examine LogCat and look at the stack trace associated with your error. If it indeed is from this app, post the entire stack trace as an edit to your question. Also, I would get rid of `android:contentDescription` from `LinearLayout`, as that container is not focusable and therefore the description will not be used AFAIK. – CommonsWare Dec 02 '12 at 21:37
  • 9
    Thanks for your response. Unfortunately there is no stack trace for this error, the output is exactly as i have posted above, but the application field in LogCat does point to my application. I have debugged the code and stepped through each line with no error being thrown, so it must be somewhere in the Android code that is expecting something that isn't there. – DMac Dec 03 '12 at 10:43
  • I would like to know how to avoid this annoying error too, without removing/disabling any feature for the end user. – android developer Apr 20 '13 at 10:46
  • 1
    I reset the phone to factory settings and it went away, after trying the 'samsung keyboard fix' which didn't seem to do the trick for me... – Shark Sep 05 '13 at 15:42
  • Perhaps it isn't a problem at all, but after upgrading to Android 4.1.1 I'm seeing a lot of errors in the logs like this one. SPAN_EXCLUSIVE_EXCLUSIVE spans cannot have a zero length – Sagar Raiyani Sep 06 '13 at 11:25
  • I only get this error on a Pixel running Android P so it might be something Google still needs to iron out. – Esdras Lopez Apr 26 '18 at 11:14

20 Answers20

253

I have run into the same error entries in LogCat. In my case it's caused by the 3rd party keyboard I am using. When I change it back to Android keyboard, the error entry does not show up any more.

Jack W
  • 2,637
  • 1
  • 14
  • 7
  • 3
    thanks @Rabi, even i switched to swiftkey and started getting the errror. – Maulik Sheth Sep 04 '13 at 11:50
  • 2
    I get the error while using stock Android keyboard on a Nexus 7 (2012 edition, running 4.3) – Tom Nov 07 '13 at 21:06
  • (On Galaxy S4, 4.4.2) -- Switching back to the stock samsung keyboard fixed this for me. Thank you! – FateNuller Sep 13 '14 at 19:28
  • 7
    Unfortunately the link from @Rabi above isn't good anymore. But thank you for this. I am indeed using SwiftKey and it appears to still be causing this issue 2 years later. – Splaktar Nov 22 '14 at 22:28
  • There isn't event an `EditText`-view in the layout shown in the question, so I don't get how can it be related to input and the keyboard. I believe the problem is caused by some layout-related bug in Samsung's TouchWiz, because I'm getting the same error as in the question when I test my app on my Samsung device, and any `TextView` is updated, due to screen rotation or something alike. I would be happy if the error could go away, but until I find a solution, I guess I will live with it... – Daniel Kvist Jun 30 '15 at 11:12
  • 3
    This issue appears with Gboard(!) as well (and it's from Google) – techfly Sep 17 '17 at 14:36
117

Because the error you're getting is not related to an EditText, then it's not related to your keyboard.

The errors you are getting are not a result of your code; you probably are testing on a Samsung device that has Samsung's TouchWiz.

I had the same errors, then I tested on a Nexus S (also by Samsung, but pure Android OS without TouchWiz) and I didn't get this error.

So, in your case, just ignore these errors while testing on a device! :)

mmBs
  • 8,421
  • 6
  • 38
  • 46
Karim
  • 5,298
  • 3
  • 29
  • 35
  • 2
    It is your keyboard, probably the keyboard of TouchWiz in your case. I have the same errors on a Nexus 4 with Swiftkey, if I choose the default Android keyboard the errors are gone. – Dante May 06 '13 at 23:46
  • Keyboards can definitely lead to this error, but in the case where the keyboard is not used or called, i.e. in the case where there are no EditText's, I don't see how the keyboard can be the source of the problem. When I switch between two different activities, both without EditText, I keep getting the error every time I go to one of these activities. – Karim May 07 '13 at 03:48
  • 2
    I agree with Cookiki, the errors are because of Samsung TouchWiz, and its not causing any problems other than spewing errors in logcat. So will be ignoring them – user2566490 Aug 02 '13 at 18:39
  • We can't just ignore errors because it only happens on a TouchWiz, the least is reporting to samsung that there's a problem with touchwiz. – Sojurn Oct 31 '13 at 06:17
  • 1
    I can most certainly occur without it being related to an EditText. I had this error when displaying a PreferenceFragment with some headers - no textboxes or anything of the sort. On every change from one PreferenceFragment I got two of these error messages. Switched from "Samsung Keyboard" to "Smart Keyboard Pro" and the error messages stopped. – Magnus Mar 15 '14 at 11:49
  • The error **still appears** when running **Cyanogenmod 11** on a samsung device _(Samsung Galaxy S3)_ – ymerdrengene May 11 '14 at 17:54
  • Same problem here, only on the Samsung devices we have. – Brian Knoblauch May 13 '14 at 15:00
  • same issue here in 10 inch acer tablet,In samsung 7 inch tab working fine. – NagarjunaReddy Jul 11 '14 at 13:59
  • same problem on Huawei P30 Lite – zeroDivider Jun 14 '23 at 10:01
55

Looking at your code, I'm not sure why you're getting that error, but I had this same error but with EditText fields.

Changing android:inputType="text" (or any of the other inputType text variations) to android:inputType="textNoSuggestions" (or android:inputType="textEmailAddress|textNoSuggestions", for example) fixed it for me.

You can also set this in Code with something like

mInputField.setInputType(InputType.TYPE_CLASS_TEXT | InputType.TYPE_TEXT_FLAG_NO_SUGGESTIONS);

Looks like Android assumes by default that EditText fields will have suggestions. When they don't, it errors. Not 100% confident in that explanation, but the above mentioned changes fixed it for me.

http://developer.android.com/reference/android/text/Spanned.html#SPAN_EXCLUSIVE_EXCLUSIVE

Hope this helps!

tim
  • 1,708
  • 14
  • 16
  • Thanks for this. But even with the TextView removed and only the layout present i get the error. It is very strange. – DMac Dec 04 '12 at 11:41
  • Removing the contentDescription from the LinearLayout doesn't help? – tim Dec 06 '12 at 21:16
  • 3
    No unfortunately not. Although with further investigation the error does only seem to display on Samsung Devices. – DMac Dec 08 '12 at 09:51
  • I can confirm that it doesn't fix in Samsung Galaxy A51 w/ Android 11. But your explanation sounds really reasonable. – Samuel T. Chou Mar 02 '22 at 11:06
13

On your android phone go to:
settings -> application manager -> all -> samsung keyboard and then click on "clear cache"
(delete all data collected by this application).

Celebes
  • 1,371
  • 1
  • 12
  • 21
10

Try using the default Android keyboard it will disappear

Yasin Hassanien
  • 4,055
  • 1
  • 21
  • 17
3

Make clear you have pass a value in your MainAcitivity for the following methods onCreateOptionsMenu and onCreate

In some cases, the developer deletes the "return super.onCreateOptionsMenu(menu)" statement and changed to "return true".

Sagar Maiyad
  • 12,655
  • 9
  • 63
  • 99
Masood Moshref
  • 370
  • 4
  • 9
2

This worked for me...on every device

    <EditText
        android:maxLines="1"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:textSize="15sp"
        android:layout_centerVertical="true"
        android:textColor="#000"
        android:id="@+id/input_search"
        android:background="@null"
        android:inputType="text"
        android:hint="Enter Address, City or Zip Code"
        android:imeOptions="actionSearch"
        />

In Java code:

mSearchText.setOnEditorActionListener(new TextView.OnEditorActionListener() {

        @Override
        public boolean onEditorAction(TextView textView, int actionId, KeyEvent keyEvent) {
            if(actionId == EditorInfo.IME_ACTION_SEARCH
                    || actionId == EditorInfo.IME_ACTION_DONE
                    || keyEvent.getAction() == KeyEvent.ACTION_DOWN
                    || keyEvent.getAction() == KeyEvent.KEYCODE_ENTER){

                //execute our method for searching
            }

            return false;
        }
    });
Suraj Vaishnav
  • 7,777
  • 4
  • 43
  • 46
0

I had the same warning and found that removing an unused @id got rid of the warning. For me it was obvious as the @id was associated with a growing list of textViews linked to a database, so there was a warning for each entry.

k.e.n.
  • 47
  • 8
0

Masood Moshref is right, this error occur because the option menu of Menu is not well prepared by lacking "return super.onCreateOptionsMenu(menu)" in onCreate() method.

Quân Lê
  • 9
  • 2
0

To try to debug this error, first go to your android terminal / console and execute this command:

ps | grep THE_ERROR_PID_YOU_GET_(IT_IS_A_NUMBER)

then if the output comes out as your app... it is your app causing the error. Try to look for empty Strings that you pass into the layout.

I had this exact same problem and it was my fault as I was passing an empty String into my layout. After changing the "" to " " this error went away.

If you don't get your app from the console output, then it is something else causing it (probably, as others said, the android keyboard)

dazito
  • 7,740
  • 15
  • 75
  • 117
-1

I have faced the same issue. I almost wasted almost couple of weeks to resolved this issue. Finally I had on doubt on myself and tried to create another project by copy and paste some startup files like SplashScreen & LoginScreen.

But with the same code still i was getting SPAN_EXCLUSIVE_EXCLUSIVE.

Then i have removed the handler code from splash screen and tried again and Wow its working. I am not getting SPAN_EXCLUSIVE_EXCLUSIVE issue in logcat.

I wondering, why it is? till the time did not get any other solution but by removing handler from splash screen it is working.

Try and update here if it is resolved or not.

Sachin Shelke
  • 449
  • 4
  • 12
-1

Check if you have any element such as button or text view duplicated (copied twice) in the screen where this encounters. I did this unnoticed and had to face the same issue.

Dibyendu Mitra Roy
  • 1,604
  • 22
  • 20
-1

I ran into this problem too when I copied some text from the Internet. My solution is to trim the text/remove formatting before doing any further processing.

Dũng Trần Trung
  • 6,198
  • 3
  • 24
  • 20
-1

I had the same problem but with a listView.... i solved it because i was using a wrong R.id.listView that list View needed to have a value, in my case it was strings that i saved on listView2... so the right code was R.id.listView2

Andrey Solera
  • 2,311
  • 2
  • 26
  • 51
-1

I had the same problem then i fixed it by following code!

  text = (EditText)findViewById(R.id.TextVoiceeditText); 
  text.setInputType(InputType.TYPE_CLASS_TEXT|InputType.TYPE_TEXT_FLAG_NO_SUGGESTIONS);
Shahzad Afridi
  • 2,058
  • 26
  • 24
-1

this error also occurs due to changed API URL. try hitting the URL you are using in postman and c if it's working properly. rechecking the APIs solved my problem

-1

try avoiding use of view in xml design.I too had the same probem but when I removed the view. its worked perfectly.

like example:

             <EditText
                android:layout_width="match_parent"
                android:layout_height="wrap_content"                   
                android:hint="Username"
                android:inputType="number"                   
                android:textColor="#fff" />

            <view
                android:layout_width="match_parent"
                android:layout_height="1dp"
                android:background="#f9d7db" />

also check and try changing by trial and error android:inputType="number" to android:inputType="text" or better not using it if not required .Sometimes keyboard stuck and gets error in some of the devices.

Eshan Chattaraj
  • 368
  • 1
  • 6
  • 19
-1

In my case, the EditText fields with inputType as text / textCapCharacters were casing this error. I noticed this in my logcat whenever I used backspace to completely remove the text typed in any of these fields.

The solution which worked for me was to change the inputType of those fields to textNoSuggestions as this was the most suited type and didn't give me any unwanted errors anymore.

Ankush Chauhan
  • 1,433
  • 2
  • 17
  • 22
-1

in my case i click on recent apps shortcut on my cell phone and close all apps. This solution always work for me, because this error not related to code.

Amir Ehsani
  • 139
  • 1
  • 10
-4

To solve this problem just add android:usesCleartextTraffic="true" in your AndroidManifest.xml file which is at ..\app\src\main\AndroidManifest.xml just like bellow...

enter image description here