在android中设置可绘制边界时出错

时间:2014-10-09 05:59:43

标签: android canvas drawable

创建自定义视图并在其上绘制不同的形状,当我尝试将自定义视图放置在customview上的布局形状时与剩余屏幕重叠,有时候应用程序崩溃与空指针异常。异常导致bcz的边界可绘制但我没有得到如何修复请检查我的代码

public void customDraw(Canvas canvas) {
        canvas.save();
        float dx = (maxX + minX) / 2;
        float dy = (maxY + minY) / 2;
        drawable.setBounds((int) minX, (int) minY, (int) maxX, (int) maxY);
        //drawable.setBounds(50, 50, 50, 50);
        canvas.translate(dx, dy);
        canvas.rotate(angle * 180.0f / (float) Math.PI);
        canvas.translate(-dx, -dy);
        drawable.draw(canvas);
        canvas.restore();
    }

此处我附上了我的自定义视图总课程custonView file

这里是stacktrace

    10-09 11:38:19.142: E/AndroidRuntime(3673): FATAL EXCEPTION: main
10-09 11:38:19.142: E/AndroidRuntime(3673): java.lang.NullPointerException
10-09 11:38:19.142: E/AndroidRuntime(3673):     at org.metalev.multitouch.photosortr.PhotoSortrView$Img.draw(PhotoSortrView.java:316)
10-09 11:38:19.142: E/AndroidRuntime(3673):     at org.metalev.multitouch.photosortr.PhotoSortrView.onDraw(PhotoSortrView.java:124)
10-09 11:38:19.142: E/AndroidRuntime(3673):     at android.view.View.draw(View.java:13707)
10-09 11:38:19.142: E/AndroidRuntime(3673):     at android.view.View.draw(View.java:13591)
10-09 11:38:19.142: E/AndroidRuntime(3673):     at android.view.ViewGroup.drawChild(ViewGroup.java:2928)
10-09 11:38:19.142: E/AndroidRuntime(3673):     at android.view.ViewGroup.dispatchDraw(ViewGroup.java:2797)
10-09 11:38:19.142: E/AndroidRuntime(3673):     at android.view.View.draw(View.java:13589)
10-09 11:38:19.142: E/AndroidRuntime(3673):     at android.view.ViewGroup.drawChild(ViewGroup.java:2928)
10-09 11:38:19.142: E/AndroidRuntime(3673):     at android.view.ViewGroup.dispatchDraw(ViewGroup.java:2797)
10-09 11:38:19.142: E/AndroidRuntime(3673):     at android.view.View.draw(View.java:13589)
10-09 11:38:19.142: E/AndroidRuntime(3673):     at android.view.ViewGroup.drawChild(ViewGroup.java:2928)
10-09 11:38:19.142: E/AndroidRuntime(3673):     at android.view.ViewGroup.dispatchDraw(ViewGroup.java:2797)
10-09 11:38:19.142: E/AndroidRuntime(3673):     at android.view.View.draw(View.java:13589)
10-09 11:38:19.142: E/AndroidRuntime(3673):     at android.view.ViewGroup.drawChild(ViewGroup.java:2928)
10-09 11:38:19.142: E/AndroidRuntime(3673):     at android.view.ViewGroup.dispatchDraw(ViewGroup.java:2797)
10-09 11:38:19.142: E/AndroidRuntime(3673):     at android.view.View.draw(View.java:13710)
10-09 11:38:19.142: E/AndroidRuntime(3673):     at android.widget.FrameLayout.draw(FrameLayout.java:467)
10-09 11:38:19.142: E/AndroidRuntime(3673):     at com.android.internal.policy.impl.PhoneWindow$DecorView.draw(PhoneWindow.java:2211)
10-09 11:38:19.142: E/AndroidRuntime(3673):     at android.view.ViewRootImpl.drawSoftware(ViewRootImpl.java:2281)
10-09 11:38:19.142: E/AndroidRuntime(3673):     at android.view.ViewRootImpl.draw(ViewRootImpl.java:2177)
10-09 11:38:19.142: E/AndroidRuntime(3673):     at android.view.ViewRootImpl.performDraw(ViewRootImpl.java:2045)
10-09 11:38:19.142: E/AndroidRuntime(3673):     at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:1854)
10-09 11:38:19.142: E/AndroidRuntime(3673):     at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:989)
10-09 11:38:19.142: E/AndroidRuntime(3673):     at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:4351)
10-09 11:38:19.142: E/AndroidRuntime(3673):     at android.view.Choreographer$CallbackRecord.run(Choreographer.java:749)
10-09 11:38:19.142: E/AndroidRuntime(3673):     at android.view.Choreographer.doCallbacks(Choreographer.java:562)
10-09 11:38:19.142: E/AndroidRuntime(3673):     at android.view.Choreographer.doFrame(Choreographer.java:532)
10-09 11:38:19.142: E/AndroidRuntime(3673):     at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:735)
10-09 11:38:19.142: E/AndroidRuntime(3673):     at android.os.Handler.handleCallback(Handler.java:725)
10-09 11:38:19.142: E/AndroidRuntime(3673):     at android.os.Handler.dispatchMessage(Handler.java:92)
10-09 11:38:19.142: E/AndroidRuntime(3673):     at android.os.Looper.loop(Looper.java:137)
10-09 11:38:19.142: E/AndroidRuntime(3673):     at android.app.ActivityThread.main(ActivityThread.java:5039)
10-09 11:38:19.142: E/AndroidRuntime(3673):     at java.lang.reflect.Method.invokeNative(Native Method)
10-09 11:38:19.142: E/AndroidRuntime(3673):     at java.lang.reflect.Method.invoke(Method.java:511)
10-09 11:38:19.142: E/AndroidRuntime(3673):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
10-09 11:38:19.142: E/AndroidRuntime(3673):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
10-09 11:38:19.142: E/AndroidRuntime(3673):     at dalvik.system.NativeStart.main(Native Method)

0 个答案:

没有答案