Android - EditText 在使用 textAllCaps 时给出 IndexOutOfBounds 异常 [英] Android - EditText gives IndexOutOfBounds Exception while using textAllCaps

查看:19
本文介绍了Android - EditText 在使用 textAllCaps 时给出 IndexOutOfBounds 异常的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用相对布局创建一个非常简单的注册页面.此注册页面链接到一个名为 RegistrationFragment 的片段.

I'm trying to create a very simple registration page using a relative layout. This registration page is linked to a fragment called RegistrationFragment.

我有五个用于此布局的 EditText 字段:姓名、电话号码、电子邮件、密码和确认密码.出于某种原因,我可以在密码中输入文本并确认密码,但是每当我尝试在其他字段中输入任何文本时,它们都会立即使应用程序崩溃并显示 IndexOutOfBounds 异常.

I have five EditText fields for this layout: name, phone number, email, password, and confirm password. For some reason, I can enter text into password and confirm password, but whenever I try to enter any text into the other fields, they immediately crash the application with an IndexOutOfBounds Exception.

这是完整的堆栈跟踪:

java.lang.IndexOutOfBoundsException
        at android.graphics.Paint.getTextRunAdvances(Paint.java:1879)
        at android.text.TextLine.handleText(TextLine.java:747)
        at android.text.TextLine.handleRun(TextLine.java:898)
        at android.text.TextLine.measureRun(TextLine.java:414)
        at android.text.TextLine.measure(TextLine.java:293)
        at android.text.TextLine.metrics(TextLine.java:267)
        at android.text.Layout.getLineExtent(Layout.java:998)
        at android.text.Layout.drawText(Layout.java:329)
        at android.widget.Editor.drawHardwareAccelerated(Editor.java:1380)
        at android.widget.Editor.onDraw(Editor.java:1303)
        at android.widget.TextView.onDraw(TextView.java:5163)
        at android.view.View.draw(View.java:14465)
        at android.view.View.getDisplayList(View.java:13362)
        at android.view.View.getDisplayList(View.java:13404)
        at android.view.ViewGroup.dispatchGetDisplayList(ViewGroup.java:3077)
        at android.view.View.getDisplayList(View.java:13300)
        at android.view.View.getDisplayList(View.java:13404)
        at android.view.ViewGroup.dispatchGetDisplayList(ViewGroup.java:3077)
        at android.view.View.getDisplayList(View.java:13300)
        at android.view.View.getDisplayList(View.java:13404)
        at android.view.ViewGroup.dispatchGetDisplayList(ViewGroup.java:3077)
        at android.view.View.getDisplayList(View.java:13300)
        at android.view.View.getDisplayList(View.java:13404)
        at android.view.ViewGroup.dispatchGetDisplayList(ViewGroup.java:3077)
        at android.view.View.getDisplayList(View.java:13300)
        at android.view.View.getDisplayList(View.java:13404)
        at android.view.ViewGroup.dispatchGetDisplayList(ViewGroup.java:3077)
        at android.view.View.getDisplayList(View.java:13300)
        at android.view.View.getDisplayList(View.java:13404)
        at android.view.HardwareRenderer$GlRenderer.buildDisplayList(HardwareRenderer.java:1570)
        at android.view.HardwareRenderer$GlRenderer.draw(HardwareRenderer.java:1449)
        at android.view.ViewRootImpl.draw(ViewRootImpl.java:2377)
        at android.view.ViewRootImpl.performDraw(ViewRootImpl.java:2249)
        at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:1879)
        at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:996)
        at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:5600)
        at android.view.Choreographer$CallbackRecord.run(Choreographer.java:761)
        at android.view.Choreographer.doCallbacks(Choreographer.java:574)
        at android.view.Choreographer.doFrame(Choreographer.java:544)
        at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:747)
        at android.os.Handler.handleCallback(Handler.java:733)
        at android.os.Handler.dispatchMessage(Handler.java:95)
        at android.os.Looper.loop(Looper.java:136)
        at android.app.ActivityThread.main(ActivityThread.java:5001)
        at java.lang.reflect.Method.invokeNative(Native Method)
        at java.lang.reflect.Method.invoke(Method.java:515)
        at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:785)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:601)
        at dalvik.system.NativeStart.main(Native Method)

我的 xml 布局文件:

My xml layout file:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="#e5e5e5">

