10

I get some crash logs with Crashlytics with the following stack:

Fatal Exception: java.lang.IllegalStateException: focus search returned a view that wasn't able to take focus!
   at android.widget.TextView.onKeyUp(TextView.java:6413)
   at android.view.KeyEvent.dispatch(KeyEvent.java:2712)
   at android.view.View.dispatchKeyEvent(View.java:9960)
   at android.view.ViewGroup.dispatchKeyEvent(ViewGroup.java:1630)
   at android.view.ViewGroup.dispatchKeyEvent(ViewGroup.java:1630)
   at android.widget.ScrollView.dispatchKeyEvent(ScrollView.java:387)
   at android.view.ViewGroup.dispatchKeyEvent(ViewGroup.java:1630)
   at android.view.ViewGroup.dispatchKeyEvent(ViewGroup.java:1630)
   at android.view.ViewGroup.dispatchKeyEvent(ViewGroup.java:1630)
   at com.android.internal.policy.DecorView.superDispatchKeyEvent(DecorView.java:406)
   at com.android.internal.policy.PhoneWindow.superDispatchKeyEvent(PhoneWindow.java:1798)
   at android.app.Activity.dispatchKeyEvent(Activity.java:3024)
   at com.android.internal.policy.DecorView.dispatchKeyEvent(DecorView.java:320)
   at android.view.ViewRootImpl$ViewPostImeInputStage.processKeyEvent(ViewRootImpl.java:4331)
   at android.view.ViewRootImpl$ViewPostImeInputStage.onProcess(ViewRootImpl.java:4302)
   at android.view.ViewRootImpl$InputStage.deliver(ViewRootImpl.java:3853)
   at android.view.ViewRootImpl$InputStage.onDeliverToNext(ViewRootImpl.java:3906)
   at android.view.ViewRootImpl$InputStage.forward(ViewRootImpl.java:3872)
   at android.view.ViewRootImpl$AsyncInputStage.forward(ViewRootImpl.java:3999)
   at android.view.ViewRootImpl$InputStage.apply(ViewRootImpl.java:3880)
   at android.view.ViewRootImpl$AsyncInputStage.apply(ViewRootImpl.java:4056)
   at android.view.ViewRootImpl$InputStage.deliver(ViewRootImpl.java:3853)
   at android.view.ViewRootImpl$InputStage.onDeliverToNext(ViewRootImpl.java:3906)
   at android.view.ViewRootImpl$InputStage.forward(ViewRootImpl.java:3872)
   at android.view.ViewRootImpl$InputStage.apply(ViewRootImpl.java:3880)
   at android.view.ViewRootImpl$InputStage.deliver(ViewRootImpl.java:3853)
   at android.view.ViewRootImpl.deliverInputEvent(ViewRootImpl.java:6247)
   at android.view.ViewRootImpl.doProcessInputEvents(ViewRootImpl.java:6221)
   at android.view.ViewRootImpl.enqueueInputEvent(ViewRootImpl.java:6182)
   at android.view.ViewRootImpl$ViewRootHandler.handleMessage(ViewRootImpl.java:3651)
   at android.os.Handler.dispatchMessage(Handler.java:102)
   at android.os.Looper.loop(Looper.java:154)
   at android.app.ActivityThread.main(ActivityThread.java:6121)
   at java.lang.reflect.Method.invoke(Method.java)
   at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:889)
   at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:779)

The closest answer I could find is here: Fatal crash: Focus search returned a view that wasn't able to take focus

But my question is, how can I track the source for this crash? It is hard to indicate it since the crash log isn't explanatory enough about the exact source where the crash actually

Itai Spector
  • 652
  • 11
  • 24
  • Which Android version is affected? I have seen this under Android 7.1.2 and Android 8.0.0 but only if an old theme was used. You could try to use activity.setTheme to set a modern theme or a compatibility theme. This doesn't answer your questions how to track the source of this crash but maybe it fixes the crash. – Dominique Aug 09 '17 at 11:14
  • Can you please help with the steps the if it is fixed? – Maddy Apr 05 '23 at 11:12

3 Answers3

1

I solved this crash in my project by explicitly providing a value for the focusSearch method of the EditText to return. (Otherwise, Android will work out what it thinks is the next focusable view on its own). Subclass EditText and provide this additional code:

private MyEditText nextFocusField;

@Override
public View focusSearch(@ViewCompat.FocusRealDirection int direction) {
    if (direction == FOCUS_DOWN) {
        return (View) nextFocusField;
    }   
    return super.focusSearch(direction);
}   

public void setNextFocusField(MyEditText nextFocusField) {
    this.nextFocusField = nextFocusField;
}

Then in my fragment class setting up the EditTexts, I made sure to supply each field a next EditText. Having collected them in an ArrayList in the order I want, I assigned them like so:

int editTextCount = editTexts.size();
for (int i = 0;  i < editTextCount;  i++) {
    MyEditText editText = (MyEditText) editTexts.get(i);
    if (i < editTextCount - 1) { // if not the last item
        editText.setNextFocusField((MyEditText) editTexts.get(i + 1)); 
    }    
    else if (editTextCount > 1) { 
        editText.setNextFocusField(null);
    }    
}    
Casey Perkins
  • 1,853
  • 2
  • 23
  • 41
1

I also solve this problem using this:

editText.setImeOptions(EditorInfo.IME_ACTION_DONE);

just setting to my editText de ImeOptions. I hope it works for you.

  • 1
    For anyone wondering - "the action key performs a "done" operation, typically meaning there is nothing more to input and the IME will be closed" – C. Skjerdal Nov 27 '19 at 21:28
0

It happened to me only in horizontal orientation, because editText took all the screen and a 'Next' button was added automatically. When I clicked 'Next' it crashed the app.

The solution is to prevent the editText to fill the entire activity by:

android:imeOptions="flagNoExtractUi"
TheLogicGuy
  • 682
  • 8
  • 19