0

我创建了一个自定义视图,该视图显示了一个圆弧组成的圆。我的目标是在我的 xml 中显示视图。我试图在我的 xml 中扩充我的自定义视图类,但它给了我错误。我的目标是在框架布局中的主要活动中显示它。我正在发布我的代码以及错误报告。请看一看。如果有任何其他方法,请告诉我。谢谢

    public class CusImage extends View {
    private Paint myPaint;
    private Paint myPaint2;
    private Paint myFramePaint;
    private RectF bigOval;
    public TextView value;
    private RectF bigOval2;
    private float myStart;
    private float mySweep;
    private float SWEEP_INC = 3;
    private float SWEEP_INC2 = 5;
    // Use this flag to control the direction of the arc's movement
    private boolean addToCircle = false;

    public CusImage(Context context, AttributeSet attrs) {
        super(context, attrs);
        if (isInEditMode()) {
            init();
        }
    }


    private void init() {
        myPaint = new Paint();

        myPaint.setAntiAlias(true);

        myPaint.setStyle(Paint.Style.STROKE);

        myPaint.setColor(Color.BLUE);

        myPaint.setStrokeWidth(10);

        bigOval = new RectF(40, 10, 280, 250);

        myFramePaint = new Paint();
        myFramePaint.setAntiAlias(true);
        myFramePaint.setColor(Color.TRANSPARENT);
    }

    @Override
    protected void onDraw(Canvas canvas) {

        // value = (TextView) findViewById(R.id.txt);
        canvas.drawRect(bigOval, myFramePaint);
        canvas.drawArc(bigOval, myStart, mySweep, false, myPaint);
        myStart = -90;

        mySweep += SWEEP_INC2;

        invalidate();
    }
}

这就是我试图在我的 xml 中膨胀它的方式:

<com.example.progressframework.MainActivity.CusImage
     android:layout_width="wrap_content"
     android:layout_height="wrap_content" >

 </com.example.progressframework.MainActivity.CusImage>

以下是最新的错误:

 Exception raised during rendering: loader 
 (instance of com/android/ide/eclipse/adt/internal/resources/
 manager/ProjectClassLoader): attempted  duplicate class definition for name:     
  "com/example/progressframework/MainActivity$CusImage"
  Exception details are logged in Window > Show View > Error Log

和 Logcat:

    08-26 12:26:33.702: E/AndroidRuntime(6428): FATAL EXCEPTION: main
