Home » Android » Android EditText on longPress: ArrayIndexOutOfBoundsException: length=15; index=491

Android EditText on longPress: ArrayIndexOutOfBoundsException: length=15; index=491

Posted by: admin May 14, 2020 Leave a comment

Questions:

I have this strange behavior I cannot explain.
My layout is quite simple: An EditText:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:gravity="center"
    android:orientation="vertical"
    android:padding="10dp" >


        <EditText
            android:id="@+id/etPass"
            android:layout_width="match_parent"
            android:layout_height="wrap_content" />

</LinearLayout>

When I try to longPress it (to paste some text) I got an imediate Force Close and this error:

05-08 16:56:16.838: E/AndroidRuntime(12164): FATAL EXCEPTION: main
05-08 16:56:16.838: E/AndroidRuntime(12164): android.view.InflateException: Binary XML file line #17: Error inflating class <unknown>
05-08 16:56:16.838: E/AndroidRuntime(12164):    at android.view.LayoutInflater.createView(LayoutInflater.java:613)
05-08 16:56:16.838: E/AndroidRuntime(12164):    at com.android.internal.policy.impl.PhoneLayoutInflater.onCreateView(PhoneLayoutInflater.java:56)
05-08 16:56:16.838: E/AndroidRuntime(12164):    at android.view.LayoutInflater.onCreateView(LayoutInflater.java:660)
05-08 16:56:16.838: E/AndroidRuntime(12164):    at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:685)
05-08 16:56:16.838: E/AndroidRuntime(12164):    at android.view.LayoutInflater.inflate(LayoutInflater.java:466)
05-08 16:56:16.838: E/AndroidRuntime(12164):    at android.view.LayoutInflater.inflate(LayoutInflater.java:396)
05-08 16:56:16.838: E/AndroidRuntime(12164):    at android.view.LayoutInflater.inflate(LayoutInflater.java:352)
05-08 16:56:16.838: E/AndroidRuntime(12164):    at android.widget.Editor$ActionPopupWindow.initContentView(Editor.java:2995)
05-08 16:56:16.838: E/AndroidRuntime(12164):    at android.widget.Editor$PinnedPopupWindow.<init>(Editor.java:2280)
05-08 16:56:16.838: E/AndroidRuntime(12164):    at android.widget.Editor$ActionPopupWindow.<init>(Editor.java:2968)
05-08 16:56:16.838: E/AndroidRuntime(12164):    at android.widget.Editor$ActionPopupWindow.<init>(Editor.java:2968)
05-08 16:56:16.838: E/AndroidRuntime(12164):    at android.widget.Editor$HandleView.showActionPopupWindow(Editor.java:3189)
05-08 16:56:16.838: E/AndroidRuntime(12164):    at android.widget.Editor$InsertionHandleView.showWithActionPopup(Editor.java:3416)
05-08 16:56:16.838: E/AndroidRuntime(12164):    at android.widget.Editor$InsertionPointCursorController.showWithActionPopup(Editor.java:3652)
05-08 16:56:16.838: E/AndroidRuntime(12164):    at android.widget.Editor.performLongClick(Editor.java:870)
05-08 16:56:16.838: E/AndroidRuntime(12164):    at android.widget.TextView.performLongClick(TextView.java:7973)
05-08 16:56:16.838: E/AndroidRuntime(12164):    at android.view.View$CheckForLongPress.run(View.java:17140)
05-08 16:56:16.838: E/AndroidRuntime(12164):    at android.os.Handler.handleCallback(Handler.java:615)
05-08 16:56:16.838: E/AndroidRuntime(12164):    at android.os.Handler.dispatchMessage(Handler.java:92)
05-08 16:56:16.838: E/AndroidRuntime(12164):    at android.os.Looper.loop(Looper.java:213)
05-08 16:56:16.838: E/AndroidRuntime(12164):    at android.app.ActivityThread.main(ActivityThread.java:4786)
05-08 16:56:16.838: E/AndroidRuntime(12164):    at java.lang.reflect.Method.invokeNative(Native Method)
05-08 16:56:16.838: E/AndroidRuntime(12164):    at java.lang.reflect.Method.invoke(Method.java:511)
05-08 16:56:16.838: E/AndroidRuntime(12164):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:789)
05-08 16:56:16.838: E/AndroidRuntime(12164):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:556)
05-08 16:56:16.838: E/AndroidRuntime(12164):    at dalvik.system.NativeStart.main(Native Method)
05-08 16:56:16.838: E/AndroidRuntime(12164): Caused by: java.lang.reflect.InvocationTargetException
05-08 16:56:16.838: E/AndroidRuntime(12164):    at java.lang.reflect.Constructor.constructNative(Native Method)
05-08 16:56:16.838: E/AndroidRuntime(12164):    at java.lang.reflect.Constructor.newInstance(Constructor.java:417)
05-08 16:56:16.838: E/AndroidRuntime(12164):    at android.view.LayoutInflater.createView(LayoutInflater.java:587)
05-08 16:56:16.838: E/AndroidRuntime(12164):    ... 25 more
05-08 16:56:16.838: E/AndroidRuntime(12164): Caused by: java.lang.ArrayIndexOutOfBoundsException: length=15; index=491
05-08 16:56:16.838: E/AndroidRuntime(12164):    at android.content.res.StringBlock.get(StringBlock.java:64)
05-08 16:56:16.838: E/AndroidRuntime(12164):    at android.content.res.XmlBlock$Parser.getPooledString(XmlBlock.java:458)
05-08 16:56:16.838: E/AndroidRuntime(12164):    at android.content.res.TypedArray.loadStringValueAt(TypedArray.java:720)
05-08 16:56:16.838: E/AndroidRuntime(12164):    at android.content.res.TypedArray.getString(TypedArray.java:124)
05-08 16:56:16.838: E/AndroidRuntime(12164):    at android.widget.TextView.<init>(TextView.java:800)
05-08 16:56:16.838: E/AndroidRuntime(12164):    at android.widget.TextView.<init>(TextView.java:450)
05-08 16:56:16.838: E/AndroidRuntime(12164):    ... 28 more

