I can't figure the issue with a stacktrace err

2019-01-11 11:09发布

问题:

I am apparently having an issue with 1 activity on a Samsung Nexus in an app and I can't understand what is actually causing the the issue.

java.lang.ArrayIndexOutOfBoundsException: length=244; index=1753
at android.text.MeasuredText.addStyleRun(MeasuredText.java:168)
at android.text.MeasuredText.addStyleRun(MeasuredText.java:204)
at android.text.StaticLayout.generate(StaticLayout.java:281)
at android.text.StaticLayout.<init>(StaticLayout.java:140)
at android.text.StaticLayout.<init>(StaticLayout.java:80)
at android.text.StaticLayout.<init>(StaticLayout.java:59)
at android.widget.TextView.makeSingleLayout(TextView.java:5901)
at android.widget.TextView.makeNewLayout(TextView.java:5741)
at android.widget.TextView.onMeasure(TextView.java:6098)
at android.view.View.measure(View.java:15172)
at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:4814)
at android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:1390)
at android.widget.LinearLayout.measureHorizontal(LinearLayout.java:1038)
at android.widget.LinearLayout.onMeasure(LinearLayout.java:576)
at android.view.View.measure(View.java:15172)
at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:4814)
at android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:1390)
at android.widget.LinearLayout.measureVertical(LinearLayout.java:681)
at android.widget.LinearLayout.onMeasure(LinearLayout.java:574)
at android.view.View.measure(View.java:15172)
at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:4814)
at android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:1390)
at android.widget.LinearLayout.measureVertical(LinearLayout.java:681)
at android.widget.LinearLayout.onMeasure(LinearLayout.java:574)
at android.view.View.measure(View.java:15172)
at android.widget.ScrollView.measureChildWithMargins(ScrollView.java:1196)
at android.widget.FrameLayout.onMeasure(FrameLayout.java:310)
at android.widget.ScrollView.onMeasure(ScrollView.java:318)
at android.view.View.measure(View.java:15172)
at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:4814)
at android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:1390)
at android.widget.LinearLayout.measureVertical(LinearLayout.java:681)
at android.widget.LinearLayout.onMeasure(LinearLayout.java:574)
at android.view.View.measure(View.java:15172)
at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:4814)
at android.widget.FrameLayout.onMeasure(FrameLayout.java:310)
at android.view.View.measure(View.java:15172)
at android.widget.LinearLayout.measureVertical(LinearLayout.java:833)
at android.widget.LinearLayout.onMeasure(LinearLayout.java:574)
at android.view.View.measure(View.java:15172)
at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:4814)
at android.widget.FrameLayout.onMeasure(FrameLayout.java:310)
at com.android.internal.policy.impl.PhoneWindow$DecorView.onMeasure(PhoneWindow.java:2148)
at android.view.View.measure(View.java:15172)
at android.view.ViewRootImpl.performMeasure(ViewRootImpl.java:1848)
at android.view.ViewRootImpl.measureHierarchy(ViewRootImpl.java:1100)
at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:1273)
at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:998)
at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:4212)
at android.view.Choreographer$CallbackRecord.run(Choreographer.java:725)
at android.view.Choreographer.doCallbacks(Choreographer.java:555)
at android.view.Choreographer.doFrame(Choreographer.java:525)
at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:711)
at android.os.Handler.handleCallback(Handler.java:615)
at android.os.Handler.dispatchMessage(Handler.java:92)
at android.os.Looper.loop(Looper.java:137)
at android.app.ActivityThread.main(ActivityThread.java:4745)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:511)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:786)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
at dalvik.system.NativeStart.main(Native Method)

This is the Activity that my customer is telling me is causing the problem. This is the ONLY problem.

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.Menu;



public class Protocols03_04Activity extends Activity 
{
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) 
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.protocols03_04);



    }
    //Options Menu
    @Override
    public boolean onCreateOptionsMenu(Menu menu)
    {
        super.onCreateOptionsMenu(menu);
        getMenuInflater().inflate(R.menu.optmenu, menu);
        menu.findItem(R.id.medcom_optmenu_item).setIntent(new Intent(this, CallMedcom.class));
        menu.findItem(R.id.dispatch_optmenu_item).setIntent(new Intent(this, CallDispatch.class));
        menu.findItem(R.id.settings_optmenu_item).setIntent(new Intent(this, ProtocolsSettingsActivity.class));
        menu.findItem(R.id.mmenu_optmenu_item).setIntent(new Intent(this, ProtocolsMMenuActivity.class));
        menu.findItem(R.id.gps_optmenu_item).setIntent(new Intent(this, GPSActivity.class));
        menu.findItem(R.id.search_optmenu_item).setIntent(new Intent(this, SearchActivity.class));
        return true;
    }
}

I have tried to recreate the issue, but I am not able. I tried on 2 different Motorola phones.

回答1:

Seems to be a known bug with TextView. Check similar issues here and here

There is mention of the galaxy in the post. The first issue is marked for future release, the second seemed to have been fixed.

UPDATED

djmedic has found a workaround here



回答2:

Had to same Problem after Updating Android Studio (or the sdk..). I could fix it by setting the inputType of the EditText.

android:inputType="text"


回答3:

I Had The same problem. Its not only an issue on jellyBean. It has to do with a text view adding span tags to html converted plain text causing an indexOutOfBounds exception. There is a workaround at this link.

Replace your text view with this custom textView. If the span tag issue arrises, this catches it and instead displays the content in plain text.

public class PatchedTextView extends TextView {
public PatchedTextView(Context context, AttributeSet attrs, int defStyle) {
    super(context, attrs, defStyle);
}
public PatchedTextView(Context context, AttributeSet attrs) {
    super(context, attrs);
}
public PatchedTextView(Context context) {
    super(context);
}
@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
    try{
        super.onMeasure(widthMeasureSpec, heightMeasureSpec);
    }catch (ArrayIndexOutOfBoundsException e){
        setText(getText().toString());
        super.onMeasure(widthMeasureSpec, heightMeasureSpec);
    }
}
@Override
public void setGravity(int gravity){
    try{
        super.setGravity(gravity);
    }catch (ArrayIndexOutOfBoundsException e){
        setText(getText().toString());
        super.setGravity(gravity);
    }
}
@Override
public void setText(CharSequence text, BufferType type) {
    try{
        super.setText(text, type);
    }catch (ArrayIndexOutOfBoundsException e){
        setText(text.toString());
    }
}

}



回答4:

My experience may help to those pals, who don't want to add custom TextView to their projects.

First, I could reproduce this error in the Android emulator (4.1.2 , API 16). After this I decided to figure out what is causing this error. Obviously - some formatting (which causes "spans" to be created). It turned out that in my case it was enough to replace in two places e.g. this:

\"<b>@username</b>\"

with this:

\"@username\"