try / catch中的致命异常?

时间:2014-05-27 04:26:34

标签: java android

我正在尝试使用HERE

所述的通知徽章

它在我的4.4.2设备上完美运行,但在我的4.0.4设备上,我在打开时崩溃。

即使在try / catch中,我也会收到此错误并强制关闭。

以下是有问题的代码:

try {
    Utils.setBadgeCount(this, ordersIcon, ordersCount, getResources().getColor(R.color.red_text));
    Utils.setBadgeCount(this, balancesIcon, balancesCount, getResources().getColor(R.color.green_text));
} catch (Exception e) {
    Toast.makeText(this, "Dynamic menu icons not available on this device.", Toast.LENGTH_LONG).show();
}

注释掉try块中的两行可以阻止致命异常被抛出。

这是setBadgeCount方法。

public static void setBadgeCount(Context context, LayerDrawable icon, int count, int color) {
    BadgeDrawable badge;
    Drawable reuse = icon.findDrawableByLayerId(R.id.ic_badge);
    if (reuse != null && reuse instanceof BadgeDrawable) {
        badge = (BadgeDrawable) reuse;
    } else {
        badge = new BadgeDrawable(context, color);
    }

    badge.setCount(count);
    icon.mutate();
    icon.setDrawableByLayerId(R.id.ic_badge, badge);
}

BadgeDrawable类:

 public class BadgeDrawable extends Drawable {

    private float mTextSize;
    private Paint mBadgePaint;
    private Paint mTextPaint;
    private Rect mTxtRect = new Rect();

    private String mCount = "";
    private boolean mWillDraw = false;

    public BadgeDrawable(Context context, int color) {
        mTextSize = context.getResources().getDimension(R.dimen.badge_text_size);

        mBadgePaint = new Paint();
        mBadgePaint.setColor(color);
        mBadgePaint.setAntiAlias(true);
        mBadgePaint.setStyle(Paint.Style.FILL);

        mTextPaint = new Paint();
        mTextPaint.setColor(Color.WHITE);
        mTextPaint.setTypeface(Typeface.DEFAULT_BOLD);
        mTextPaint.setTextSize(mTextSize);
        mTextPaint.setAntiAlias(true);
        mTextPaint.setTextAlign(Paint.Align.CENTER);
    }

    @Override
    public void draw(Canvas canvas) {
        if (!mWillDraw) {
            return;
        }

        Rect bounds = getBounds();
        float width = bounds.right - bounds.left;
        float height = bounds.bottom - bounds.top;

        // Position the badge in the top-right quadrant of the icon.
        float radius = ((Math.min(width, height) / 2) - 1) / 2;
        float centerX = width - radius - 1;
        float centerY = radius + 1;

        // Draw badge circle.
        canvas.drawCircle(centerX, centerY, radius, mBadgePaint);

        // Draw badge count text inside the circle.
        mTextPaint.getTextBounds(mCount, 0, mCount.length(), mTxtRect);
        float textHeight = mTxtRect.bottom - mTxtRect.top;
        float textY = centerY + (textHeight / 2f);
        canvas.drawText(mCount, centerX, textY, mTextPaint);
    }

    /*
     * Sets the count (i.e notifications) to display.
     */
    public void setCount(int count) {
        mCount = Integer.toString(count);

        // Only draw a badge if there are notifications.
        mWillDraw = count > 0;
        invalidateSelf();
    }

    @Override
    public void setAlpha(int alpha) {
        // do nothing
    }

    @Override
    public void setColorFilter(ColorFilter cf) {
        // do nothing
    }

    @Override
    public int getOpacity() {
        return PixelFormat.UNKNOWN;
    }
}

有什么想法吗?

1 个答案:

答案 0 :(得分:0)

使用throwable而不是Exception

try {
    Utils.setBadgeCount(this, ordersIcon, ordersCount, getResources().getColor(R.color.red_text));
    Utils.setBadgeCount(this, balancesIcon, balancesCount, getResources().getColor(R.color.green_text));
} catch (**Throwable** e) {
    Toast.makeText(this, "Dynamic menu icons not available on this device.", Toast.LENGTH_LONG).show();
}
相关问题