I have really no clue what’s happening, and I would be pleased to get some help!

Just to be clear, I have nothing in my Activity related to a listener on this EditText:

import android.app.Activity;
import android.os.Bundle;

public class ProActivity extends Activity {

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        this.setContentView(R.layout.activity_pro);

    }

}
How to&Answers:

Just remove this line from your custom style

This single line:

  <item name="android:fontFamily">sans-serif-light</item>

Will make the EditText crash when long pressing it.

The Android copy/paste popup was the root cause of the error.

Answer:

this link solved my issue, although the auto-complete in xml files was not working, but the code in this example is correct, i declared all in the same styles file and it worked :

https://github.com/rengwuxian/MaterialEditText/issues/162

and that’s what was said in it :

Seems to be an error in styles, verify if you have

<item name="android:fontFamily"></item>

in your application theme, not in the themes view, in application theme.
The same error happened to me until i removed this line of my style.xml, if you have that line, remove them, if you need this property, you can stylize textviews and buttons with:

<style name="TextViewStyle" parent="android:Widget.TextView">
    <item name="android:fontFamily">sans-serif-medium</item>
</style>

<style name="ButtonStyle" parent="android:Widget.Holo.Button">
    <item name="android:fontFamily">sans-serif-medium</item>
</style>

And in your app theme, use:

<item name="android:textViewStyle">@style/TextViewStyle</item>
<item name="android:buttonStyle">@style/ButtonStyle</item>

Answer:

I have also encountered this problem ;

but the cause of it is that i use ContextThemeWrapper;

here is how we get LayoutInflater from ContextThemeWrapper

@Override 
public Object getSystemService(String name) {
    if (LAYOUT_INFLATER_SERVICE.equals(name)) {
        if (mInflater == null) {
            mInflater = LayoutInflater.from(mBase).cloneInContext(this);
        }
        return mInflater;
    }
    return mBase.getSystemService(name);
}

from the code block above, we can see that it handle LayoutInflater specially, which is the source of the problem.

my solution is : override this method as follow

@Override
public Object getSystemService(String name){
    return getBaseContext().getSystemService(name);
}

Answer:

I face this problem when using <item name="android:fontFamily">sans-serif</item> in style.xml

It CRASH on

  • Samsung S4 (SC-04E) (5.0.1)
  • Samsung Galaxy Node 5 (5.1.1)

It NOT CRASH on

  • Samsung S7 Edge (SM-G935F) (7.0)
  • Xiaomi A2 (8.0.1)
  • Pixel XL (8.1.0)
  • Sony Xperia Z5 Au (SOV32) (6.0)
  • Arrow NX (F-04G) (6.0.1)
  • Kyocera (S2) (7.0)

At first, I think this problem will only happened on Samsung but Samsung S7 don’t crash.

Then I think the API may cause this problem because current crash device have API < 6.0. However I am not sure about it because I don’t have much device for testing

Summary: It will crash in some device

TO FIX THIS PROBLEM
First, remove sans-serif
Then download Roboto font from https://github.com/google/fonts/tree/master/apache/roboto then add to project following https://developer.android.com/guide/topics/ui/look-and-feel/fonts-in-xml

Why Roboto font?
You are facing this problem when using sans-serif font right?
sans-serif in Android is Roboto font (see here Android default font)

If you know some device which will crash, please edit my answer, it may help for testing.
Hope it help