我创建了一个自定义视图,显示圆弧是否符合圆圈。我的目标是在我的xml中显示视图。我试图在我的xml中为我的自定义视图类充气,但它给了我错误。我的目标是在framelayout中显示我的主要活动。我发布了我的代码以及错误报告。请看一下。如果有任何其他方法,请告诉我。 感谢
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)
答案 0 :(得分:2)
你的类需要使用Context和AttributeSet参数,并将它放在一个单独的编译单元
中答案 1 :(得分:0)
在cusImage类中尝试以下代码。
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
// TODO Auto-generated method stub
super.onMeasure(widthMeasureSpec, heightMeasureSpec);
invalidate();
}
并从onDraw方法中删除invalidate()。