Android Program is Crashing - Don't Know Why
<p>I have a list that will filter in input in an EditText field and populate the ListView according to what they type.</p>

<p>For example, if the user types "al" it will populate names that have a first or last name beginning with "al."</p>

<p>My problem is when the user backspaces the EditText field all the way back where no letters are typed, my program crashes. I'm not sure why my initial if statement isn't getting used.</p>

<p>This is NOT an issue of the ListView being empty causing a crash, because if I type a name that isn't in the list, the ListView will stay empty and not crash.</p>

<pre><code>private void AlterAdapter() {
    partialNames.clear();

    if (searchName.getText().toString() == "")
        partialNames.add("There are no names that meet your criteria");
    else  {
            String seekString = searchName.getText().toString();
            String firstLetter = seekString.substring(0,1).toString().toUpperCase();
            Log.d("JAY", "The firstLetter is: " + firstLetter);

            if (!seekString.isEmpty() &amp;&amp; seekString.length() == 1) {
                //firstLetter = seekString.toUpperCase();
                for (int i = 0; i &lt; searchNames.size(); i++) {
                    if (searchNames.get(i).toString().contains(firstLetter)) {
                        partialNames.add(searchNames.get(i).toString());
                    }
                }
            }
            else if (!seekString.isEmpty() &amp;&amp; seekString.length() &gt; 1) {
                //searchCriteria = searchCriteria.concat(seekString);
                String searchWord = firstLetter.concat(seekString.substring(1,seekString.length()));
                Log.d("JAY", "The seekString is " + seekString);
                Log.d("JAY", "The searchWord is " + searchWord);
                for (int i = 0; i &lt; searchNames.size(); i++) {
                    if (searchNames.get(i).toString().contains(searchWord)) {
                        partialNames.add(searchNames.get(i).toString());
                    }
                }
            }
    }
    adapter.notifyDataSetChanged();         
}
</code></pre>

<p>Here is the logcat:</p>

<p>D/AndroidRuntime( 1518): Shutting down VM</p>

<p>W/dalvikvm( 1518): threadid=1: thread exiting with uncaught exception (group=0x40015560)</p>

<p>E/AndroidRuntime( 1518): FATAL EXCEPTION: main</p>

<p>E/AndroidRuntime( 1518): java.lang.StringIndexOutOfBoundsException</p>

<p>E/AndroidRuntime( 1518):    at java.lang.String.substring(String.java:1651)</p>

<p>E/AndroidRuntime( 1518):    at com.jaylefler.contacts.SearchContacts.AlterAdapter(SearchContacts.java:92)</p>

<p>E/AndroidRuntime( 1518):    at com.jaylefler.contacts.SearchContacts.access$0(SearchContacts.java:88)</p>

<p>E/AndroidRuntime( 1518):    at com.jaylefler.contacts.SearchContacts$1.onTextChanged(SearchContacts.java:73)</p>

<p>E/AndroidRuntime( 1518):    at android.widget.TextView.sendOnTextChanged(TextView.java:6295)</p>

<p>E/AndroidRuntime( 1518):    at android.widget.TextView.handleTextChanged(TextView.java:6336)</p>

<p>E/AndroidRuntime( 1518):    at android.widget.TextView$ChangeWatcher.onTextChanged(TextView.java:6485)</p>

<p>E/AndroidRuntime( 1518):    at android.text.SpannableStringBuilder.sendTextChange(SpannableStringBuilder.java:889)</p>

<p>E/AndroidRuntime( 1518):    at android.text.SpannableStringBuilder.change(SpannableStringBuilder.java:400)</p>

<p>E/AndroidRuntime( 1518):    at android.text.SpannableStringBuilder.change(SpannableStringBuilder.java:269)</p>

<p>E/AndroidRuntime( 1518):    at android.text.SpannableStringBuilder.replace(SpannableStringBuilder.java:432)</p>

<p>E/AndroidRuntime( 1518):    at android.text.SpannableStringBuilder.delete(SpannableStringBuilder.java:218)</p>

<p>E/AndroidRuntime( 1518):    at android.text.SpannableStringBuilder.delete(SpannableStringBuilder.java:28)</p>

<p>E/AndroidRuntime( 1518):    at android.text.method.BaseKeyListener.backspace(BaseKeyListener.java:60)</p>

<p>E/AndroidRuntime( 1518):    at android.text.method.BaseKeyListener.onKeyDown(BaseKeyListener.java:123)</p>

<p>E/AndroidRuntime( 1518):    at android.text.method.QwertyKeyListener.onKeyDown(QwertyKeyListener.java:327)</p>

<p>E/AndroidRuntime( 1518):    at android.text.method.TextKeyListener.onKeyDown(TextKeyListener.java:132)</p>

