如何正确扩展布局类?

Ste*_*oen 25 android android-layout

我想制作一个自定义RelativeLayout,但我一直收到这个错误:

08-01 02:28:19.385: E/AndroidRuntime(9989): FATAL EXCEPTION: main
08-01 02:28:19.385: E/AndroidRuntime(9989): android.view.InflateException: Binary XML     file line #1: Error inflating class com.stevenschoen.putio.CheckableRelativeLayout
08-01 02:28:19.385: E/AndroidRuntime(9989):     at     android.view.LayoutInflater.createView(LayoutInflater.java:596)
08-01 02:28:19.385: E/AndroidRuntime(9989):     at     android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:687)
08-01 02:28:19.385: E/AndroidRuntime(9989):     at     android.view.LayoutInflater.inflate(LayoutInflater.java:466)
08-01 02:28:19.385: E/AndroidRuntime(9989):     at     android.view.LayoutInflater.inflate(LayoutInflater.java:396)

...

08-01 02:28:19.385: E/AndroidRuntime(9989): Caused by: java.lang.NoSuchMethodException:     <init> [class android.content.Context, interface android.util.AttributeSet]
08-01 02:28:19.385: E/AndroidRuntime(9989):     at     java.lang.Class.getConstructorOrMethod(Class.java:460)
08-01 02:28:19.385: E/AndroidRuntime(9989):     at     java.lang.Class.getConstructor(Class.java:431)
08-01 02:28:19.385: E/AndroidRuntime(9989):     at     android.view.LayoutInflater.createView(LayoutInflater.java:561)
Run Code Online (Sandbox Code Playgroud)

这是我的班级:

public class CheckableRelativeLayout extends RelativeLayout implements
        Checkable {

    public CheckableRelativeLayout(Context context) {
        super(context);
        // TODO Auto-generated constructor stub
    }

    private boolean isChecked;
//  List<Checkable> checkableViews = new ArrayList<Checkable>();

    // @see android.widget.Checkable#isChecked()
    public boolean isChecked() {
        return isChecked;
    }

    // @see android.widget.Checkable#setChecked(boolean)
    public void setChecked(boolean isChecked) {
        this.isChecked = isChecked;
//      for (Checkable c : checkableViews) {
            // Pass the information to all the child Checkable widgets
//          c.setChecked(isChecked);
//      }
    }

    // @see android.widget.Checkable#toggle()
    public void toggle() {
        this.isChecked = !this.isChecked;
//      for (Checkable c : checkableViews) {
            // Pass the information to all the child Checkable widgets
//          c.toggle();
//      }
    }

    @Override
    protected void onFinishInflate() {
        super.onFinishInflate();

        final int childCount = this.getChildCount();
        for (int i = 0; i < childCount; ++i) {
            findCheckableChildren(this.getChildAt(i));
        }
    }

    /**
     * Add to our checkable list all the children of the view that implement the
     * interface Checkable
     */
    private void findCheckableChildren(View v) {
        if (v instanceof Checkable) {
//          this.checkableViews.add((Checkable) v);
        }

        if (v instanceof ViewGroup) {
            final ViewGroup vg = (ViewGroup) v;
            final int childCount = vg.getChildCount();
            for (int i = 0; i < childCount; ++i) {
                findCheckableChildren(vg.getChildAt(i));
            }
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

我究竟做错了什么?

Ale*_*lov 79

您只实现了一个构造函数.为了能够在xml中使用视图,您应该在View中实现2个其他构造函数:

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

public CheckableRelativeLayout(Context context, AttributeSet attrs, int defStyle) {
    super(context, attrs, defStyle);
}
Run Code Online (Sandbox Code Playgroud)

  • 啊! 我希望林特会警告我这件事.谢谢! (2认同)
  • 我必须在这里添加一个"谢谢".花了很多年,想知道为什么我的定制课程倒闭了. (2认同)

Dmi*_*sov 5

您忘记重写构造函数,每个布局都可以从代码或 XML 创建,并且为了创建使用不同的构造函数,请重写另一个构造函数

  • 关于为什么需要实现其他两个构造函数的任何参考? (2认同)