如何解决BadgeView类中的java.lang.StackOverflowError

时间:2015-11-26 09:56:48

标签: android badge

我正在使用这个BadgeView类在日历事件日期创建徽章。第一次运行它但是当下个月BadgeView类它给出stackoverflow错误.. 由此调用的badgeView类和在linearlayout上的徽章appy

badge = new BadgeView(context, event_dateview_layout);
                    badge.setText("" + eventno.size());
                    Log.d("aa", "eventno:" + eventno);
                    badge.show();


BadgeView.java

import android.content.Context;
import android.graphics.Color;
import android.graphics.Typeface;
import android.graphics.drawable.ShapeDrawable;
import android.graphics.drawable.shapes.RoundRectShape;
import android.util.AttributeSet;
import android.view.Gravity;
import android.view.View;
import android.view.ViewGroup;
import android.view.ViewGroup.LayoutParams;
import android.view.ViewParent;
import android.widget.FrameLayout;
import android.widget.TextView;

public class BadgeView extends TextView {

    private Context context;
    private View target;
    private ShapeDrawable badgeBg;

    public BadgeView(Context context, View target) {

        this(context, null, android.R.attr.textViewStyle, target);
    }

    public BadgeView(Context context, AttributeSet attrs, int defStyle, View target) {

        super(context, attrs, defStyle);
        init(context, target);
    }

    private void init(Context context, View target) {

        this.context = context;
        this.target = target;

        //apply defaults
        setTypeface(Typeface.DEFAULT_BOLD);
        setPadding(7, 0, 7, 0);
        setTextColor(Color.WHITE);

        if (this.target != null) {
            applyTo(this.target);
        } else {
            show();
        }

    }

    private void applyTo(View target) {

        LayoutParams lp = target.getLayoutParams();
        ViewParent parent = target.getParent();
        FrameLayout container = new FrameLayout(context);

        // TODO verify that parent is indeed a ViewGroup
        ViewGroup group = (ViewGroup) parent;
        int index = group.indexOfChild(target);

        group.removeView(target);
        group.addView(container, index, lp);
        container.addView(target);
        container.addView(this);
        group.invalidate();

    }

    public void show() {

        if (getBackground() == null) {
            if (badgeBg == null) {
                badgeBg = getDefaultBackground();
            }
            setBackgroundDrawable(badgeBg);
        }
        applyLayoutParams();
    }

    private ShapeDrawable getDefaultBackground() {

        float[] outerR = new float[]{20, 20, 20, 20, 20, 20, 20, 20};
        RoundRectShape rr = new RoundRectShape(outerR, null, null);
        ShapeDrawable drawable = new ShapeDrawable(rr);
        drawable.getPaint().setColor(Color.RED);
        return drawable;
    }

    private void applyLayoutParams() {

        FrameLayout.LayoutParams lp = new FrameLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
        lp.gravity = Gravity.RIGHT | Gravity.TOP;
        setLayoutParams(lp);
    }

