2012-08-01 107 views
25

我想打一个自定义的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)); 
      } 
     } 
    } 
} 

我在做什么错?

+0

我在列的左侧使用单选ListView的无线电使用相同的代码,当我第一次加载此列表时,我无法检查项目,因为我必须显示预先选择的一个默认项目。 – Puneet 2014-09-18 07:18:48

回答

80

您只实现了一个构造函数。是XML能够使用的观点,你应该从View实现其他2层的构造函数:

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

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

啊!我希望林特能就此提醒我。谢谢! – 2012-08-01 06:35:16

+2

我必须在这里添加一个“谢谢”。花时间想知道为什么我的习惯课会摔倒。 – RichieHH 2014-03-21 09:22:58

4

你忘了覆盖构造函数,可以从代码或XML和创造中创建的每个版面使用不同的构造,覆盖另一个

+2

为什么需要实现另外两个构造函数的任何参考? – 2014-10-02 05:51:47