<p>E/AndroidRuntime( 1518):    at android.widget.TextView.doKeyDown(TextView.java:4433)</p>

<p>E/AndroidRuntime( 1518):    at android.widget.TextView.onKeyDown(TextView.java:4267)</p>

<p>E/AndroidRuntime( 1518):    at android.view.KeyEvent.dispatch(KeyEvent.java:1256)</p>

<p>E/AndroidRuntime( 1518):    at android.view.View.dispatchKeyEvent(View.java:3855)</p>

<p>E/AndroidRuntime( 1518):    at android.view.ViewGroup.dispatchKeyEvent(ViewGroup.java:789)</p>

<p>E/AndroidRuntime( 1518):    at android.view.ViewGroup.dispatchKeyEvent(ViewGroup.java:789)</p>

<p>E/AndroidRuntime( 1518):    at android.view.ViewGroup.dispatchKeyEvent(ViewGroup.java:789)</p>

<p>E/AndroidRuntime( 1518):    at android.view.ViewGroup.dispatchKeyEvent(ViewGroup.java:789)</p>

<p>E/AndroidRuntime( 1518):    at android.view.ViewGroup.dispatchKeyEvent(ViewGroup.java:789)</p>

<p>E/AndroidRuntime( 1518):    at android.view.ViewGroup.dispatchKeyEvent(ViewGroup.java:789)</p>

<p>E/AndroidRuntime( 1518):    at com.android.internal.policy.impl.PhoneWindow$DecorView.superDispatchKeyEvent(PhoneWindow.java:1687)</p>

<p>E/AndroidRuntime( 1518):    at com.android.internal.policy.impl.PhoneWindow.superDispatchKeyEvent(PhoneWindow.java:1120)</p>

<p>E/AndroidRuntime( 1518):    at android.app.Activity.dispatchKeyEvent(Activity.java:2073)</p>

<p>E/AndroidRuntime( 1518):    at com.android.internal.policy.impl.PhoneWindow$DecorView.dispatchKeyEvent(PhoneWindow.java:1663)</p>

<p>E/AndroidRuntime( 1518):    at android.view.ViewRoot.deliverKeyEventToViewHierarchy(ViewRoot.java:2560)</p>

<p>E/AndroidRuntime( 1518):    at android.view.ViewRoot.handleFinishedEvent(ViewRoot.java:2535)</p>

<p>E/AndroidRuntime( 1518):    at android.view.ViewRoot.handleMessage(ViewRoot.java:1867)</p>

<p>E/AndroidRuntime( 1518):    at android.os.Handler.dispatchMessage(Handler.java:99)</p>

<p>E/AndroidRuntime( 1518):    at android.os.Looper.loop(Looper.java:123)</p>

<p>E/AndroidRuntime( 1518):    at android.app.ActivityThread.main(ActivityThread.java:3683)</p>

<p>E/AndroidRuntime( 1518):    at java.lang.reflect.Method.invokeNative(Native Method)</p>

<p>E/AndroidRuntime( 1518):    at java.lang.reflect.Method.invoke(Method.java:507)</p>

<p>E/AndroidRuntime( 1518):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)</p>

<p>E/AndroidRuntime( 1518):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)</p>

<p>E/AndroidRuntime( 1518):    at dalvik.system.NativeStart.main(Native Method)</p>

<p>W/ActivityManager(   74):   Force finishing activity com.jaylefler.contacts/.SearchContacts</p>

<p>W/ActivityManager(   74): Activity pause timeout for HistoryRecord{4086e540 com.jaylefler.contacts/.SearchContacts}</p>

<p>I/Process ( 1518): Sending signal. PID: 1518 SIG: 9</p>

<p>I/ActivityManager(   74): Process com.jaylefler.contacts (pid 1518) has died.</p>

<p>E/InputDispatcher(   74): channel '409a83e8 com.jaylefler.contacts/com.jaylefler.contacts.ContactProjectActivity (server)' ~ Consumer closed input channel or an error occurred.  events=0x8</p>

<p>E/InputDispatcher(   74): channel '409a83e8 com.jaylefler.contacts/com.jaylefler.contacts.ContactProjectActivity (server)' ~ Channel is unrecoverably broken and will be disposed!</p>

<p>I/WindowManager(   74): WIN DEATH: Window{4086ff40 com.jaylefler.contacts/com.jaylefler.contacts.SearchContacts paused=false}</p>

<p>I/WindowManager(   74): WIN DEATH: Window{409a83e8 com.jaylefler.contacts/com.jaylefler.contacts.ContactProjectActivity paused=false}</p>

<p>E/InputDispatcher(   74): Received spurious receive callback for unknown input channel.  fd=150, events=0x8</p>

<p>W/InputManagerService(   74): Got RemoteException sending setActive(false) notification to pid 1518 uid 10030</p>
