android - 如何正确扩展布局类?

标签 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)

这是我的课:

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));
            }
        }
    }
}

我做错了什么?

最佳答案

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

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

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

关于android - 如何正确扩展布局类?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11753719/

相关文章:

android - 创建用于测试的模拟 Activity 的最快方法

java - listView 不显示“设计”选项卡中的项目,但显示在模拟器中

android - System.gc() 导致从 Activity 的第二次启动开始变慢

java - 如何从 CustomView 开始一个新的 Activity?

android - 以编程方式将 View 添加到 LinearLayout 的中间

android - 缓存访问被拒绝-eclipse-emulator

android - 具有多个订阅者的单个 Observable

android - 现在,Arc Welder 未在 Windows 上启动测试应用程序

android - 如何知道是否存在互联网连接?

android - FrameLayout 中的叠加行为不正确