<TextView
    android:id="@+id/fragment_registration_title"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_marginBottom="@dimen/activity_vertical_margin"
    android:layout_marginTop="@dimen/activity_vertical_margin"
    android:layout_marginRight="@dimen/activity_horizontal_margin"
    android:layout_marginLeft="@dimen/activity_horizontal_margin"
    android:text="@string/registration_title"
    android:textAllCaps="true"
    android:textSize="35sp"
    android:layout_centerHorizontal="true"
    android:textColor="@color/blue"
    />

<RelativeLayout
    android:id="@+id/fragment_registration_edit_text_layout"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_marginLeft="30dp"
    android:layout_marginRight="30dp"
    android:layout_below="@id/fragment_registration_title"
    android:layout_centerHorizontal="true">

    <EditText
        android:id="@+id/fragment_registration_legal_name"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:gravity="center_horizontal"
        android:hint="@string/full_name_prompt"
        android:maxLines="1"
        android:textAllCaps="true"
        android:background="@drawable/edit_text_top_rounded"
        />

    <EditText
        android:id="@+id/fragment_registration_cell_phone"
        android:layout_below="@id/fragment_registration_legal_name"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:gravity="center_horizontal"
        android:hint="@string/cell_phone_prompt"
        android:maxLines="1"
        android:textAllCaps="true"
        android:background="@drawable/edit_text_white"
        />

    <EditText
        android:id="@+id/fragment_registration_email"
        android:layout_below="@id/fragment_registration_cell_phone"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:gravity="center_horizontal"
        android:hint="@string/email_prompt"
        android:maxLines="1"
        android:textAllCaps="true"
        android:background="@drawable/edit_text_white"
        />

    <EditText
        android:id="@+id/fragment_registration_password"
        android:layout_below="@id/fragment_registration_email"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:gravity="center_horizontal"
        android:hint="@string/confirm_password_prompt"
        android:maxLines="1"
        android:background="@drawable/edit_text_bottom_rounded"
        />

    </RelativeLayout>

<Button
    android:id="@+id/fragment_registration_button"
    android:background="@drawable/button_registration"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="@string/registration_button"
    android:textColor="#ffffff"
    android:layout_below="@+id/fragment_registration_edit_text_layout"
    android:layout_marginTop="5dp"
    android:layout_marginLeft="30dp"
    android:layout_marginRight="30dp"
    android:layout_alignParentLeft="true"
    android:layout_alignParentRight="true"
    />
</RelativeLayout>

如果相关,我正在使用下面的代码查看"我的 Fragment 代码,它位于我的主要活动的 onCreate() 方法的末尾,用于测试目的.但是,当我尝试将注册作为活动进行测试时,我遇到了同样的错误.

If relevant, I'm "looking" at my Fragment code using the below, which is at the end of the onCreate() method of my main activity for testing purposes. I got the same errors when I tried testing Registration as an activity though.

RegistrationFragment test = new RegistrationFragment();
setContentView(R.layout.fragment_registration);

推荐答案

我在我的应用程序中为 EditText 使用 textAllCaps 时遇到了同样的问题.

I had the same problem with textAllCaps for EditText in my application.

我发现 textAllCaps 仅是 TextView 的属性.您不能将此属性用于 EditText.

I have found that textAllCaps is a property for TextView only. You can not use this property for EditText.

所以,我为此进行了研发,并为这个问题找到了更好的解决方案.

So, I did R&D for it and found a better solution for this issue.

我们可以使用 android:inputType="textCapCharacters" 而不是使用 textAllCaps.

Rather than using textAllCaps we can use android:inputType="textCapCharacters".

例如

    <EditText
        android:id="@+id/edittext1"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:ems="10"
        android:inputType="textCapCharacters"
        android:hint="@string/first_name"
        android:padding="10dp" >
    </EditText>

如果我们使用 android:inputType="textCapCharacters" 它会将所有字符转换为大写,就像我们在 textAllCaps 中想要的那样.

If we use android:inputType="textCapCharacters" it will convert all characters into UPPER CASE, like we want in textAllCaps.

P.S. 如果您使用 shift 键并输入文本,它可能会将文本转换为小写.您始终可以在字符串对象中使用 toUpper() 方法将其转换回大写.它可能会有所帮助...

P.S. If you use the shift key and type text it may convert the text in lowercase. You can always use toUpper() method in string object to convert it back to uppercase. It may help...

您可以从这篇博文中阅读这些详细信息:https://androidacademic.blogspot.com/2018/05/indexoutofbounds-exception-while-using.html

You can read these details from this blog post: https://androidacademic.blogspot.com/2018/05/indexoutofbounds-exception-while-using.html

这篇关于Android - EditText 在使用 textAllCaps 时给出 IndexOutOfBounds 异常的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