收到错误“您必须首先在孩子的父母上调用removeView()”

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了收到错误“您必须首先在孩子的父母上调用removeView()”相关的知识,希望对你有一定的参考价值。

我尝试将行动态添加到我的tableview主要活动代码时发生上述错误。我已经尝试了很多堆栈溢出的解决方案。但没有解决我的问题。

package com.example.bhramaram.myapplication;

import android.content.Context;
import android.database.Cursor;
import android.os.Bundle;
import android.support.v7.app.AlertDialog;
import android.support.v7.app.AppCompatActivity;
import android.view.ViewGroup;
import android.widget.TableLayout;
import android.widget.TableRow;
import android.widget.TextView;

import com.example.bhramaram.myapplication.Dbhelper;
import com.example.bhramaram.myapplication.R;

public class jav extends AppCompatActivity {
    TableLayout tb;
    TableRow tableRow;
    Dbhelper dbhelper;
    @Override
    public void onCreate(Bundle savedInstanceState){
        int day=0;
        super.onCreate(savedInstanceState);
        setContentView(R.layout.timetable);
        getvalues();
    }

    private void getvalues() {

        tb=(TableLayout)findViewById(R.id.timetble);
        TextView textView1,textView2,textView3,textView4,textView5,textView0;
        textView0=new TextView(this);textView1=new TextView(this);
        textView2=new TextView(this);textView3=new TextView(this);
        textView4=new TextView(this);textView5=new TextView(this);
        TableRow.LayoutParams lp = new TableRow.LayoutParams(TableRow.LayoutParams.MATCH_PARENT);
        textView0.setLayoutParams(lp);textView1.setLayoutParams(lp);
        textView2.setLayoutParams(lp);textView3.setLayoutParams(lp);
        textView4.setLayoutParams(lp);textView5.setLayoutParams(lp);
        dbhelper= new Dbhelper(jav.this);
        Cursor cursor= dbhelper.recieveData();

        if(cursor.getCount()==0){
            alert("Nothing","Nothing to show");
            return;}
        int i=0;
        while (cursor.moveToNext())
        {
            tableRow=new TableRow(this);

            TableLayout.LayoutParams lp1=new TableLayout.LayoutParams(TableLayout.LayoutParams.MATCH_PARENT,TableLayout.LayoutParams.MATCH_PARENT);

            textView0.setText((cursor.getString(0)));textView1.setText((cursor.getString(1)));
            textView2.setText((cursor.getString(2)));textView3.setText((cursor.getString(3)));
            textView4.setText((cursor.getString(4)));textView5.setText((cursor.getString(5)));
            tableRow.setLayoutParams(lp);
            //Error occurred here
            tableRow.addView(textView0);tableRow.addView(textView1);
            tableRow.addView(textView2);tableRow.addView(textView3);
            tableRow.addView(textView4);tableRow.addView(textView5);
            tb.addView(tableRow,lp1);

        }



    }

    private void alert(String title, String alert) {
        AlertDialog.Builder builder=new AlertDialog.Builder(this);
        builder.setCancelable(true);
        builder.setTitle(title);
        builder.setMessage(alert);
    }


}

相应的布局

<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <TableLayout
        android:id="@+id/timetble"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        tools:layout_editor_absoluteX="8dp"
        tools:layout_editor_absoluteY="8dp"
        tools:ignore="MissingConstraints">
    </TableLayout>
</android.support.constraint.ConstraintLayout>

错误详情

进程:com.example.bhramaram.myapplication,PID:14266 java.lang.RuntimeException:无法启动活动ComponentInfo {com.example.bhramaram.myapplication / com.example.bhramaram.myapplication.jav}:'java.lang.IllegalStateException :指定的子项已有父项。你必须先在孩子的父母身上调用removeView()。在android.app.A活动中的android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2646),android.app.A活动中的android.app.ActivityThread.handleLaunch活动(ActivityThread.java:2707),android.app.ActivityThread的android.app.ActivityThread.-wrap12(ActivityThread.java) $ H.handleMessage(ActivityThread.java:1460)位于android.app.A.运行时,Android.O.Ra上运行android.O.Roper.loop(Looper.java:154)的android.os.Handler.dispatchMessage(Handler.java:102) ActivityThread.java:6077)位于com.android.internal.os的com.android.internal.os.ZygoteInit $ MethodAndArgsCaller.run(ZygoteInit.java:865)的java.lang.reflect.Method.invoke(Native Method)。 ZygoteInit.main(ZygoteInit.java:755)引起:java.lang.IllegalStateException:指定的子节点已经有父节点。您必须首先在孩子的父母上调用removeView()。在Android.view上的android.view.ViewGroup.addView(ViewGroup.java:4301)上的android.view.ViewGroup.addViewInner(ViewGroup.java:4460)的android.view.View.View. View.com.addView(ViewGroup.java:4214)at com.example.bhramaram.myapplication.jav.getvalues(jav.java:60)at com.example.bhramaram.myapplication.jav.onCreate(jav.java:29)at android .app.Activity.performCreate(Activity.java:6705)在android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1119)android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2599)

为什么我收到此错误?

我也是Android开发者社区的新手。这个项目是关于一个使用本地SQLite数据库来存储和显示时间表的应用程序。我按照我在网上找到的方法在表格中显示结果。如果您可以为此代码段建议更有效的方法,对我有帮助。

答案

在这里,您可以创建文本视图:

TextView textView1,textView2,textView3,textView4,textView5,textView0;
textView0=new TextView(this);textView1=new TextView(this);
textView2=new TextView(this);textView3=new TextView(this);
textView4=new TextView(this);textView5=new TextView(this);

然后,在循环内部,您将相同的TextView添加到TableRow中几次:

tableRow.addView(textView0);tableRow.addView(textView1);
tableRow.addView(textView2);tableRow.addView(textView3);
tableRow.addView(textView4);tableRow.addView(textView5);
tb.addView(tableRow,lp1);

由于这些TextView之前已经添加到一行,因此它们已经有了父视图,并导致您提到的错误。

为了修复,你必须将new TextView()部分移动到循环中。您必须为每个循环迭代创建新对象:

while (cursor.moveToNext()) {
    tableRow=new TableRow(this);

    TableLayout.LayoutParams lp1=new TableLayout.LayoutParams(TableLayout.LayoutParams.MATCH_PARENT,TableLayout.LayoutParams.MATCH_PARENT);

    // Create new objects before adding them
    textView0=new TextView(this);textView1=new TextView(this);
    textView2=new TextView(this);textView3=new TextView(this);
    textView4=new TextView(this);textView5=new TextView(this);

    textView0.setText((cursor.getString(0)));textView1.setText((cursor.getString(1)));
    textView2.setText((cursor.getString(2)));textView3.setText((cursor.getString(3)));
    textView4.setText((cursor.getString(4)));textView5.setText((cursor.getString(5)));
    tableRow.setLayoutParams(lp);

    tableRow.addView(textView0);tableRow.addView(textView1);
    tableRow.addView(textView2);tableRow.addView(textView3);
    tableRow.addView(textView4);tableRow.addView(textView5);
    tb.addView(tableRow,lp1);
}

以上是关于收到错误“您必须首先在孩子的父母上调用removeView()”的主要内容,如果未能解决你的问题,请参考以下文章

为啥我会收到这种错误验证错误?

SimpleSAML 配置错误,收到此错误。可能是啥问题?

为啥我会收到错误数量的参数错误?

我收到以下错误:[GraphQL 错误]:消息:任务

为啥我会收到错误错误 C2664:'reverseString'

为啥我会收到 Laravel Composer 解析错误?