构造函数TextView()未定义

时间:2014-10-22 09:28:56

标签: java android textview

我创建了一种方法来创建具有某些CSS样式的textview,但是我将代码放在一个类中,它突然停止工作。我首先得到了#34;这个"因为它是在主类中,但现在它在一个子类中,并且"这个"我没有在那里工作,所以我删除它,现在我得到一个不同的错误,我不知道如何解决。

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

}

3 个答案:

答案 0 :(得分:2)

TextView没有空构造函数。由于您不是活动或片段的次级,您应该至少提供上下文作为方法的参数:

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 :(得分:1)

您需要使用TextView初始化contextpublic void textviewToevoegen(RelativeLayout relativeLayout)可以在非活动类的构造函数或方法中传递。喜欢:

public void textviewToevoegen(RelativeLayout relativeLayout, Context context) 变为:

textView[i] = new TextView();  

同样传递给

textView[i] = new TextView(context);  
使用上下文的

变为

textviewToevoegen

当您使用relativeLayout并传递活动中的this时,请传递activitycontext&#39; s {{1}}。

参考:public constructors of the TextView

答案 2 :(得分: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);
        }
    }
}