 java.lang.StackOverflowError
            at android.view.HardwareCanvas.<init>(HardwareCanvas.java:29)
            at android.view.GLES20Canvas.<init>(GLES20Canvas.java:100)
            at android.view.GLES20RecordingCanvas.<init>(GLES20RecordingCanvas.java:62)
            at android.view.GLES20RecordingCanvas.<init>(GLES20RecordingCanvas.java:38)
            at android.view.GLES20RecordingCanvas$1.newInstance(GLES20RecordingCanvas.java:46)
            at android.view.GLES20RecordingCanvas$1.newInstance(GLES20RecordingCanvas.java:44)
            at android.util.FinitePool.acquire(FinitePool.java:69)
            at android.util.SynchronizedPool.acquire(SynchronizedPool.java:39)
            at android.view.GLES20RecordingCanvas.obtain(GLES20RecordingCanvas.java:66)
            at android.view.GLES20DisplayList.start(GLES20DisplayList.java:67)
            at android.view.View.getDisplayList(View.java:12682)
            at android.view.View.getDisplayList(View.java:12768)
            at android.view.View.draw(View.java:13502)
            at android.view.ViewGroup.drawChild(ViewGroup.java:2928)
            at android.view.ViewGroup.dispatchDraw(ViewGroup.java:2797)
            at android.view.View.getDisplayList(View.java:12722)
            at android.view.View.getDisplayList(View.java:12768)
            at android.view.View.draw(View.java:13502)
            at android.view.ViewGroup.drawChild(ViewGroup.java:2928)
            at android.view.ViewGroup.dispatchDraw(ViewGroup.java:2797)
            at android.view.View.getDisplayList(View.java:12722)
            at android.view.View.getDisplayList(View.java:12768)
            at android.view.View.draw(View.java:13502)
            at android.view.ViewGroup.drawChild(ViewGroup.java:2928)
            at android.view.ViewGroup.dispatchDraw(ViewGroup.java:2797)
            at android.view.View.getDisplayList(View.java:12722)
            at android.view.View.getDisplayList(View.java:12768)
            at android.view.View.draw(View.java:13502)
            at android.view.ViewGroup.drawChild(ViewGroup.java:2928)
            at android.view.ViewGroup.dispatchDraw(ViewGroup.java:2797)
            at android.view.View.getDisplayList(View.java:12722)
            at android.view.View.getDisplayList(View.java:12768)
            at android.view.View.draw(View.java:13502)
            at android.view.ViewGroup.drawChild(ViewGroup.java:2928)
            at android.view.ViewGroup.dispatchDraw(ViewGroup.java:2797)
            at android.view.View.getDisplayList(View.java:12722)
            at android.view.View.getDisplayList(View.java:12768)
            at android.view.View.draw(View.java:13502)
            at android.view.ViewGroup.drawChild(ViewGroup.java:2928)
            at android.view.ViewGroup.dispatchDraw(ViewGroup.java:2797)
            at android.view.View.getDisplayList(View.java:12722)
            at android.view.View.getDisplayList(View.java:12768)
            at android.view.View.draw(View.java:13502)
            at android.view.ViewGroup.drawChild(ViewGroup.java:2928)
            at android.view.ViewGroup.dispatchDraw(ViewGroup.java:2797)
            at android.view.View.getDisplayList(View.java:12722)
            at android.view.View.getDisplayList(View.java:12768)
            at android.view.View.draw(View.java:13502)
            at android.view.ViewGroup.drawChild(ViewGroup.java:2928)
            at android.view.ViewGroup.dispatchDraw(ViewGroup.java:2797)
            at android.view.View.getDisplayList(View.java:12722)
            at android.view.View.getDisplayList(View.java:12768)
            at android.view.View.draw(View.java:13502)
            at android.view.ViewGroup.drawChild(ViewGroup.java:2928)
            at android.view.ViewGroup.dispatchDraw(ViewGroup.java:2797)
            at android.view.View.getDisplayList(View.java:12722)
            at android.view.View.getDisplayList(View.java:12768)
            at android.view.View.draw(View.java:13502)
            at android.view.ViewGroup.drawChild(ViewGroup.java:2928)
            at android.view.ViewGroup.dispatchDraw(ViewGroup.java:2797)
            at android.view.View.getDisplayList(View.java:12722)
            at android.view.View.getDisplayList(View.java:12768)
            at android.view.View.draw(View.java:13502)
            at android.view.ViewGroup.drawChild(ViewGroup.java:2928)
            at android.view.ViewGroup.dispatchDraw(ViewGroup.java:2797)
            at android.view.View.getDisplayList(View.java:12722)
            at android.view.View.getDisplayList(View.java:12768)
            at android.view.View.draw(View.java:13502)
            at android.view.ViewGroup.drawChild(ViewGroup.java:2928)
            at android.view.ViewGroup.dispatchDraw(ViewGroup.java:2797)
            at android.view.View.getDisplayList(View.

1 个答案:

答案 0 :(得分:0)

我尝试使用您的代码,但没有任何错误。