Getting ANR in aftertextchanged while setting numbers to subscript in Android Edittext

huangapple 未分类评论47阅读模式
英文:

Getting ANR in aftertextchanged while setting numbers to subscript in Android Edittext

问题

我有一个EditText,用于输入化学方程式。我在afterTextChangedListener中对文本附加了HTML标签。我在代码中遇到了很多ANR(应用程序未响应)错误,错误提示是:Input Dispatching timed out。

tv1.addTextChangedListener(new TextWatcher() {
    int currentpos;
  
    @Override
    public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {
        
    }

    @Override
    public void onTextChanged(CharSequence s, int i, int i1, int i2) {
    }

    @Override
    public void afterTextChanged(Editable editable) {
        try {
            tv1.removeTextChangedListener(this);
            String s = editable.toString();
            String s22 = s.replaceAll("([0-9]+)", "<sub><small>$1</sub></small>");
            tv1.setText("");
            tv1.append(Html.fromHtml(s22));
            if (currentpos < tv1.getText().toString().length() - 1) {
                tv1.setSelection(currentpos);
            }
            tv1.addTextChangedListener(this);
        } catch (Exception e) {
            Log.e(e.getLocalisedMessage().toString());
        }
    }
});

所有的ANR错误都指向以下代码行:

tv1.append(Html.fromHtml(s22));

有谁可以帮我解决这个问题吗?
在输入文本时,有没有更好的方法来实时设置下标?

从Google Play获取的堆栈跟踪(两个ANR错误都指向代码中的相同行):

广播 Intent { act=android.intent.action.SCREEN_OFF flg=0x50200010(具有附加项)}

输入分派超时

主线程 " prio=5 tid=1 Native
| group="main" sCount=1 dsCount=0 flags=1 obj=0x737e0680 self=0xefefb000
| sysTid=6357 nice=0 cgrp=default sched=0/0 handle=0xf41de4b8
| state=S schedstat=( 0 0 0 ) utm=6694 stm=2163 core=0 HZ=100
| stack=0xff524000-0xff526000 stackSize=8MB
| held mutexes=

#00 pc 0000000000018e50 /system/lib/libc.so (syscall+28)

#01 pc 00000000000b7d99 /system/lib/libart.so (art::ConditionVariable::WaitHoldingLocks(art::Thread*)+88)

#02 pc 00000000003ec5e5 /system/lib/libart.so (art::GoToRunnable(art::Thread*)+308)

...
(省略部分堆栈跟踪信息)



<details>
<summary>英文:</summary>

I have a edittext which takes chemical equation as input. I attach html tags on text in aftertextchangedlistener. I am getting a lot of ANRs as Input Dispatching timed out in the code : 

 

    tv1.addTextChangedListener(new TextWatcher() {
                    int currentpos;
              
                    @Override
                    public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2)        
                    {
    
                    }
    
                    @Override
                    public void onTextChanged(CharSequence s, int i, int i1, int i2) {
                            }
    
    
                    @Override
                    public void afterTextChanged(Editable editable) {
                        try {
    
    
    
                            tv1.removeTextChangedListener(this);
                            String s = editable.toString();
                            String s22=s.replaceAll(&quot;([0-9]+)&quot;,&quot;&lt;sub&gt;&lt;small&gt;$1&lt;/sub&gt;&lt;/small&gt;&quot;);
                             tv1.setText(&quot;&quot;);
                            tv1.append(Html.fromHtml(s22));
                            if (currentpos &lt; tv1.getText().toString().length() - 1) {
                                tv1.setSelection(currentpos);
                            }
                            tv1.addTextChangedListener(this);
    
                            //SpannableStringBuilder  s= editable.toString();
                        } catch (Exception e) {
    
                              Log.e(e.getLocalisedMessage().toString());
                           }
                    }
                });


All ANR point to the line 

      tv1.append(Html.fromHtml(s22));
                      
Can anyone help me with this?
And is there any better way to set subscript on the fly while entering text?



Stacktrace from Google Play (Both ANRs point to same line in code)

Broadcast of Intent { act=android.intent.action.SCREEN_OFF flg=0x50200010 (has extras) } 

