android.widget.ProgressBar中没有可用的默认构造函数

时间:2016-08-01 16:24:43

标签: java android

首先我是android的初学者。 我想创建一个用于处理进度条的类并设置一些动画操作。 不幸, 系统显示标题中提到的错误。 这是我的代码:

import android.graphics.Canvas;
import android.widget.ProgressBar;

public class TexPtrogressBar extends ProgressBar {
    @Override
    protected synchronized void onDraw(Canvas canvas) {
        super.onDraw(canvas);
    }
} 

错误发生在第三行。 怎么了?感谢

3 个答案:

答案 0 :(得分:1)

ProgressBar类代码中,如果你继承它,你必须提供至少一个构造函数

 public ProgressBar(Context context, AttributeSet attrs) {
    this(context, attrs, com.android.internal.R.attr.progressBarStyle);
}

public ProgressBar(Context context, AttributeSet attrs, int defStyleAttr) {
    this(context, attrs, defStyleAttr, 0);
}

public ProgressBar(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes) {
    super(context, attrs, defStyleAttr, defStyleRes);

    mUiThreadId = Thread.currentThread().getId();
    initProgressBar();

    final TypedArray a = context.obtainStyledAttributes(
            attrs, R.styleable.ProgressBar, defStyleAttr, defStyleRes);

    mNoInvalidate = true;

    final Drawable progressDrawable = a.getDrawable(R.styleable.ProgressBar_progressDrawable);
    if (progressDrawable != null) {
        // Calling this method can set mMaxHeight, make sure the corresponding
        // XML attribute for mMaxHeight is read after calling this method
        setProgressDrawableTiled(progressDrawable);
    }


    mDuration = a.getInt(R.styleable.ProgressBar_indeterminateDuration, mDuration);

    mMinWidth = a.getDimensionPixelSize(R.styleable.ProgressBar_minWidth, mMinWidth);
    mMaxWidth = a.getDimensionPixelSize(R.styleable.ProgressBar_maxWidth, mMaxWidth);
    mMinHeight = a.getDimensionPixelSize(R.styleable.ProgressBar_minHeight, mMinHeight);
    mMaxHeight = a.getDimensionPixelSize(R.styleable.ProgressBar_maxHeight, mMaxHeight);

    mBehavior = a.getInt(R.styleable.ProgressBar_indeterminateBehavior, mBehavior);

    final int resID = a.getResourceId(
            com.android.internal.R.styleable.ProgressBar_interpolator, 
            android.R.anim.linear_interpolator); // default to linear interpolator
    if (resID > 0) {
        setInterpolator(context, resID);
    } 

    setMax(a.getInt(R.styleable.ProgressBar_max, mMax));

    setProgress(a.getInt(R.styleable.ProgressBar_progress, mProgress));

    setSecondaryProgress(
            a.getInt(R.styleable.ProgressBar_secondaryProgress, mSecondaryProgress));

    final Drawable indeterminateDrawable = a.getDrawable(
            R.styleable.ProgressBar_indeterminateDrawable);
    if (indeterminateDrawable != null) {
        setIndeterminateDrawableTiled(indeterminateDrawable);
    }

    mOnlyIndeterminate = a.getBoolean(
            R.styleable.ProgressBar_indeterminateOnly, mOnlyIndeterminate);

    mNoInvalidate = false;

    setIndeterminate(mOnlyIndeterminate || a.getBoolean(
            R.styleable.ProgressBar_indeterminate, mIndeterminate));

    mMirrorForRtl = a.getBoolean(R.styleable.ProgressBar_mirrorForRtl, mMirrorForRtl);

    if (a.hasValue(R.styleable.ProgressBar_progressTintMode)) {
        if (mProgressTintInfo == null) {
            mProgressTintInfo = new ProgressTintInfo();
        }
        mProgressTintInfo.mProgressTintMode = Drawable.parseTintMode(a.getInt(
                R.styleable.ProgressBar_progressBackgroundTintMode, -1), null);
        mProgressTintInfo.mHasProgressTintMode = true;
    }

    if (a.hasValue(R.styleable.ProgressBar_progressTint)) {
        if (mProgressTintInfo == null) {
            mProgressTintInfo = new ProgressTintInfo();
        }
        mProgressTintInfo.mProgressTintList = a.getColorStateList(
                R.styleable.ProgressBar_progressTint);
        mProgressTintInfo.mHasProgressTint = true;
    }

    if (a.hasValue(R.styleable.ProgressBar_progressBackgroundTintMode)) {
        if (mProgressTintInfo == null) {
            mProgressTintInfo = new ProgressTintInfo();
        }
        mProgressTintInfo.mProgressBackgroundTintMode = Drawable.parseTintMode(a.getInt(
                R.styleable.ProgressBar_progressTintMode, -1), null);
        mProgressTintInfo.mHasProgressBackgroundTintMode = true;
    }

    if (a.hasValue(R.styleable.ProgressBar_progressBackgroundTint)) {
        if (mProgressTintInfo == null) {
            mProgressTintInfo = new ProgressTintInfo();
        }
        mProgressTintInfo.mProgressBackgroundTintList = a.getColorStateList(
                R.styleable.ProgressBar_progressBackgroundTint);
        mProgressTintInfo.mHasProgressBackgroundTint = true;
    }

    if (a.hasValue(R.styleable.ProgressBar_secondaryProgressTintMode)) {
        if (mProgressTintInfo == null) {
            mProgressTintInfo = new ProgressTintInfo();
        }
        mProgressTintInfo.mSecondaryProgressTintMode = Drawable.parseTintMode(
                a.getInt(R.styleable.ProgressBar_secondaryProgressTintMode, -1), null);
        mProgressTintInfo.mHasSecondaryProgressTintMode = true;
    }

    if (a.hasValue(R.styleable.ProgressBar_secondaryProgressTint)) {
        if (mProgressTintInfo == null) {
            mProgressTintInfo = new ProgressTintInfo();
        }
        mProgressTintInfo.mSecondaryProgressTintList = a.getColorStateList(
                R.styleable.ProgressBar_secondaryProgressTint);
        mProgressTintInfo.mHasSecondaryProgressTint = true;
    }

    if (a.hasValue(R.styleable.ProgressBar_indeterminateTint)) {
        if (mProgressTintInfo == null) {
            mProgressTintInfo = new ProgressTintInfo();
        }
        mProgressTintInfo.mIndeterminateTintMode = Drawable.parseTintMode(a.getInt(
                R.styleable.ProgressBar_indeterminateTintMode, -1), null);
        mProgressTintInfo.mHasIndeterminateTintMode = true;
    }

    if (a.hasValue(R.styleable.ProgressBar_indeterminateTint)) {
        if (mProgressTintInfo == null) {
            mProgressTintInfo = new ProgressTintInfo();
        }
        mProgressTintInfo.mIndeterminateTintList = a.getColorStateList(
                R.styleable.ProgressBar_indeterminateTint);
        mProgressTintInfo.mHasIndeterminateTint = true;
    }

    a.recycle();

    applyProgressTints();
    applyIndeterminateTint();

    // If not explicitly specified this view is important for accessibility.
    if (getImportantForAccessibility() == View.IMPORTANT_FOR_ACCESSIBILITY_AUTO) {
        setImportantForAccessibility(View.IMPORTANT_FOR_ACCESSIBILITY_YES);
    }
}

参考official document

答案 1 :(得分:0)

你必须覆盖这样的3个构造函数:

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

    public TexPtrogressBar(Context context, AttributeSet attrs) {
        super(context, attrs);
    }

    public TexPtrogressBar(Context context) {
        super(context);
    }

答案 2 :(得分:0)

你的班级

public class TexPtrogressBar extends ProgressBar {
    @Override
    protected synchronized void onDraw(Canvas canvas) {
        super.onDraw(canvas);
    }
} 

有一个隐式的默认构造函数,可以像

那样查找编译器
public TexPtrogressBar() {
    super(); // Looking for ProgressBar();
}

但是,ProgressBar没有带有该签名的构造函数。您需要添加一个构造函数,该构造函数为ProgressBar调用一个已定义的构造函数。

您的构造函数不必具有与ProgressBar的构造函数相同的参数,但它需要调用ProgressBar的构造函数之一。