Java 调用自定义按钮类的构造函数时发生ClassCastException

Java 调用自定义按钮类的构造函数时发生ClassCastException,java,android,eclipse,button,Java,Android,Eclipse,Button,我正在玩一些简单的Android应用程序代码,但代码中的所有布局都有问题 这是我在eclipse中打开布局时的错误 The following classes could not be instantiated: - com.android2.calculator3.view.ColorButton (Open Class, Show Error Log) See the Error Log (Window > Show View) for more details. Tip: Use V

我正在玩一些简单的Android应用程序代码,但代码中的所有布局都有问题

这是我在eclipse中打开布局时的错误

The following classes could not be instantiated:
- com.android2.calculator3.view.ColorButton (Open Class, Show Error Log)
See the Error Log (Window > Show View) for more details.
Tip: Use View.isInEditMode() in your custom views to skip code when shown in Eclipse

java.lang.ClassCastException: com.android.layoutlib.bridge.android.BridgeContext cannot be cast to com.android2.calculator3.Calculator
at com.android2.calculator3.view.ColorButton.<init>(ColorButton.java:39)
at sun.reflect.NativeConstructorAccessorImpl.newInstance0(    at sun.reflect.NativeConstructorAccessorImpl.newInstance(    at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(    at java.lang.reflect.Constructor.newInstance(    at com.android.ide.eclipse.adt.internal.editors.layout.ProjectCallback.instantiateClass(ProjectCallback.java:442)
at com.android.ide.eclipse.adt.internal.editors.layout.ProjectCallback.loadView(ProjectCallback.java:194)
at android.view.BridgeInflater.loadCustomView(BridgeInflater.java:207)
at android.view.BridgeInflater.createViewFromTag(BridgeInflater.java:132)
at android.view.LayoutInflater.rInflate_Original(LayoutInflater.java:806)
at android.view.LayoutInflater_Delegate.rInflate(LayoutInflater_Delegate.java:64)
at android.view.LayoutInflater.rInflate(LayoutInflater.java:782)
at android.view.LayoutInflater.rInflate_Original(LayoutInflater.java:809)
at android.view.LayoutInflater_Delegate.rInflate(LayoutInflater_Delegate.java:64)
at android.view.LayoutInflater.rInflate(LayoutInflater.java:782)
at android.view.LayoutInflater.rInflate_Original(LayoutInflater.java:809)
at android.view.LayoutInflater_Delegate.rInflate(LayoutInflater_Delegate.java:64)
at android.view.LayoutInflater.rInflate(LayoutInflater.java:782)
at android.view.LayoutInflater.rInflate_Original(LayoutInflater.java:809)
at android.view.LayoutInflater_Delegate.rInflate(LayoutInflater_Delegate.java:64)
at android.view.LayoutInflater.rInflate(LayoutInflater.java:782)
at android.view.LayoutInflater.rInflate_Original(LayoutInflater.java:809)
at android.view.LayoutInflater_Delegate.rInflate(LayoutInflater_Delegate.java:64)
at android.view.LayoutInflater.rInflate(LayoutInflater.java:782)
at android.view.LayoutInflater.inflate(LayoutInflater.java:504)
at android.view.LayoutInflater.inflate(LayoutInflater.java:385)
无法实例化以下类:
-com.android2.calculator3.view.ColorButton(打开类,显示错误日志)
有关详细信息,请参阅错误日志(窗口>显示视图)。
提示:在自定义视图中使用View.isInEditMode()可以在Eclipse中显示时跳过代码
java.lang.ClassCastException:com.android.layoutlib.bridge.android.BridgeContext无法强制转换为com.android2.calculator3.Calculator
在com.android2.calculator3.view.ColorButton.(ColorButton.java:39)
位于sun.reflect.nativeconstructoraccessormpl.newInstance0(位于sun.reflect.nativeconstructoraccessormpl.newInstance(位于sun.reflect.DelegatingConstructorAccessorImpl.newInstance)(位于java.lang.reflect.Constructor.newInstance(位于com.android.ide.eclipse.adt.internal.editors.layout.ProjectCallback.instanceClass(ProjectCallback.java:442))
位于com.android.ide.eclipse.adt.internal.editors.layout.ProjectCallback.loadView(ProjectCallback.java:194)
在android.view.BridgeInflater.loadCustomView(BridgeInflater.java:207)中
在android.view.BridgeInflater.createViewFromTag(BridgeInflater.java:132)中
位于android.view.LayoutInflater.rInflate_Original(LayoutInflater.java:806)
在android.view.LayoutInflater\u Delegate.rInflate(LayoutInflater\u Delegate.java:64)
位于android.view.LayoutInflater.rInflate(LayoutInflater.java:782)
位于android.view.LayoutInflater.rInflate_Original(LayoutInflater.java:809)
在android.view.LayoutInflater\u Delegate.rInflate(LayoutInflater\u Delegate.java:64)
位于android.view.LayoutInflater.rInflate(LayoutInflater.java:782)
位于android.view.LayoutInflater.rInflate_Original(LayoutInflater.java:809)
在android.view.LayoutInflater\u Delegate.rInflate(LayoutInflater\u Delegate.java:64)
位于android.view.LayoutInflater.rInflate(LayoutInflater.java:782)
位于android.view.LayoutInflater.rInflate_Original(LayoutInflater.java:809)
在android.view.LayoutInflater\u Delegate.rInflate(LayoutInflater\u Delegate.java:64)
位于android.view.LayoutInflater.rInflate(LayoutInflater.java:782)
位于android.view.LayoutInflater.rInflate_Original(LayoutInflater.java:809)
在android.view.LayoutInflater\u Delegate.rInflate(LayoutInflater\u Delegate.java:64)
位于android.view.LayoutInflater.rInflate(LayoutInflater.java:782)
在android.view.LayoutInflater.充气(LayoutInflater.java:504)
在android.view.LayoutInflater.充气(LayoutInflater.java:385)
这是我在Colorbutton.java中的代码

 package com.android2.calculator3.view;

 import java.util.regex.Pattern;

 import android.content.Context;
 import android.content.res.Resources;
 import android.graphics.Canvas;
 import android.graphics.Paint;
 import android.graphics.Paint.Style;
 import android.graphics.Rect;
 import android.util.AttributeSet;
 import android.view.MotionEvent;
 import android.widget.Button;

 import com.android2.calculator3.Calculator;
 import com.android2.calculator3.EventListener;
 import calculator.app.R;

 /**
 * Button with click-animation effect.
 */
class ColorButton extends Button {
int CLICK_FEEDBACK_COLOR;
static final int CLICK_FEEDBACK_INTERVAL = 10;
static final int CLICK_FEEDBACK_DURATION = 350;

float mTextX;
float mTextY;
long mAnimStart;
EventListener mListener;
Paint mFeedbackPaint;
Paint mHintPaint = new Paint();
Rect bounds = new Rect();
float mTextSize = 0f;

public ColorButton(Context context, AttributeSet attrs) {
    super(context, attrs);
    Calculator calc = (Calculator) context;
    init(calc);
    mListener = calc.mListener;
    setOnClickListener(mListener);
    setOnLongClickListener(mListener);
}

private void init(Calculator calc) {
    Resources res = getResources();

    CLICK_FEEDBACK_COLOR = res.getColor(R.color.magic_flame);
    mFeedbackPaint = new Paint();
    mFeedbackPaint.setStyle(Style.STROKE);
    mFeedbackPaint.setStrokeWidth(2);
    getPaint().setColor(res.getColor(R.color.button_text));
    mHintPaint.setColor(res.getColor(R.color.button_hint_text));

    mAnimStart = -1;
}

private void layoutText() {
    Paint paint = getPaint();
    if(mTextSize != 0f) paint.setTextSize(mTextSize);
    float textWidth = paint.measureText(getText().toString());
    float width = getWidth() - getPaddingLeft() - getPaddingRight();
    float textSize = getTextSize();
    if(textWidth > width) {
        paint.setTextSize(textSize * width / textWidth);
        mTextX = getPaddingLeft();
        mTextSize = textSize;
    }
    else {
        mTextX = (getWidth() - textWidth) / 2;
    }
    mTextY = (getHeight() - paint.ascent() - paint.descent()) / 2;
    if(mHintPaint != null) mHintPaint.setTextSize(paint.getTextSize() * 0.8f);
}

@Override
protected void onTextChanged(CharSequence text, int start, int before, int after) {
    layoutText();
}

@Override
protected void onLayout(boolean changed, int left, int top, int right, int bottom) {
    super.onLayout(changed, left, top, right, bottom);
    if(changed) layoutText();
}

private void drawMagicFlame(int duration, Canvas canvas) {
    int alpha = 255 - 255 * duration / CLICK_FEEDBACK_DURATION;
    int color = CLICK_FEEDBACK_COLOR | (alpha << 24);

    mFeedbackPaint.setColor(color);
    canvas.drawRect(1, 1, getWidth() - 1, getHeight() - 1, mFeedbackPaint);
}

@Override
public void onDraw(Canvas canvas) {
    if(mAnimStart != -1) {
        int animDuration = (int) (System.currentTimeMillis() - mAnimStart);

        if(animDuration >= CLICK_FEEDBACK_DURATION) {
            mAnimStart = -1;
        }
        else {
            drawMagicFlame(animDuration, canvas);
            postInvalidateDelayed(CLICK_FEEDBACK_INTERVAL);
        }
    }
    else if(isPressed()) {
        drawMagicFlame(0, canvas);
    }

    CharSequence hint = getHint();
    if(hint != null) {
        String[] exponents = hint.toString().split(Pattern.quote("^"));
        int offsetX = getContext().getResources().getDimensionPixelSize(R.dimen.button_hint_offset_x);
        int offsetY = (int) ((mTextY + getContext().getResources().getDimensionPixelSize(R.dimen.button_hint_offset_y) - getTextHeight(mHintPaint,
                hint.toString())) / 2)
                - getPaddingTop();

        float textWidth = mHintPaint.measureText(hint.toString());
        float width = getWidth() - getPaddingLeft() - getPaddingRight() - mTextX - offsetX;
        float textSize = mHintPaint.getTextSize();
        if(textWidth > width) {
            mHintPaint.setTextSize(textSize * width / textWidth);
        }

        for(String str : exponents) {
            if(str == exponents[0]) {
                canvas.drawText(str, 0, str.length(), mTextX + offsetX, mTextY - offsetY, mHintPaint);
                offsetY += getContext().getResources().getDimensionPixelSize(R.dimen.button_hint_exponent_jump);
                offsetX += mHintPaint.measureText(str);
            }
            else {
                canvas.drawText(str, 0, str.length(), mTextX + offsetX, mTextY - offsetY, mHintPaint);
                offsetY += getContext().getResources().getDimensionPixelSize(R.dimen.button_hint_exponent_jump);
                offsetX += mHintPaint.measureText(str);
            }
        }
    }

    CharSequence text = getText();
    canvas.drawText(text, 0, text.length(), mTextX, mTextY, getPaint());
}

private int getTextHeight(Paint paint, String text) {
    mHintPaint.getTextBounds(text, 0, text.length(), bounds);
    int height = bounds.height();
    String[] exponents = text.split(Pattern.quote("^"));
    for(int i = 1; i < exponents.length; i++) {
        height += getContext().getResources().getDimensionPixelSize(R.dimen.button_hint_exponent_jump);
    }
    return height;
}

public void animateClickFeedback() {
    mAnimStart = System.currentTimeMillis();
    invalidate();
}

@Override
public boolean onTouchEvent(MotionEvent event) {
    boolean result = super.onTouchEvent(event);

    switch(event.getAction()) {
    case MotionEvent.ACTION_UP:
        if(isPressed()) {
            animateClickFeedback();
        }
        else {
            invalidate();
        }
        break;
    case MotionEvent.ACTION_DOWN:
    case MotionEvent.ACTION_CANCEL:
        mAnimStart = -1;
        invalidate();
        break;
    }

    return result;
}
}
package com.android2.calculator3.view;
导入java.util.regex.Pattern;
导入android.content.Context;
导入android.content.res.Resources;
导入android.graphics.Canvas;
导入android.graphics.Paint;
导入android.graphics.Paint.Style;
导入android.graphics.Rect;
导入android.util.AttributeSet;
导入android.view.MotionEvent;
导入android.widget.Button;
导入com.android2.Calculator或3.Calculator;
导入com.android2.calculator3.EventListener;
导入calculator.app.R;
/**
*具有单击动画效果的按钮。
*/
类ColorButton扩展按钮{
int点击反馈颜色;
静态最终整数点击反馈间隔=10;
静态最终点击时间=350;
浮动mTextX;
浮动mTextY;
长曼尼姆斯塔特;
事件监听器;
涂料;涂料;
油漆mHintPaint=新油漆();
Rect bounds=new Rect();
浮动mTextSize=0f;
公共颜色按钮(上下文、属性集属性){
超级(上下文,attrs);
计算器calc=(计算器)上下文;
初始(计算);
mListener=计算mListener;
setOnClickListener(MLListener);
设置LongClickListener(MLListener);
}
私有void init(计算器计算){
Resources res=getResources();
单击“反馈”\u COLOR=res.getColor(R.COLOR.magic\u flame);
mFeedbackPaint=新油漆();
mFeedbackPaint.setStyle(Style.STROKE);
mFeedbackPaint.设置行程宽度(2);
getPaint().setColor(res.getColor(R.color.button_text));
mHintPaint.setColor(res.getColor(R.color.button_hint_text));
mAnimStart=-1;
}
私有void layoutText(){
Paint-Paint=getPaint();
如果(mTextSize!=0f)paint.setTextSize(mTextSize);
float textWidth=paint.measureText(getText().toString());
浮动宽度=getWidth()-getPaddingLeft()-getPaddingRight();
float textSize=getTextSize();
如果(文本宽度>宽度){
paint.setTextSize(textSize*width/textWidth);
mTextX=getPaddingLeft();
mTextSize=textSize;
}
否则{
mTextX=(getWidth()-textWidth)/2;
}
mTextY=(getHeight()-paint.ascent()-paint.descent())/2;
如果(mHintPaint!=null)mHintPaint.setTextSize(paint.getTextSize()*0.8f);
}
@凌驾
受保护的void onTextChanged(字符序列文本、int-start、int-before、int-after){
layoutText();
}
@凌驾
仅限受保护的空心布局(布尔值已更改、整数左侧、整数顶部、整数右侧、整数底部){
超级。仅限布局(已更改、左、上、右、下);
如果(更改)layoutText();
}
私有void drawMagicFlame(整数持续时间,画布){
int alpha=255-255*持续时间/单击“反馈”持续时间;
int color=点击反馈颜色(alpha=点击反馈持续时间){
mAnimStart=-1;
}
否则{
drawMagicFlame(动画、画布);
postInvalidateDelayed(单击反馈间隔);
}
}
else if(isPressed()){
drawMagicFlame(0,画布);
}
CharSequence hint=getHint();
if(提示!=null){
String[]exponents=hint.toString().split(Pattern.quote(“^”);
int offsetX=getContext().getResources().getDimensionPixelSize(R.dimen.button\u hint\u offset\u x);
int offsetY=(int)((mTextY+getContext().getResources().getDimensionPixelSize(R.dimen.button\u hint\u offset\u y)-getTextHeight(mHintPaint,
hint.toString())/2)
-getPaddingTop();
浮动文本宽度=mHintPaint。
java.lang.ClassCastException: com.android.layoutlib.bridge.android.BridgeContext cannot be cast to com.android2.calculator3.Calculator
at com.android2.calculator3.view.ColorButton.<init>(ColorButton.java:39)
public ColorButton(Context context, AttributeSet attrs) {
    super(context, attrs);
    Calculator calc = (Calculator) context; //This Line
    init(calc);
    mListener = calc.mListener;
    setOnClickListener(mListener);
    setOnLongClickListener(mListener);
}
if (context instanceof Calculator) {
    Calculator calc = (Calculator) context;
} else {
    Log.e("Log Tag", context.toString() + " must inherit from Calculator class");
}
try {
    Calculator calc = (Calculator) context;
} catch (ClassCastException e) {
    Log.e("Log Tag", context.toString() + " must inherit from Calculator class");
    e.printStackTrace();
}
public ColorButton(Context context, AttributeSet attrs, Caculator calculator) {
    super(context, attrs);
    Calculator calc = calculator;
    init(calc);
    mListener = calc.mListener;
    setOnClickListener(mListener);
    setOnLongClickListener(mListener);
}