Input dispatching timed out 
 
    main&quot; prio=5 tid=1 Native
      | group=&quot;main&quot; sCount=1 dsCount=0 flags=1 obj=0x737e0680 self=0xefefb000
      | sysTid=6357 nice=0 cgrp=default sched=0/0 handle=0xf41de4b8
      | state=S schedstat=( 0 0 0 ) utm=6694 stm=2163 core=0 HZ=100
      | stack=0xff524000-0xff526000 stackSize=8MB
      | held mutexes=
     
      #00  pc 0000000000018e50  /system/lib/libc.so (syscall+28)
     
      #01  pc 00000000000b7d99  /system/lib/libart.so (art::ConditionVariable::WaitHoldingLocks(art::Thread*)+88)
     
      #02  pc 00000000003ec5e5  /system/lib/libart.so (art::GoToRunnable(art::Thread*)+308)
     
      #03  pc 00000000003ec481  /system/lib/libart.so (art::JniMethodEnd(unsigned int, art::Thread*)+8)
     
      #04  pc 00000000007933e9  /system/framework/arm/boot-framework.oat (Java_android_os_BinderProxy_transactNative__ILandroid_os_Parcel_2Landroid_os_Parcel_2I+144)
     
      at android.os.BinderProxy.transactNative (Native method)
     
      at android.os.BinderProxy.transact (Binder.java:786)
     
      at android.content.pm.IPackageManager$Stub$Proxy.hasSystemFeature (IPackageManager.java:6177)
     
      at android.app.ApplicationPackageManager.hasSystemFeature (ApplicationPackageManager.java:712)
     
      at android.app.ApplicationPackageManager.hasSystemFeature (ApplicationPackageManager.java:706)
     
      at android.view.ViewGroup.shouldBlockFocusForTouchscreen (ViewGroup.java:1333)
     
      at android.view.ViewGroup.addFocusables (ViewGroup.java:1206)
     
      at android.view.ViewGroup.addFocusables (ViewGroup.java:1234)
     
      at android.view.ViewGroup.addFocusables (ViewGroup.java:1234)
     
      at androidx.drawerlayout.widget.DrawerLayout.addFocusables (DrawerLayout.java:1936)
     
      at android.view.ViewGroup.addFocusables (ViewGroup.java:1234)
     
      at android.view.ViewGroup.addFocusables (ViewGroup.java:1234)
     
      at android.view.ViewGroup.addFocusables (ViewGroup.java:1234)
     
      at android.view.View.addFocusables (View.java:11181)
     
      at android.view.FocusFinder.findNextFocus (FocusFinder.java:108)
     
      at android.view.FocusFinder.findNextFocus (FocusFinder.java:80)
     
      at android.view.ViewGroup.focusSearch (ViewGroup.java:965)
     
      at android.view.ViewGroup.focusSearch (ViewGroup.java:967)
     
      at android.view.ViewGroup.focusSearch (ViewGroup.java:967)
     
      at android.view.ViewGroup.focusSearch (ViewGroup.java:967)
     
      at android.view.View.focusSearch (View.java:10846)
     
      at android.widget.TextView.onCreateInputConnection (TextView.java:8717)
     
      at androidx.appcompat.widget.AppCompatAutoCompleteTextView.onCreateInputConnection (AppCompatAutoCompleteTextView.java:189)
     
      at android.view.inputmethod.InputMethodManager.startInputInner (InputMethodManager.java:1690)
     
      at android.view.inputmethod.InputMethodManager.restartInput (InputMethodManager.java:1604)
     
      at android.widget.TextView.setText (TextView.java:5940)
     
      at android.widget.TextView.setText (TextView.java:5876)
     
      at android.widget.EditText.setText (EditText.java:143)
     
      at android.widget.TextView.setText (TextView.java:5833)
     
      at com.equationbalancer.MainActivity$4.afterTextChanged (MainActivity.java:1106)
     
      at android.widget.TextView.sendAfterTextChanged (TextView.java:10745)
     
      at android.widget.TextView.setText (TextView.java:6059)
     
      at android.widget.TextView.setText (TextView.java:5876)
     
      at android.widget.EditText.setText (EditText.java:143)
     
      at android.widget.TextView.setText (TextView.java:5833)
     
      at com.equationbalancer.MainActivity$4.afterTextChanged (MainActivity.java:1106)
     
      at android.widget.TextView.sendAfterTextChanged (TextView.java:10745)
     
      at android.widget.TextView.setText (TextView.java:6059)
     
      at android.widget.TextView.setText (TextView.java:5876)
     
      at android.widget.EditText.setText (EditText.java:143)
     
      at android.widget.TextView.setText (TextView.java:5833)
     
      at com.equationbalancer.MainActivity$4.afterTextChanged (MainActivity.java:1106)
     
      at android.widget.TextView.sendAfterTextChanged (TextView.java:10745)
     
      at android.widget.TextView.setText (TextView.java:6059)
     
      at android.widget.TextView.setText (TextView.java:5876)
     
      at android.widget.EditText.setText (EditText.java:143)
     
      at android.widget.TextView.setText (TextView.java:5833)
     
      at com.equationbalancer.MainActivity$4.afterTextChanged (MainActivity.java:1106)
     
      at android.widget.TextView.sendAfterTextChanged (TextView.java:10745)
     
      at android.widget.TextView.setText (TextView.java:6059)
     
      at android.widget.TextView.setText (TextView.java:5876)
     
      at android.widget.EditText.setText (EditText.java:143)
     
      at android.widget.TextView.setText (TextView.java:5833)
     
      at com.equationbalancer.MainActivity$4.afterTextChanged (MainActivity.java:1106)
     
      at android.widget.TextView.sendAfterTextChanged (TextView.java:10745)
     
      at android.widget.TextView.setText (TextView.java:6059)
     
      at android.widget.TextView.setText (TextView.java:5876)
     
      at android.widget.EditText.setText (EditText.java:143)
     
      at android.widget.TextView.setText (TextView.java:5833)
     
      at com.equationbalancer.MainActivity$4.afterTextChanged (MainActivity.java:1106)
     
      at android.widget.TextView.sendAfterTextChanged (TextView.java:10745)
     
      at android.widget.TextView.setText (TextView.java:6059)
     
      at android.widget.TextView.setText (TextView.java:5876)
     
      at android.widget.EditText.setText (EditText.java:143)
     
      at android.widget.TextView.setText (TextView.java:5833)
     
      at com.equationbalancer.MainActivity$4.afterTextChanged (MainActivity.java:1106)
     
      at android.widget.TextView.sendAfterTextChanged (TextView.java:10745)
     
      at android.widget.TextView$ChangeWatcher.afterTextChanged (TextView.java:14241)
     
      at android.text.SpannableStringBuilder.sendAfterTextChanged (SpannableStringBuilder.java:1262)
     
      at android.text.SpannableStringBuilder.replace (SpannableStringBuilder.java:574)
     
      at android.text.SpannableStringBuilder.replace (SpannableStringBuilder.java:504)
     
      at android.text.SpannableStringBuilder.replace (SpannableStringBuilder.java:502)
     
      at android.view.inputmethod.BaseInputConnection.replaceText (BaseInputConnection.java:849)
     
      at android.view.inputmethod.BaseInputConnection.commitText (BaseInputConnection.java:199)
     
      at com.android.internal.widget.EditableInputConnection.commitText (EditableInputConnection.java:183)
     
      at com.android.internal.view.IInputConnectionWrapper.executeMessage (IInputConnectionWrapper.java:341)
     
      at com.android.internal.view.IInputConnectionWrapper$MyHandler.handleMessage (IInputConnectionWrapper.java:85)
     
      at android.os.Handler.dispatchMessage (Handler.java:106)
     
      at android.os.Looper.loop (Looper.java:164)
     
      at android.app.ActivityThread.main (ActivityThread.java:7000)
     
      at java.lang.reflect.Method.invoke (Native method)
     
      at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run (RuntimeInit.java:441)
     
      at com.android.internal.os.ZygoteInit.main (ZygoteInit.java:1408)



</details>


huangapple
  • 本文由 发表于 2020年5月5日 13:23:02
  • 转载请务必保留本文链接:https://java.coder-hub.com/61606275.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定