2014-10-22 105 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

你需要使用TextViewcontext,它可以在非活動類的構造函數或方法傳遞的初始化。像:

public void textviewToevoegen(RelativeLayout relativeLayout)變爲:

public void textviewToevoegen(RelativeLayout relativeLayout, Context context) 

而同樣傳遞給

textView[i] = new TextView(); 

它使用的情況下,當您使用textviewToevoegen並通過成爲

textView[i] = new TextView(context); 

relativeLayout從您的活動中,通過thisactivitycontext

參見:public constructors of the TextView

+0

給那個投了倒票的人..會不會很好評論爲什麼投票棄權 – user2450263 2014-10-22 09:47:37

2

沒有爲TextView的沒有空的構造函數。既然你是不是在活動或片段的sublcass你應該提供,至少,上下文作爲參數的方法:

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

或在您的情況下,如果RelativeLayout的是一個有效的對象,你可以使用的getContext

public void textviewToevoegen(RelativeLayout relativeLayout) { 
    textView[0] = new TextView(relativeLayout.getContext()); 
1

試試這個辦法,希望這將幫助你解決你的問題。

當你提供你的自定義視圖類,那麼你必須給一些重載的構造函數,它像android內置視圖(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); 
     } 
    } 
}