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

那年仲夏 提交于 2019-11-28 13:15:45

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.

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.

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 v = focusSearch(FOCUS_FORWARD);
            if (v != null) {
                if (!v.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 v, int actionId, KeyEvent event) {
            if (actionId == EditorInfo.IME_ACTION_NEXT) {
                View v1 = v.focusSearch(FOCUS_FORWARD);
                if (v1 != null) {
                    if (!v.requestFocus(FOCUS_FORWARD)) {
                        return true;
                    }
                }
               return false;
            } else return false;
        }
    });
Hsiao-Ting

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);

I had this problem and fixed it like this:

public void setEditable(boolean flag) {
    mEditText.setFocusableInTouchMode(flag);
    mEditText.setFocusable(flag);
    mEditText.setClickable(flag);
}

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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!