Editext提供IndexOutOfBound异常

时间:2016-06-21 10:51:26

标签: java android xml android-edittext

我创建了一个自定义的EditText,以便能够移动和调整EditText的大小。当我开始输入时,它正在我的手机上工作,但在其他手机上崩溃了。

当我将android:inputType="text"添加到Xml代码时,它工作但是应用程序的整个目的都丢失了 这是完整的堆栈错误

java.lang.IndexOutOfBoundsException
   at android.graphics.Paint.getTextRunAdvances(Paint.java:1841)
   at android.graphics.Paint.getTextRunAdvances(Paint.java:1818)
   at android.text.TextLine.handleText(TextLine.java:755)
   at android.text.TextLine.handleRun(TextLine.java:907)
   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:977)
   at android.text.Layout.drawText(Layout.java:329)
   at android.widget.Editor.drawHardwareAccelerated(Editor.java:1384)
   at android.widget.Editor.onDraw(Editor.java:1298)
   at android.widget.TextView.onDraw(TextView.java:5061)
   at com.oluwayomi.quotage.custom.DragEditText.onDraw(DragEditText.java:340)
   at android.view.View.draw(View.java:13917)
   at android.view.View.getDisplayList(View.java:12832)
   at android.view.View.getDisplayList(View.java:12876)
   at android.view.ViewGroup.dispatchGetDisplayList(ViewGroup.java:3019)
   at android.view.View.getDisplayList(View.java:12755)
   at android.view.View.getDisplayList(View.java:12876)
   at android.view.ViewGroup.dispatchGetDisplayList(ViewGroup.java:3019)
   at android.view.View.getDisplayList(View.java:12755)
   at android.view.View.getDisplayList(View.java:12876)
   at android.view.ViewGroup.dispatchGetDisplayList(ViewGroup.java:3019)
   at android.view.View.getDisplayList(View.java:12755)
   at android.view.View.getDisplayList(View.java:12876)
   at android.view.ViewGroup.dispatchGetDisplayList(ViewGroup.java:3019)
   at android.view.View.getDisplayList(View.java:12755)
   at android.view.View.getDisplayList(View.java:12876)
   at android.view.ViewGroup.dispatchGetDisplayList(ViewGroup.java:3019)
   at android.view.View.getDisplayList(View.java:12755)
   at android.view.View.getDisplayList(View.java:12876)
   at android.view.ViewGroup.dispatchGetDisplayList(ViewGroup.java:3019)
   at android.view.View.getDisplayList(View.java:12755)
   at android.view.View.getDisplayList(View.java:12876)
   at android.view.ViewGroup.dispatchGetDisplayList(ViewGroup.java:3019)
   at android.view.View.getDisplayList(View.java:12755)
   at android.view.View.getDisplayList(View.java:12876)
   at android.view.ViewGroup.dispatchGetDisplayList(ViewGroup.java:3019)
   at android.view.View.getDisplayList(View.java:12755)
   at android.view.View.getDisplayList(View.java:12876)
   at android.view.ViewGroup.dispatchGetDisplayList(ViewGroup.java:3019)
   at android.view.View.getDisplayList(View.java:12755)
   at android.view.View.getDisplayList(View.java:12876)
   at android.view.HardwareRenderer$GlRenderer.draw(HardwareRenderer.java:1249)
   at android.view.ViewRootImpl.draw(ViewRootImpl.java:2520)
   at android.view.ViewRootImpl.performDraw(ViewRootImpl.java:2387)
   at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:2189)
   at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:1181)
   at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:4942)
   at android.view.Choreographer$CallbackRecord.run(Choreographer.java:776)
   at android.view.Choreographer.doCallbacks(Choreographer.java:579)
   at android.view.Choreographer.doFrame(Choreographer.java:548)
   at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:762)
   at android.os.Handler.handleCallback(Handler.java:800)
   at android.os.Handler.dispatchMessage(Handler.java:100)
   at android.os.Looper.loop(Looper.java:194)
   at android.app.ActivityThread.main(ActivityThread.java:5370)
   at java.lang.reflect.Method.invokeNative(Native Method)
   at java.lang.reflect.Method.invoke(Method.java:525)
   at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:833)
   at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:600)
   at dalvik.system.NativeStart.main(Native Method)

这是XML代码

<RelativeLayout
            android:id="@+id/TextRoot"
            android:layout_width="match_parent"
            android:layout_height="match_parent">

            <com.oluwayomi.quotage.custom.DragEditText
                android:id="@+id/etText"
                style="@style/Widget.AppCompat.Button.Borderless"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_centerHorizontal="true"
                android:layout_gravity="center"
                android:layout_marginTop="124dp"
                android:background="@drawable/null_background"
                android:gravity="center"
                android:padding="10dp"
                android:hint="Type text here.\n Double tap to edit \n and long press to resize."
                android:textSize="22sp" />
        </RelativeLayout>

