2014-10-22 4 views
1

특정 CSS 스타일을 사용하여 textviews를 만드는 방법을 만들었지 만 클래스에 코드를 배치했는데 갑자기 작동이 멈췄습니다. 나는 그것이 메인 클래스에 있었기 때문에 처음에는 "this"를 가졌지 만 지금은 서브 클래스에 있고 "this"는 거기에서 작동하지 않으므로 제거하고 다른 에러를 얻는다. .생성자 TextView()가 정의되지 않았습니다.

public class textviewscreate { 

public void textviewToevoegen(RelativeLayout relativeLayout) { 
    RelativeLayout.LayoutParams relativeLayoutParams;  
    String[] naam = {"Bouw onderdeel", "Kapconstructie","Kapconstructieve bevesiging","Doorbuiging","Vochtinwerking","Dakconstructie","Constructieve bevesiging","Doorbuiging","Vochtinwerking","Waterkerende lagen","Waterdichtheid (folie)laag","Lekwaterafvoerend vermogen","Detaillering aan dakvoet","Thermischeisolatie","Bevestiging","Aansluitdetails","Isolerend vermogen","Dakpannen en vorsten","Conditie dakpannen en vorsten","Breukschade","Vorstschade","Afschilfering","Aangroei algen en mos"}; 
    TextView[] textView = new TextView[22]; 
    //1st TextView 
    textView[0] = new TextView(); //This gives an error, first was new TextView(this); 

    relativeLayoutParams = new RelativeLayout.LayoutParams(
      RelativeLayout.LayoutParams.WRAP_CONTENT, 
      RelativeLayout.LayoutParams.WRAP_CONTENT); 

    textView[0].setId(1); // changed id from 0 to 1 
    textView[0].setText(naam[0].toUpperCase()); 
    relativeLayout.addView(textView[0], relativeLayoutParams); 
    relativeLayoutParams.setMargins(24, 39, 0, 0); 
    // All othet Textviews 
    for (int i = 1; i < textView.length; i++) { 
     relativeLayoutParams = new RelativeLayout.LayoutParams(
       RelativeLayout.LayoutParams.WRAP_CONTENT, 
       RelativeLayout.LayoutParams.WRAP_CONTENT);  
     relativeLayoutParams.addRule(RelativeLayout.BELOW, 
        textView[i-1].getId()); 
      relativeLayoutParams.addRule(RelativeLayout.ALIGN_LEFT, 
       textView[i-1].getId()); // added top alignment rule 

     textView[i] = new TextView(); //Also an error firstly new TextView(this); 
      textView[i].setText(naam[i]); 
      textView[i].setId(i+1); 
      relativeLayout.addView(textView[i], relativeLayoutParams); 
    } 
} 

}

답변

1

당신은 TextView 비 활동 클래스의 생성자 또는 메소드에 전달 될 수 context를 사용하여 초기화해야합니다. 마찬가지로 :

public void textviewToevoegen(RelativeLayout relativeLayout, Context context) 

을 그리고 같은 컨텍스트를 사용하는

textView[i] = new TextView(); 

로 전달됩니다, 당신은 textviewToevoegen를 사용하여 전달하는 경우

textView[i] = new TextView(context); 

된다 :

public void textviewToevoegen(RelativeLayout relativeLayout)이된다 귀하의 활동에 속한 relativeLayout, this 또는 activitycontext , public constructors of the TextView

+0

투표 한 사람에게 .. 투표하는 이유는 무엇입니까? – user2450263

2

는 텍스트 뷰에 대한 빈 생성자가 없습니다. 당신은 활동이나 조각의 sublcass에없는 때문에 당신은 제공해야 적어도, 방법에 대한 매개 변수로 상황에 맞는 다음 RelativeLayout의 유효한 객체 인 경우

public void textviewToevoegen(Context context, RelativeLayout relativeLayout) { 
    textView[0] = new TextView(context); 

또는 귀하의 경우

, 당신이는 getContext

사용할 수 있습니다
public void textviewToevoegen(RelativeLayout relativeLayout) { 
    textView[0] = new TextView(relativeLayout.getContext()); 
1

이 방법을 시도 이것은 당신이 당신의 문제를 해결하는 데 도움이되기를 바랍니다 :

을 참조하십시오.

사용자 정의보기 클래스를 제공하면 사용자 정의보기를 (TextView, Button 등)와 같은 안드로이드 내장보기로 작동하는 오버로드 된 생성자를 제공해야합니다.

public class textviewscreate { 

    private Context context; 

    public textviewscreate(Context context, AttributeSet attrs, int defStyle) { 
     this.context=context; 
    } 

    public textviewscreate(Context context, AttributeSet attrs) { 
     this.context=context; 
    } 

    public textviewscreate(Context context) { 
     this.context=context; 
    } 

    public void textviewToevoegen(Context context,RelativeLayout relativeLayout) { 
     this.context = context; 
     RelativeLayout.LayoutParams relativeLayoutParams; 
     String[] naam = {"Bouw onderdeel", "Kapconstructie","Kapconstructieve bevesiging","Doorbuiging","Vochtinwerking","Dakconstructie","Constructieve bevesiging","Doorbuiging","Vochtinwerking","Waterkerende lagen","Waterdichtheid (folie)laag","Lekwaterafvoerend vermogen","Detaillering aan dakvoet","Thermischeisolatie","Bevestiging","Aansluitdetails","Isolerend vermogen","Dakpannen en vorsten","Conditie dakpannen en vorsten","Breukschade","Vorstschade","Afschilfering","Aangroei algen en mos"}; 
     TextView[] textView = new TextView[22]; 
     //1st TextView 
     textView[0] = new TextView(); //This gives an error, first was new TextView(this); 

     relativeLayoutParams = new RelativeLayout.LayoutParams(
       RelativeLayout.LayoutParams.WRAP_CONTENT, 
       RelativeLayout.LayoutParams.WRAP_CONTENT); 

     textView[0].setId(1); // changed id from 0 to 1 
     textView[0].setText(naam[0].toUpperCase()); 
     relativeLayout.addView(textView[0], relativeLayoutParams);Ij 
     relativeLayoutParams.setMargins(24, 39, 0, 0); 
     // All othet Textviews 
     for (int i = 1; i < textView.length; i++) { 
      relativeLayoutParams = new RelativeLayout.LayoutParams(
        RelativeLayout.LayoutParams.WRAP_CONTENT, 
        RelativeLayout.LayoutParams.WRAP_CONTENT); 
      relativeLayoutParams.addRule(RelativeLayout.BELOW, 
        textView[i-1].getId()); 
      relativeLayoutParams.addRule(RelativeLayout.ALIGN_LEFT, 
        textView[i-1].getId()); // added top alignment rule 

      textView[i] = new TextView(); //Also an error firstly new TextView(this); 
      textView[i].setText(naam[i]); 
      textView[i].setId(i+1); 
      relativeLayout.addView(textView[i], relativeLayoutParams); 
     } 
    } 
} 
관련 문제