Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/328.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/194.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 构造函数TextView()未定义_Java_Android_Textview - Fatal编程技术网

Java 构造函数TextView()未定义

Java 构造函数TextView()未定义,java,android,textview,Java,Android,Textview,我创建了一个方法来创建具有特定CSS样式的TextView,但是我将代码放在一个类中,它突然停止工作。我第一次使用它是因为它在主类中,但现在它在一个子类中,在那里不起作用,所以我删除了它,现在我得到了一个不同的错误,我不知道如何修复它 public class textviewscreate { public void textviewToevoegen(RelativeLayout relativeLayout) { RelativeLayout.LayoutParams relat

我创建了一个方法来创建具有特定CSS样式的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);
    }
}

}TextView没有空构造函数。由于您不在activity或fragment的子类中,因此至少应提供上下文作为方法的参数:

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

TextView没有空构造函数。由于您不在activity或fragment的子类中,因此至少应提供上下文作为方法的参数:

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

您需要使用上下文初始化TextView,上下文可以在非活动类的构造函数或方法中传递。比如:

public void textViewToVoegenRelativeLayout变为:

public void textviewToevoegen(RelativeLayout relativeLayout, Context context)  
并将其传递给

textView[i] = new TextView();  
它使用上下文,成为

textView[i] = new TextView(context);  
当您使用TextViewToeGen并从活动中传递relativeLayout时,请传递此活动或该活动的上下文


请参阅:

您需要使用上下文初始化TextView,上下文可以在非活动类的构造函数或方法中传递。比如:

public void textViewToVoegenRelativeLayout变为:

public void textviewToevoegen(RelativeLayout relativeLayout, Context context)  
并将其传递给

textView[i] = new TextView();  
它使用上下文,成为

textView[i] = new TextView(context);  
当您使用TextViewToeGen并从活动中传递relativeLayout时,请传递此活动或该活动的上下文

参考:

试试这种方法,希望这能帮助你解决问题

当您提供自定义视图类时,您必须提供一些重载构造函数,这些构造函数将您的自定义视图作为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);
        }
    }
}
试试这个方法,希望这能帮助你解决你的问题

当您提供自定义视图类时,您必须提供一些重载构造函数,这些构造函数将您的自定义视图作为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);
        }
    }
}

对于投票被否决的人,最好评论一下为什么被否决的人对于投票被否决的人,最好评论一下为什么被否决的人