以下是我的自定义EditText的代码

    public DragEditText(Context context) {
        this(context, null);

    }

    public DragEditText(Context context, AttributeSet attrs) {
        super(context, attrs);
        mPaintFrame = new Paint();
        mPaintTranslucent = new Paint();
        c = context;

        RelativeLayout.LayoutParams layoutParams = new RelativeLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
        this.setLayoutParams(layoutParams);
    }

    public DragEditText(Context context, AttributeSet attrs, int defStyleAttr) {
        this(context, attrs);

    }  @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        super.onMeasure(widthMeasureSpec, heightMeasureSpec);
        int viewWidth = MeasureSpec.getSize(widthMeasureSpec);
        int viewHeight = MeasureSpec.getSize(heightMeasureSpec);

        mParentWidth = c.getResources().getDisplayMetrics().widthPixels;
        mParentHeight = c.getResources().getDisplayMetrics().heightPixels;
        mParentRect = new RectF(0, 0, viewWidth, viewHeight);
    }
@Override
    protected void onDraw(Canvas canvas) {
        drawDragFrame(canvas);
        super.onDraw(canvas);
    }
     private void drawDragFrame(Canvas canvas) {
            if (showResizeFrame) {
                drawFrame(canvas);
                drawHandleShadows(canvas);
                drawHandles(canvas);
            } else if (showDragFrame) {
                drawHighlight(canvas);
            }
        }

        private void drawFrame(Canvas canvas) {
            mPaintFrame.setAntiAlias(true);
            mPaintFrame.setFilterBitmap(true);
            mPaintFrame.setStyle(Paint.Style.STROKE);
            mPaintFrame.setColor(getResources().getColor(R.color.colorAccent));
            mPaintFrame.setStrokeWidth(1);
            canvas.drawRect(mFrameRect.left + mHandleSize / 2, mFrameRect.top, mFrameRect.right - mHandleSize / 2, mFrameRect.bottom, mPaintFrame);
        }

        private void drawHighlight(Canvas canvas) {
            mPaintTranslucent.setAntiAlias(true);
            mPaintTranslucent.setFilterBitmap(true);
            mPaintTranslucent.setStyle(Paint.Style.FILL);
            mPaintTranslucent.setColor(TRANSLUCENT_WHITE);
            mPaintTranslucent.setStrokeWidth(1);
            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
                canvas.drawRoundRect(mFrameRect.left, mFrameRect.top, mFrameRect.right - mHandleSize, mFrameRect.bottom, mHandleSize, mHandleSize, mPaintTranslucent);
            } else
                canvas.drawRect(mFrameRect.left, mFrameRect.top, mFrameRect.right - mHandleSize, mFrameRect.bottom, mPaintTranslucent);
        }

        private void drawHandles(Canvas canvas) {
            //drawHandleShadows(canvas);
            mPaintFrame.setStyle(Paint.Style.FILL_AND_STROKE);
            mPaintFrame.setColor(getResources().getColor(R.color.colorAccent));


            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
                canvas.drawRoundRect(mFrameRect.left, mFrameRect.top + mHandleSize, mFrameRect.left + mHandleSize, mFrameRect.bottom - mHandleSize, mHandleSize / 2, mHandleSize / 2, mPaintFrame);
                canvas.drawRoundRect(mFrameRect.right - mHandleSize, mFrameRect.top + mHandleSize, mFrameRect.right, mFrameRect.bottom - mHandleSize, mHandleSize / 2, mHandleSize / 2, mPaintFrame);
            } else {
                canvas.drawRect(mFrameRect.left, mFrameRect.top + mHandleSize, mFrameRect.left + mHandleSize, mFrameRect.bottom - mHandleSize, mPaintFrame);
                canvas.drawRect(mFrameRect.right - mHandleSize, mFrameRect.top + mHandleSize, mFrameRect.right, mFrameRect.bottom - mHandleSize, mPaintFrame);
            }

        }

        private void drawHandleShadows(Canvas canvas) {
            mPaintFrame.setStyle(Paint.Style.FILL);
            mPaintFrame.setColor(TRANSLUCENT_BLACK);
            RectF rect = new RectF(mFrameRect);
            rect.offset(0, 1);
            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
                canvas.drawRoundRect(rect.left, rect.top + mHandleSize, rect.left + mHandleSize, rect.bottom - mHandleSize, mHandleSize / 2, mHandleSize / 2, mPaintFrame);
                canvas.drawRoundRect(rect.right - mHandleSize, rect.top + mHandleSize, rect.right, rect.bottom - mHandleSize, mHandleSize / 2, mHandleSize / 2, mPaintFrame);
            } else {
                canvas.drawRect(rect.left, rect.top + mHandleSize, rect.left + mHandleSize, rect.bottom - mHandleSize, mPaintFrame);
                canvas.drawRect(rect.right - mHandleSize, rect.top + mHandleSize, rect.right, rect.bottom - mHandleSize, mPaintFrame);
            }
    }

1 个答案:

答案 0 :(得分:0)

在没有看到有用的解决方案的情况下进行搜索后,我终于发现了问题

style="@style/Widget.AppCompat.Button.Borderless"

无法应用EditText。

感谢大家。