2012-08-01 32 views
25

Tôi muốn thực hiện một RelativeLayout tùy chỉnh, nhưng tôi cứ bị lỗi này:Tôi làm cách nào để mở rộng lớp bố cục đúng cách?

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) 

Và đây là lớp học của tôi:

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

Tôi đang làm gì sai?

+0

Tôi đang sử dụng cùng một mã này cho một lựa chọn ListView với radio ở bên trái của hàng, tôi không thể kiểm tra một mục khi tôi tải danh sách này lần đầu tiên vì tôi phải hiển thị một mục mặc định được chọn trước. – Puneet

Trả lời

80

Bạn chỉ thực hiện một hàm tạo. Để có quan điểm sử dụng có thể trong xml, bạn nên thực hiện 2 nhà thầu khác từ Xem:

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

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

Ah! Tôi ước Lint sẽ cảnh báo tôi về điều đó. Cảm ơn! –

+2

Tôi phải thêm "cảm ơn" ở đây cho điều đó. Chi tiêu lứa tuổi tự hỏi tại sao lớp tùy chỉnh của tôi đã giảm hơn. – RichieHH

4

Bạn quên ghi đè constructor, mỗi bố trí có thể được tạo ra từ mã hoặc từ XML và để tạo sử dụng nhà thầu khác nhau, ghi đè lên nhau

+2

Bất kỳ tham chiếu nào về lý do tại sao cần thiết để triển khai hai hàm tạo khác? –

Các vấn đề liên quan