08-26 12:26:33.702: E/AndroidRuntime(6428): java.lang.NullPointerException
08-26 12:26:33.702: E/AndroidRuntime(6428):     at android.view.GLES20Canvas.drawRect(GLES20Canvas.java:1136)
08-26 12:26:33.702: E/AndroidRuntime(6428):     at com.example.progressframework.CusImage.onDraw(CusImage.java:60)
08-26 12:26:33.702: E/AndroidRuntime(6428):     at android.view.View.draw(View.java:13944)
08-26 12:26:33.702: E/AndroidRuntime(6428):     at android.view.View.getDisplayList(View.java:12838)
08-26 12:26:33.702: E/AndroidRuntime(6428):     at android.view.View.getDisplayList(View.java:12880)
08-26 12:26:33.702: E/AndroidRuntime(6428):     at android.view.View.draw(View.java:13657)
08-26 12:26:33.702: E/AndroidRuntime(6428):     at android.view.ViewGroup.drawChild(ViewGroup.java:3083)
08-26 12:26:33.702: E/AndroidRuntime(6428):     at android.view.ViewGroup.dispatchDraw(ViewGroup.java:2920)
08-26 12:26:33.702: E/AndroidRuntime(6428):     at android.view.View.getDisplayList(View.java:12833)    
08-26 12:26:33.702: E/AndroidRuntime(6428):     at android.view.View.getDisplayList(View.java:12880)
08-26 12:26:33.702: E/AndroidRuntime(6428):     at android.view.View.draw(View.java:13657)
08-26 12:26:33.702: E/AndroidRuntime(6428):     at android.view.ViewGroup.drawChild(ViewGroup.java:3083)
08-26 12:26:33.702: E/AndroidRuntime(6428):     at android.view.ViewGroup.dispatchDraw(ViewGroup.java:2920)
08-26 12:26:33.702: E/AndroidRuntime(6428):     at android.view.View.getDisplayList(View.java:12833)
08-26 12:26:33.702: E/AndroidRuntime(6428):     at android.view.View.getDisplayList(View.java:12880)
08-26 12:26:33.702: E/AndroidRuntime(6428):     at android.view.View.draw(View.java:13657)
08-26 12:26:33.702: E/AndroidRuntime(6428):     at android.view.ViewGroup.drawChild(ViewGroup.java:3083)
08-26 12:26:33.702: E/AndroidRuntime(6428):     at android.view.ViewGroup.dispatchDraw(ViewGroup.java:2920)
08-26 12:26:33.702: E/AndroidRuntime(6428):     at android.view.View.getDisplayList(View.java:12833)
08-26 12:26:33.702: E/AndroidRuntime(6428):     at android.view.View.getDisplayList(View.java:12880)
08-26 12:26:33.702: E/AndroidRuntime(6428):     at android.view.View.draw(View.java:13657)
08-26 12:26:33.702: E/AndroidRuntime(6428):     at android.view.ViewGroup.drawChild(ViewGroup.java:3083)
08-26 12:26:33.702: E/AndroidRuntime(6428):     at android.view.ViewGroup.dispatchDraw(ViewGroup.java:2920)
08-26 12:26:33.702: E/AndroidRuntime(6428):     at android.view.View.draw(View.java:13947)
08-26 12:26:33.702: E/AndroidRuntime(6428):     at android.widget.FrameLayout.draw(FrameLayout.java:467)
08-26 12:26:33.702: E/AndroidRuntime(6428):     at com.android.internal.policy.impl.PhoneWindow$DecorView.draw(PhoneWindow.java:2224)
08-26 12:26:33.702: E/AndroidRuntime(6428):     at android.view.View.getDisplayList(View.java:12838)
08-26 12:26:33.702: E/AndroidRuntime(6428):     at android.view.View.getDisplayList(View.java:12880)
08-26 12:26:33.702: E/AndroidRuntime(6428):     at android.view.HardwareRenderer$GlRenderer.buildDisplayList(HardwareRenderer.java:1411)
08-26 12:26:33.702: E/AndroidRuntime(6428):     at android.view.HardwareRenderer$GlRenderer.draw(HardwareRenderer.java:1359)
08-26 12:26:33.702: E/AndroidRuntime(6428):     at android.view.ViewRootImpl.draw(ViewRootImpl.java:2367)
08-26 12:26:33.702: E/AndroidRuntime(6428):     at android.view.ViewRootImpl.performDraw(ViewRootImpl.java:2239)
08-26 12:26:33.702: E/AndroidRuntime(6428):     at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:1872)
08-26 12:26:33.702: E/AndroidRuntime(6428):     at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:1004)
08-26 12:26:33.702: E/AndroidRuntime(6428):     at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:5481)
08-26 12:26:33.702: E/AndroidRuntime(6428):     at android.view.Choreographer$CallbackRecord.run(Choreographer.java:749)
08-26 12:26:33.702: E/AndroidRuntime(6428):     at android.view.Choreographer.doCallbacks(Choreographer.java:562)
08-26 12:26:33.702: E/AndroidRuntime(6428):     at android.view.Choreographer.doFrame(Choreographer.java:532)
08-26 12:26:33.702: E/AndroidRuntime(6428):     at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:735)
08-26 12:26:33.702: E/AndroidRuntime(6428):     at android.os.Handler.handleCallback(Handler.java:730)
08-26 12:26:33.702: E/AndroidRuntime(6428):     at android.os.Handler.dispatchMessage(Handler.java:92)
08-26 12:26:33.702: E/AndroidRuntime(6428):     at android.os.Looper.loop(Looper.java:137)
08-26 12:26:33.702: E/AndroidRuntime(6428):     at android.app.ActivityThread.main(ActivityThread.java:5103)
08-26 12:26:33.702: E/AndroidRuntime(6428):     at java.lang.reflect.Method.invokeNative(Native Method)
08-26 12:26:33.702: E/AndroidRuntime(6428):     at java.lang.reflect.Method.invoke(Method.java:525)
08-26 12:26:33.702: E/AndroidRuntime(6428):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:737)
08-26 12:26:33.702: E/AndroidRuntime(6428):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
08-26 12:26:33.702: E/AndroidRuntime(6428):     at dalvik.system.NativeStart.main(Native Method)
4

2 回答 2

2

您的类需要具有 Context 和 AttributeSet 参数的 ctor,也将其放在单独的编译单元中

于 2013-08-26T06:59:30.610 回答
0

在您的 cusImage 类中尝试以下代码。

protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
    // TODO Auto-generated method stub
    super.onMeasure(widthMeasureSpec, heightMeasureSpec);
    invalidate();

}

并从 onDraw 方法中删除 invalidate() 。

于 2013-08-26T07:25:26.477 回答