2012-08-01 104 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