android上的非活动InputConnection警告的getExtractedText

pan*_*wal 120 java android logcat

我在logcat中收到以下警告.

getExtractedText on inactive InputConnection
Run Code Online (Sandbox Code Playgroud)

我无法找到背后的原因.请帮忙

Joh*_*ong 43

我遇到了类似的问题.我的logcat:

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.
Run Code Online (Sandbox Code Playgroud)

我的情况:我有一个用户输入的EditText视图.当用户按下按钮时,EditText将被清除.当我快速按下按钮时,大量不活动的InputConnection条目流出.

例如:

editText.setText(null);
Run Code Online (Sandbox Code Playgroud)

我上面的logcat中的最后一行提供了一个很好的指示.果然,InputConnection被清除文本的请求所淹没.我尝试修改代码以检查文本长度,然后再尝试清除它:

if (editText.length() > 0) {
    editText.setText(null);
}
Run Code Online (Sandbox Code Playgroud)

这有助于缓解快速按下按钮不再导致IInputConnectionWrapper警告流的问题.然而,当用户在应用程序处于足够负载等情况下在键入内容和按下按钮或按下按钮之间快速交替时,这仍然容易出现问题.

幸运的是,我找到了另一种清除文本的方法:Editable.clear().有了这个,我根本没有得到警告:

if (editText.length() > 0) {
    editText.getText().clear();
}
Run Code Online (Sandbox Code Playgroud)

请注意,如果您希望清除所有输入状态而不仅仅是文本(自动文本,自动缓存,多键,撤消),则可以使用TextKeyListener.clear(可编辑的e).

if (editText.length() > 0) {
    TextKeyListener.clear(editText.getText());
}
Run Code Online (Sandbox Code Playgroud)


aha*_*ash 16

更新:

我收到InputConnection警告的原因不是因为我在哪里设置文本(即,在onTextChanged回调中,或者afterTextChanged) - 这是因为我正在使用setText.

我通过致电解决了这个问题:

hiddenKeyboardText.getText().clear();
hiddenKeyboardText.append("some string");
Run Code Online (Sandbox Code Playgroud)

注意:我仍然在afterTextChanged回调中进行调用,但它也可以在没有警告的情况下运行ontextChanged.

上一个答案:

我在logcat中也收到了相同的消息,尽管我的情况略有不同.我想读取每个进入EditText的字符(或组成字符/粘贴文本),然后将有问题的EditText重置为默认的初始化字符串.

明文部分按照Johnson的解决方案工作.但是,重置文本是有问题的,我会收到输入连接警告.

最初,我的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;
}
Run Code Online (Sandbox Code Playgroud)

onTextChanged(...)被调用时,EditText上处于只读模式.我不确定这是否意味着我们不能做更多的事情来调用getText.clear()它(setText(...)调用也产生inputConnection警告).

但是,回调afterTextChanged(Editable s)是设置文本的正确位置.

@Override
public void afterTextChanged(Editable s) {

    if (isResettingKeyboard)
        return;

    resetKeyboardString();

    // ... 
}
Run Code Online (Sandbox Code Playgroud)

到目前为止,这是在没有任何警告的情况下工作的.


Emi*_*ile 7

从帮助文档

http://developer.android.com/reference/android/view/inputmethod/InputConnection.html

InputConnection接口是从InputMethod返回到接收其输入的应用程序的通信通道.它用于执行诸如读取光标周围的文本,将文本提交到文本框以及将原始键事件发送到应用程序之类的操作.

此外,进一步阅读显示

getExtractedText():如果输入连接变得无效(例如进程崩溃)或客户端花费太长时间来响应文本(返回时间为几秒钟),则此方法可能会失败 .在任何一种情况下,都返回null.

它似乎还监视此类文本的更改,并警告更改.

要解决问题,您必须探索正在进行的任何数据库查询,可能是在布局中的listViews或列表周围.

如果你没有任何视图,例如它在后台随机发生,那么我会建议它不是UI元素问题,所以忽略文本字段等.它可以是将信息存储在游标中或请求游标的后台服务.

此外,您的应用程序是否会出现此问题?或者也许是你最近安装的其他人.列出完整的logCat跟踪.有人可能会认识到这个问题.

我猜想如果你没有写一些特定的东西,它的某个人会记录消息,或者你正在使用的库?


ant*_*iom 7

我遇到了同样的问题.当我的一个软键盘被激活EditTexts并且活动失去焦点时出现警告.

我所做的是在onPause()中隐藏键盘;

@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();
}
Run Code Online (Sandbox Code Playgroud)

  • 这是答案.在您远离当前屏幕上的活动之前,请确保隐藏键盘. (2认同)