0

I have a list that will filter in input in an EditText field and populate the ListView according to what they type.

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

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.

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.

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() && seekString.length() == 1) {
                //firstLetter = seekString.toUpperCase();
                for (int i = 0; i < searchNames.size(); i++) {
                    if (searchNames.get(i).toString().contains(firstLetter)) {
                        partialNames.add(searchNames.get(i).toString());
                    }
                }
            }
            else if (!seekString.isEmpty() && seekString.length() > 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 < searchNames.size(); i++) {
                    if (searchNames.get(i).toString().contains(searchWord)) {
                        partialNames.add(searchNames.get(i).toString());
                    }
                }
            }
    }
    adapter.notifyDataSetChanged();         
}

Here is the logcat:

D/AndroidRuntime( 1518): Shutting down VM

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

E/AndroidRuntime( 1518): FATAL EXCEPTION: main

E/AndroidRuntime( 1518): java.lang.StringIndexOutOfBoundsException

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

5
  • How do I add the logcat output?
    – Jay Lefler
    Oct 30, 2011 at 20:23
  • OK, here is the logcat output from the moment my error occurred:
    – Jay Lefler
    Oct 30, 2011 at 20:30
  • Btw, when operating with a String already you don't need to use toString() anymore.
    – JimmyB
    Oct 30, 2011 at 20:30
  • Bingo: It is the substring(0,1), I bet. Have you tried what slkorolev suggested?
    – JimmyB
    Oct 30, 2011 at 20:33
  • I prefaced the code with if (searchName.getText().toString().length() != 0) and was able to get things working :-)
    – Jay Lefler
    Oct 30, 2011 at 20:40

2 Answers 2

2

I think the prob;em is that you use '==' for comparing searchName text with empty string. Such a comparison will always produce false. Use equals method instead or compare text length with 0.

6
  • +1 for me overlooking this even after all these years of Java... :)
    – JimmyB
    Oct 30, 2011 at 20:23
  • @Dave seekString.substring(0,1) will crash in that case.
    – JimmyB
    Oct 30, 2011 at 20:24
  • and while we're at it: An empty string can be checked for faster and less error-prone using str.length() == 0
    – JimmyB
    Oct 30, 2011 at 20:25
  • @HannoBinder Agreed, but that's a behavioral side effect. Oct 30, 2011 at 20:27
  • 1
    @Dave Even if we call it "behavioral side effect" it's the cause of the problem :)
    – slkorolev
    Oct 30, 2011 at 20:35
0

First of all have you tried to debug your application? You can use logcat to see what exception is raised (to open it: Window > Show view > Other, Android > Logcat).

Ok i suppose your problem, is that you are trying to substring a string that is empty if you just press backspace. In fact make a substring of your seekString just after read it without check if it is empty. Probably you must check if seekString is not empty and then try to make a substring.

In fact the Help of substring function says that one exception could be risen: IndexOutOfBoundsException - if beginIndex is negative or larger than the length of this String object.

1
  • Why do you (a) think there's a backspace (not sure how that could come from a text field anyway, it'd just delete the character) or (b) think that would cause an exception? Oct 30, 2011 at 20:22

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.