getExtractedText on inactive InputConnection
我无法找到背后的原因,请帮忙。
getExtractedText on inactive InputConnection
我无法找到背后的原因,请帮忙。
我遇到了类似的问题。我的日志:
W/IInputConnectionWrapper(21214): getTextBeforeCursor on inactive InputConnection
W/IInputConnectionWrapper(21214): getSelectedText on inactive InputConnection
W/IInputConnectionWrapper(21214): getTextBeforeCursor on inactive InputConnection
W/IInputConnectionWrapper(21214): getTextAfterCursor on inactive InputConnection
...
I/Choreographer(20010): Skipped 30 frames! The application may be doing too much work on its main thread.
我的情况:
我有一个用户可以输入的EditText视图。当用户按下按钮时,EditText将被清除。当我快速按下按钮时,大量不活动的InputConnection条目会流出。
例如:
editText.setText(null);
if (editText.length() > 0) {
editText.setText(null);
}
if (editText.length() > 0) {
editText.getText().clear();
}
if (editText.length() > 0) {
TextKeyListener.clear(editText.getText());
}
更新:
我出现InputConnection警告的原因并不是因为我在哪里设置文本(例如,在onTextChanged
回调中或者afterTextChanged
中)——而是因为我使用了setText
。
我通过调用以下方法来解决这个问题:
hiddenKeyboardText.getText().clear();
hiddenKeyboardText.append("some string");
注意:尽管没有来自“ontextChanged”的警告,但我仍然在afterTextChanged
回调中进行调用。onTextChanged(CharSequence s, ...)
定义如下:@Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
if (isResettingKeyboard)
return;
// ... do what needs to be done
resetKeyboardString();
}
public void resetKeyboardString()
{
isResettingKeyboard = true;
hiddenKeyboardText.getText().clear();
hiddenKeyboardText.setText(keyboardInitString);
hiddenKeyboardText.setSelection(defaultKeyboardCursorLocation);
isResettingKeyboard = false;
}
当调用onTextChanged(...)
时,EditText处于只读模式。我不确定这是否意味着我们不能对其进行更多操作,除了调用getText.clear()
(调用setText(...)
也会产生inputConnection警告)。
然而,afterTextChanged(Editable s)
回调是设置文本的正确位置。
@Override
public void afterTextChanged(Editable s) {
if (isResettingKeyboard)
return;
resetKeyboardString();
// ...
}
到目前为止,这个工作没有出现任何警告。
afterTextChanged
方法在hiddenKeyboardText.getText().clear();
和hiddenKeyboardText.append("some string");
上都被调用,这一事实也应该被考虑进去。给你点赞! - Nickif (isResettingKeyboard) return;
在顶部... - ahashEditText
中被激活并且活动失去焦点时,出现了警告。@Override
protected void onPause() {
// hide the keyboard in order to avoid getTextBeforeCursor on inactive InputConnection
InputMethodManager inputMethodManager = (InputMethodManager)getSystemService(Context.INPUT_METHOD_SERVICE);
inputMethodManager.hideSoftInputFromWindow(myEditText.getWindowToken(), 0);
super.onPause();
}
http://developer.android.com/reference/android/view/inputmethod/InputConnection.html
InputConnection接口是从输入法到接收其输入的应用程序之间的通信渠道。它用于执行诸如读取光标周围的文本、提交文本到文本框和向应用程序发送原始键事件等操作。此外,进一步阅读显示:getExtractedText()方法可能会失败,如果输入连接变得无效(例如其进程崩溃)或客户端在回复文本方面花费太长时间(给予几秒钟返回),则将返回null。它似乎还监视此类文本的更改,并提醒更改发生。要解决问题,您需要探索正在进行的任何数据库查询,例如布局中的listViews或列表。如果您没有任何视图,例如在后台随机发生,则建议忽略文本字段等UI元素问题。可能是一个后台服务,在游标中存储信息或请求游标引起的。另外,问题是否来自您的应用程序?还是最近安装的其他人的应用程序?列出完整的logCat跟踪。有人可能认识这个问题。除了antoniom的答案外,确保在隐藏键盘后,确实完成了需要进行的任何进一步操作,因此,如果您已经像下面这样隐藏了键盘:
public void hideKeyboard() {
InputMethodManager inputMethodManager =(InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
inputMethodManager.hideSoftInputFromWindow(getWindow().getDecorView().getWindowToken(), 0);
}
getWindow().getDecorView().post(new Runnable() {
@Override
public void run() {
finish(); //Sample succeeding code
}
});
我不知道为什么会在构建过程中初始化控制器,但我确实这样做了。
`Widget build(BuildContext context) {
final _formKey = GlobalKey<FormState>();
final prodName = TextEditingController();
final prodDesc = TextEditingController();
final prodPrice = TextEditingController();
...`
`final _formKey = GlobalKey<FormState>();
final prodName = TextEditingController();
final prodDesc = TextEditingController();
final prodPrice = TextEditingController();
...
Widget build(BuildContext context) {`
当我将控制器和全局键正确地放置到我的表单中时,所有问题(如预期)都消失了!
View v = LayoutInflater.from(CONTEXT).inflate(R.layout.in_overscrollview, null);
Object= (Object) v.findViewById(R.id.OBJECT_ID);
Object.addTextChangedListener(new TextWatcher() {
@Override
public void afterTextChanged(Editable s) {
}
@Override
public void beforeTextChanged(CharSequence s, int start, int count, int after){
}
@Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
//Do my work
//Update my view
}
});
将其添加到列表适配器中并构建适配器。
JobListView = (ListView) getListView().findViewWithTag("JOBLISTVIEWTAG");
JobListView.addHeaderView(v, null, false);
JobsAdapter = new IN_JOBS_ADAPTER(CONTEXT, ITEMS_FOR_ADATER);
ListView.setAdapter(JOBSadapter);
一切都很好,文本监视器发挥了作用。
但是,如果我在初始构建之后重新构建适配器。
JobsAdapter = new IN_JOBS_ADAPTER(CONTEXT, DIFFERENT_ITEMS_FOR_ADAPTER);
ListView.setAdapter(JOBSadapter);
JOBSadapter.notifyDataSetChanged();
问题已经解决。
但是,如果您在适配器内部有一个对象,并且文本监视器附加到适配器内部的对象。 那么您可能需要做更多的工作。
尝试删除监听器,并在完成任何工作后重新附加它。
Object.removeTextChangedListener();
或者
Object.addTextChangedListener(null);
EditText
的可见性设置为 GONE
,然后立即将其设置为 VISIBLE
,因为我每次文本更改时都在验证输入,并且在某些情况下需要隐藏视图。 EditText
可能会失去焦点。清空EditText之前隐藏软键盘 - 这样不会显示警告。
另外,这似乎是与设备有关。我只在Nexus 4(Android 7.1)上看到过它。在模拟器(8.0、7.1)或Nexus 5上没有警告。