java 致命崩溃:焦点搜索返回了一个无法获得焦点的视图
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/13614101/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Fatal crash: Focus search returned a view that wasn't able to take focus
提问by ObAt
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.
当我在 EditText 中键入内容时,我的应用程序不断崩溃,但这并不总是仅在某些情况下发生。我正在使用 Android 4.0.4 (ICS) 的 Samsung Galaxy Tab 2 10.1 WiFI & 3G (GT-P5100) 上运行我的应用程序。我使用股票键盘。
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:
这是我的 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" />
采纳答案by jargetz
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 框。底部是顶部的 Next Focus Down。在某些情况下,我会隐藏底部框,因此当我从顶部框按键盘上的 next 时,它会尝试转到底部框,但由于它被隐藏,因此会崩溃。我通过将底部框(另一个 EditText 的下一个向下焦点的目标)设置为不可聚焦来解决这个问题:
EditText inputBox = (EditText)findViewById(R.id.Bottom_Box);
inputBox.setFocusable(false);
I hope this helps.
我希望这有帮助。
回答by Tatarize
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.
当 ImeOptions 设置为EditorInfo.IME_ACTION_NEXT
或时,会发生此错误EditorInfo.IME_ACTION_PREVIOUS
。虽然该视图的父视图不可聚焦,或者下一个找到的焦点对象不可聚焦。
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.
它调用查找应该快速跳转到的下一个焦点项目,但该项目不存在或不可聚焦,它会引发此错误。如果 next 是隐藏的,因此不可聚焦,或者父级无法实际处理 findFocus() 调用并返回 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.
因此,解决方案相当容易。在这种情况下,不要将其设置为下一个 IME_ACTION。如果您无法从文本字段快速跳转到文本字段,那么要么使其可行,要么将操作切换为“完成”。
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.
这是对某些默认操作的处理。请注意,为了向后兼容,如果未提供显式 ime 选项,我们不会执行此默认处理,而是将其转换为应用程序可能期望的正常输入键代码。
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.
如果您设置了 edittext 类型,或者您提供了一个功能上没有错误的显式 IME,它就不会这样做。或者有一个可以关注下一个或其他任何东西的父对象。它只是后备位中的遗留代码,有时可能没有尽可能给定的操作。
回答by Paramveer Singh
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.
以前的答案都不适合我。我在 recyclerview 中有 editText 并且下一个 editText 在不在视图中时无法获得焦点。
I checked for the cause of the problem and it was this in TextView class:
我检查了问题的原因,它是在 TextView 类中的:
// 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:
我更新我的 EditText 以监听任何编辑器操作并执行以下操作:
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;
}
});
回答by Hsiao-Ting
I have another solution, to trace TextView source codes and match the error log you provided
我有另一个解决方案,用于跟踪 TextView 源代码并匹配您提供的错误日志
TextView.java :
文本视图.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.
因此,我认为您可以给 View.OnClickListener 以避免发生错误。
ex: textView.setOnClickListener(new OnClickListener);
例如: textView.setOnClickListener(new OnClickListener);
回答by Goran Horia Mihail
I had this problem and fixed it like this:
我遇到了这个问题并像这样修复它:
public void setEditable(boolean flag) {
mEditText.setFocusableInTouchMode(flag);
mEditText.setFocusable(flag);
mEditText.setClickable(flag);
}
回答by Cristan
A reason for this to happen is when your EditText has a disabled view specified as nextFocusDown
.
发生这种情况的一个原因是当您的 EditText 具有指定为nextFocusDown
.