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
Android “动态创建表”下的“动态创建”按钮_Android - Fatal编程技术网

Android “动态创建表”下的“动态创建”按钮

Android “动态创建表”下的“动态创建”按钮,android,Android,我动态创建一个表: 标题 void addHeader() { /** Create a TableRow dynamically **/ tr = new TableRow(this); /** Creating a TextView to add to the row **/ label = new TextView(this); label.setText("Inventarnummer");

我动态创建一个表:

标题

void addHeader() {
        /** Create a TableRow dynamically **/
        tr = new TableRow(this);
        /** Creating a TextView to add to the row **/
        label = new TextView(this);
        label.setText("Inventarnummer");
        label.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT,
                LayoutParams.WRAP_CONTENT));
        label.setPadding(5, 5, 5, 5);
        LinearLayout Ll = new LinearLayout(this);
        LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(
                LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT);
        params.setMargins(5, 5, 5, 5);
        // Ll.setPadding(10, 5, 5, 5);
        Ll.addView(label, params);
        tr.addView((View) Ll); // Adding textView to tablerow.

        /** Creating Qty Button **/
        TextView place = new TextView(this);
        place.setText("Bezeichnung");
        place.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT,
                LayoutParams.WRAP_CONTENT));
        place.setPadding(5, 5, 5, 5);
        // place.setBackgroundColor(Color.RED);
        Ll = new LinearLayout(this);
        params = new LinearLayout.LayoutParams(LayoutParams.FILL_PARENT,
                LayoutParams.WRAP_CONTENT);
        params.setMargins(0, 5, 5, 5);
        // Ll.setPadding(10, 5, 5, 5);
        Ll.addView(place, params);
        tr.addView((View) Ll); // Adding textview to tablerow.

        /** Creating Qty Button **/
        TextView place1 = new TextView(this);
        place1.setText("Betriebszahl");
        place1.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT,
                LayoutParams.WRAP_CONTENT));
        place1.setPadding(5, 5, 5, 5);
        // place.setBackgroundColor(Color.RED);
        Ll = new LinearLayout(this);
        params = new LinearLayout.LayoutParams(LayoutParams.FILL_PARENT,
                LayoutParams.WRAP_CONTENT);
        params.setMargins(0, 5, 5, 5);
        // Ll.setPadding(10, 5, 5, 5);
        Ll.addView(place1, params);
        tr.addView((View) Ll); // Adding textview to tablerow.

        // Add the TableRow to the TableLayout
        tl.addView(tr, new TableLayout.LayoutParams(LayoutParams.FILL_PARENT,
                LayoutParams.WRAP_CONTENT));
    }
该按钮在ScrollView内的表格下方是静态的,但如果表格过长,则该按钮位于可见区域之外

我需要动态地创建一个按钮,在桌子下面(和桌子里面?)

我用下面的代码(和其他代码)进行了尝试:

日志:

java.lang.RuntimeException: Unable to start activity ComponentInfo{eu.straff/eu.straff.OutAcceptActivity}: java.lang.IllegalStateException: The specified child already has a parent. You must call removeView() on the child's parent first.

第二种方法中的“标签”是什么?看起来它属于另一个父视图,因此不能对其调用
addView(label)
。此外,您正在创建一个新按钮,但您没有在任何地方调用
addView(myButton)
。噢,谢谢,必须有myButton而不是label。谢谢,但是onClick方法不起作用,我是怎么做到的?帮帮我!
java.lang.RuntimeException: Unable to start activity ComponentInfo{eu.straff/eu.straff.OutAcceptActivity}: java.lang.IllegalStateException: The specified child already has a parent. You must call removeView() on the child's parent first.