Fatal crash: Focus search returned a view that wasn't able to take focus
Asked Answered
P

7

66

My application keeps crashing when I type something in a EditText, but this does not happen always only in some cases. I am running my app on a Samsung Galaxy Tab 2 10.1 WiFI & 3G (GT-P5100) with Android 4.0.4 (ICS). I use the stock Keyboard.

This is my logcat:

11-28 21:43:01.007: E/AndroidRuntime(15540): java.lang.IllegalStateException: focus search returned a view that wasn't able to take focus!
11-28 21:43:01.007: E/AndroidRuntime(15540):    at android.widget.TextView.onKeyUp(TextView.java:5833)
11-28 21:43:01.007: E/AndroidRuntime(15540):    at android.view.KeyEvent.dispatch(KeyEvent.java:2659)
11-28 21:43:01.007: E/AndroidRuntime(15540):    at android.view.View.dispatchKeyEvent(View.java:5547)
11-28 21:43:01.007: E/AndroidRuntime(15540):    at android.view.ViewGroup.dispatchKeyEvent(ViewGroup.java:1246)
11-28 21:43:01.007: E/AndroidRuntime(15540):    at android.view.ViewGroup.dispatchKeyEvent(ViewGroup.java:1246)
11-28 21:43:01.007: E/AndroidRuntime(15540):    at android.view.ViewGroup.dispatchKeyEvent(ViewGroup.java:1246)
11-28 21:43:01.007: E/AndroidRuntime(15540):    at android.view.ViewGroup.dispatchKeyEvent(ViewGroup.java:1246)
11-28 21:43:01.007: E/AndroidRuntime(15540):    at android.view.ViewGroup.dispatchKeyEvent(ViewGroup.java:1246)
11-28 21:43:01.007: E/AndroidRuntime(15540):    at android.view.ViewGroup.dispatchKeyEvent(ViewGroup.java:1246)
11-28 21:43:01.007: E/AndroidRuntime(15540):    at com.android.internal.policy.impl.PhoneWindow$DecorView.superDispatchKeyEvent(PhoneWindow.java:2027)
11-28 21:43:01.007: E/AndroidRuntime(15540):    at com.android.internal.policy.impl.PhoneWindow.superDispatchKeyEvent(PhoneWindow.java:1388)
11-28 21:43:01.007: E/AndroidRuntime(15540):    at android.app.Activity.dispatchKeyEvent(Activity.java:2324)
11-28 21:43:01.007: E/AndroidRuntime(15540):    at com.android.internal.policy.impl.PhoneWindow$DecorView.dispatchKeyEvent(PhoneWindow.java:1954)
11-28 21:43:01.007: E/AndroidRuntime(15540):    at android.view.ViewRootImpl.deliverKeyEventPostIme(ViewRootImpl.java:3360)
11-28 21:43:01.007: E/AndroidRuntime(15540):    at android.view.ViewRootImpl.handleMessage(ViewRootImpl.java:2618)
11-28 21:43:01.007: E/AndroidRuntime(15540):    at android.os.Handler.dispatchMessage(Handler.java:99)
11-28 21:43:01.007: E/AndroidRuntime(15540):    at android.os.Looper.loop(Looper.java:137)
11-28 21:43:01.007: E/AndroidRuntime(15540):    at android.app.ActivityThread.main(ActivityThread.java:4514)
11-28 21:43:01.007: E/AndroidRuntime(15540):    at java.lang.reflect.Method.invokeNative(Native Method)
11-28 21:43:01.007: E/AndroidRuntime(15540):    at java.lang.reflect.Method.invoke(Method.java:511)
11-28 21:43:01.007: E/AndroidRuntime(15540):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:790)
11-28 21:43:01.007: E/AndroidRuntime(15540):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:557)
11-28 21:43:01.007: E/AndroidRuntime(15540):    at dalvik.system.NativeStart.main(Native Method)

This is my one of my EditTexts:

    <EditText
    android:id="@+id/input_ftu_position_other"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_marginLeft="64dp"
    android:ems="20"
    android:inputType="text" />
Phenyl answered 28/11, 2012 at 20:47 Comment(1)
I don't know wich code I have to post because I don't know when my code chrashesPhenyl
S
25

I was having this same crash and although it doesn't sound like the exact situation, perhaps this will still be helpful:

I had two EditText boxes. The bottom one was the Next Focus Down of the top one. In some situations I would hide the bottom box, so when I hit next on the keyboard from the top box, it would try to go the bottom one, but would crash since it was hidden. I fixed this by setting the bottom box (the target of another EditText's Next Down Focus) as not focusable:

    EditText inputBox = (EditText)findViewById(R.id.Bottom_Box);
    inputBox.setFocusable(false);

I hope this helps.

Suspire answered 7/1, 2013 at 2:37 Comment(1)
setFocusable(false) makes EditText not editable, better to set a Condition before to check if you need editable.. just adding so it may help some body like me...Anomalism
M
64

This error occurs when the ImeOptions is set to EditorInfo.IME_ACTION_NEXT or EditorInfo.IME_ACTION_PREVIOUS. While the parent of that view is not focusable, or the next found focus object is not focusable.

It calls to find the next focus item that should be quickly jumped to but this does not exist or isn't focusable, it throws this error. It will happen if the next is hidden and thus not focusable or the parent cannot actually deal with the findFocus() call and returns null.

The solution thusly is rather easy. Don't set it to IME_ACTION as next in this case. If you can't quickly jump from text field to text field then either make that doable or switch the action to being DONE.

edittext.setImeOptions(EditorInfo.IME_ACTION_DONE);

Note the comment in the code before the offending bit:

This is the handling for some default action. Note that for backwards compatibility we don't do this default handling if explicit ime options have not been given, instead turning this into the normal enter key codes that an app may be expecting.

It won't do this if you set the edittext type or if you give an explicit IME that isn't functionally wrong. Or have a parent object that is focusable on the next or anything else. It's only legacy code in a fallback bit that might sometimes not have the given action as possible.

Milk answered 27/12, 2017 at 11:44 Comment(7)
Thank you so much! I couldn't find the cause of the crash until I read your answer. I was able to solve this issue by adding the following to my EditText: android:imeOptions="actionDone"Malefactor
In my case the crash occured when the next key on the soft keyboard tried to change the focus from an EditText to a RadioGroup ! setting android:imeOptions="actionDone"on the EditText the problem was gone. Thank you!Alurd
Your answer is correct. But why is this crash happening for android:imeOptions="actionSearch"? Stupid Android !Ive
@BugsHappen, that'd have to be a different crash bug and likely a new question. If it is this one it would be because the parent cannot deal with findFocus() and is returning null. After actionSearch where is the app focus supposed to go?Milk
I don't know, where is the app focus supposed to go when imeOptions="actionDone"? I think actionSearch should be the same as actionDoneIve
You could try some of the nextFocusForward attributes or so. Seems like they do different things. Done mean done and search means go search for this thing.Milk
Yeah, double checked it calls focusSearch in View.java and when it fails to find the next focus by FOCUS_DOWN and can't, returns null, and crashes.Milk
M
30

None of previous answers worked for me. I was having editText in recyclerview and the next editText couldn't receive focus when it was not in view.

I checked for the cause of the problem and it was this in TextView class:

    // This is the handling for some default action.
        // Note that for backwards compatibility we don't do this
        // default handling if explicit ime options have not been given,
        // instead turning this into the normal enter key codes that an
        // app may be expecting.
        if (actionCode == EditorInfo.IME_ACTION_NEXT) {
            View view = focusSearch(FOCUS_FORWARD);
            if (view != null) {
                if (!view.requestFocus(FOCUS_FORWARD)) {
                    throw new IllegalStateException("focus search returned a view "
                            + "that wasn't able to take focus!");
                }
            }
            return;

        } 

I update my EditText to listen for any editor actions and did this:

 companyNameET.setOnEditorActionListener(new TextView.OnEditorActionListener() {
        @Override
        public boolean onEditorAction(TextView textView, int actionId, KeyEvent event) {
            if (actionId == EditorInfo.IME_ACTION_NEXT) {
                View view = textView.focusSearch(FOCUS_FORWARD);
                if (view != null) {
                    if (!view.requestFocus(FOCUS_FORWARD)) {
                        return true;
                    }
                }
               return false;
            } 
            return false;
        }
    });
Monnet answered 22/3, 2018 at 16:4 Comment(0)
S
25

I was having this same crash and although it doesn't sound like the exact situation, perhaps this will still be helpful:

I had two EditText boxes. The bottom one was the Next Focus Down of the top one. In some situations I would hide the bottom box, so when I hit next on the keyboard from the top box, it would try to go the bottom one, but would crash since it was hidden. I fixed this by setting the bottom box (the target of another EditText's Next Down Focus) as not focusable:

    EditText inputBox = (EditText)findViewById(R.id.Bottom_Box);
    inputBox.setFocusable(false);

I hope this helps.

Suspire answered 7/1, 2013 at 2:37 Comment(1)
setFocusable(false) makes EditText not editable, better to set a Condition before to check if you need editable.. just adding so it may help some body like me...Anomalism
G
4

I have another solution, to trace TextView source codes and match the error log you provided

TextView.java :

if (!hasOnClickListeners()) {
    View v = focusSearch(FOCUS_DOWN);

    if (v != null) {
        if (!v.requestFocus(FOCUS_DOWN)) {
            throw new IllegalStateException(
                    "focus search returned a view " +
                    "that wasn't able to take focus!");
        }
    }
}

As a result, I think you could give View.OnClickListener to avoid the error happened.

ex: textView.setOnClickListener(new OnClickListener);

Gailey answered 3/8, 2015 at 10:12 Comment(0)
C
4

Simply add the below property in your edittext. android:imeOptions="actionDone"

<EditText
    android:id="@+id/input_ftu_position_other"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_marginLeft="64dp"
    android:ems="20"
    android:inputType="text" 
    android:imeOptions="actionDone"
/>
Commodus answered 13/1, 2022 at 6:22 Comment(0)
R
1

I had this problem and fixed it like this:

public void setEditable(boolean flag) {
    mEditText.setFocusableInTouchMode(flag);
    mEditText.setFocusable(flag);
    mEditText.setClickable(flag);
}
Rod answered 16/5, 2017 at 11:34 Comment(0)
A
-1

A reason for this to happen is when your EditText has a disabled view specified as nextFocusDown.

Antirrhinum answered 8/3, 2019 at 10:53 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.