错误:宽度和高度必须> 0

时间:2015-08-26 17:35:32

标签: java android eclipse

如果用户向左或向右滑动(图形计算器,十六进制,矩阵......),我正在使用一些计算器源代码和应用程​​序有不同的视图但是当我刷我的应用程序时遇到此错误:

FATAL EXCEPTION: main
Process: calculator.app, PID: 15758
java.lang.IllegalArgumentException: width and height must be > 0
at android.graphics.Bitmap.createBitmap(Bitmap.java:933)
at android.graphics.Bitmap.createBitmap(Bitmap.java:912)
at android.graphics.Bitmap.createBitmap(Bitmap.java:879)
at com.android2.calculator3.view.Cling.dispatchDraw(Cling.java:117)
at android.view.View.updateDisplayListIfDirty(View.java:15134)
at android.view.View.getDisplayList(View.java:15162)
at android.view.ViewGroup.recreateChildDisplayList(ViewGroup.java:3687)
at android.view.ViewGroup.dispatchGetDisplayList(ViewGroup.java:3666)
at android.view.View.updateDisplayListIfDirty(View.java:15099)
at android.view.View.getDisplayList(View.java:15162)
at android.view.ViewGroup.recreateChildDisplayList(ViewGroup.java:3687)
at android.view.ViewGroup.dispatchGetDisplayList(ViewGroup.java:3666)
at android.view.View.updateDisplayListIfDirty(View.java:15099)
at android.view.View.getDisplayList(View.java:15162)
at android.view.ViewGroup.recreateChildDisplayList(ViewGroup.java:3687)
at android.view.ViewGroup.dispatchGetDisplayList(ViewGroup.java:3666)
at android.view.View.updateDisplayListIfDirty(View.java:15099)
at android.view.View.getDisplayList(View.java:15162)
at android.view.ViewGroup.recreateChildDisplayList(ViewGroup.java:3687)
at android.view.ViewGroup.dispatchGetDisplayList(ViewGroup.java:3666)
at android.view.View.updateDisplayListIfDirty(View.java:15099)
at android.view.View.getDisplayList(View.java:15162)
at android.view.ThreadedRenderer.updateViewTreeDisplayList(ThreadedRenderer.java:275)
at android.view.ThreadedRenderer.updateRootDisplayList(ThreadedRenderer.java:281)
at android.view.ThreadedRenderer.draw(ThreadedRenderer.java:320)
at android.view.ViewRootImpl.draw(ViewRootImpl.java:2751)
at android.view.ViewRootImpl.performDraw(ViewRootImpl.java:2584)
at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:2176)
at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:1191)
at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:6642)
at android.view.Choreographer$CallbackRecord.run(Choreographer.java:777)
at android.view.Choreographer.doCallbacks(Choreographer.java:590)
at android.view.Choreographer.doFrame(Choreographer.java:560)
at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:763)
at android.os.Handler.handleCallback(Handler.java:739)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:145)
at android.app.ActivityThread.main(ActivityThread.java:5942)
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(Method.java:372)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1400)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1195)

这是来自Cling.java类的代码

public class Cling extends FrameLayout {
public static final int SHOW_CLING_DURATION = 600;
public static final int DISMISS_CLING_DURATION = 250;

public static final String SIMPLE_CLING_DISMISSED_KEY = "cling.simple.dismissed";
public static final String MATRIX_CLING_DISMISSED_KEY = "cling.matrix.dismissed";
public static final String HEX_CLING_DISMISSED_KEY = "cling.hex.dismissed";
public static final String GRAPH_CLING_DISMISSED_KEY = "cling.graph.dismissed";

private Calculator mCalculator;
private boolean mIsInitialized;
private Drawable mBackground;
private Drawable mPunchThroughGraphic;
private Drawable mHandTouchGraphic;
private int mPunchThroughGraphicCenterRadius;
private float mRevealRadius;
private int[] mPositionData;
private boolean mShowHand;
private boolean mDismissed;

private Paint mErasePaint;

public Cling(Context context) {
    this(context, null, 0);
}

public Cling(Context context, AttributeSet attrs) {
    this(context, attrs, 0);
}

public Cling(Context context, AttributeSet attrs, int defStyle) {
    super(context, attrs, defStyle);
}

public void init(Calculator c, int[] positionData, float revealRadius, boolean showHand) {
    if(!mIsInitialized) {
        mCalculator = c;
        mPositionData = positionData;
        mShowHand = showHand;
        mDismissed = false;

        Resources r = getContext().getResources();
        mPunchThroughGraphic = r.getDrawable(R.drawable.cling);
        mPunchThroughGraphicCenterRadius = r.getDimensionPixelSize(R.dimen.clingPunchThroughGraphicCenterRadius);
        mRevealRadius = revealRadius;

        mErasePaint = new Paint();
        mErasePaint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.MULTIPLY));
        mErasePaint.setColor(0xFFFFFF);
        mErasePaint.setAlpha(0);

