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

답변

80

하나의 생성자 만 구현했습니다. XML에서 수 사용의 전망을하기 위해, 당신이보기에서이 다른 생성자를 구현해야합니다 :

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

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

아! 나는 린트가 그것에 대해 경고했으면 좋겠다. 감사! –

+2

여기에 "감사합니다"를 추가해야합니다. 내 커스텀 클래스가 왜 떨어 졌는지 궁금해하는 시대를 보내십시오. – RichieHH

4

당신은 생성자를 오버라이드 (override)하는 것을 잊었다, 각 레이아웃 코드 또는 XML로부터 생성을 만들 수 있습니다 또 하나를 오버라이드 (override), 다른 생성자를 사용

+2

왜 다른 두 생성자를 구현하는 데 필요한 참조? –

관련 문제