        mIsInitialized = true;
    }
}

public void dismiss() {
    mDismissed = true;
}

boolean isDismissed() {
    return mDismissed;
}

public void cleanup() {
    mBackground = null;
    mPunchThroughGraphic = null;
    mHandTouchGraphic = null;
    mIsInitialized = false;
}

private int[] getPunchThroughPosition() {
    if(mPositionData != null) {
        return mPositionData;
    }
    return new int[] { -1, -1, -1 };
}

@Override
public boolean onTouchEvent(android.view.MotionEvent event) {
    int[] pos = getPunchThroughPosition();
    double diff = Math.sqrt(Math.pow(event.getX() - pos[0], 2) + Math.pow(event.getY() - pos[1], 2));
    if(diff < mRevealRadius) {
        return false;
    }
    return true;
};

@Override
protected void dispatchDraw(Canvas canvas) {
    if(mIsInitialized) {
        DisplayMetrics metrics = new DisplayMetrics();
        mCalculator.getWindowManager().getDefaultDisplay().getMetrics(metrics);

        // Initialize the draw buffer (to allow punching through)
        Bitmap b = Bitmap.createBitmap(getMeasuredWidth(), getMeasuredHeight(), Bitmap.Config.ARGB_8888);
        Canvas c = new Canvas(b);

        // Draw the background
        if(mBackground == null) {
            mBackground = getResources().getDrawable(R.drawable.bg_cling);
        }
        if(mBackground != null) {
            mBackground.setBounds(0, 0, getMeasuredWidth(), getMeasuredHeight());
            mBackground.draw(c);
        }
        else {
            c.drawColor(0x99000000);
        }

        int cx = -1;
        int cy = -1;
        int cz = -1;
        float scale = mRevealRadius / mPunchThroughGraphicCenterRadius;
        int dw = (int) (scale * mPunchThroughGraphic.getIntrinsicWidth());
        int dh = (int) (scale * mPunchThroughGraphic.getIntrinsicHeight());

        // Determine where to draw the punch through graphic
        Rect rect = new Rect();
        Window window = ((Activity) getContext()).getWindow();
        window.getDecorView().getWindowVisibleDisplayFrame(rect);
        int statusBarHeight = rect.top;
        int[] pos = getPunchThroughPosition();
        cx = pos[0];
        cy = pos[1] - statusBarHeight;
        cz = pos[2];
        if(cx > -1 && cy > -1 && scale > 0) {
            c.drawCircle(cx, cy, mRevealRadius, mErasePaint);
            mPunchThroughGraphic.setBounds(cx - dw / 2, cy - dh / 2, cx + dw / 2, cy + dh / 2);
            mPunchThroughGraphic.draw(c);
        }

        // Draw the hand graphic
        if(mShowHand) {
            if(mHandTouchGraphic == null) {
                mHandTouchGraphic = getResources().getDrawable(R.drawable.hand);
            }
            int offset = cz;
            mHandTouchGraphic.setBounds(cx + offset, cy + offset, cx + mHandTouchGraphic.getIntrinsicWidth() + offset,
                    cy + mHandTouchGraphic.getIntrinsicHeight() + offset);
            mHandTouchGraphic.draw(c);
        }

        canvas.drawBitmap(b, 0, 0, null);
        c.setBitmap(null);
        b = null;
    }

    // Draw the rest of the cling
    super.dispatchDraw(canvas);
};

}

我在编程方面是初学者,但如果我理解了这一行中出错的话

Bitmap b = Bitmap.createBitmap(getMeasuredWidth(), getMeasuredHeight(), Bitmap.Config.ARGB_8888);

我正在尝试实施此方法:

public void run() {
    // display ShowcaseView here
}

但没有任何成功。我不知道该怎么办?任何建议,请完全初学者:)

1 个答案:

答案 0 :(得分:0)

尝试插入:

Log.i("Width: ", Integer.toString(getMeasuredWidth()));
Log.i("Height: ", Integer.toString(getMeasuredHeight()));

就在你的行前:

Bitmap b = Bitmap.createBitmap(getMeasuredWidth(), getMeasuredHeight(), Bitmap.Config.ARGB_8888);

这样,您可以检查getMeasuredWidth()getMeasuredHeight()的值,以确认它们等于或小于零,然后继续追踪原因。

希望这有帮助,祝你好运!