'How to solve the error: The specified child already has a parent. You must call removeView() on the child's parent first

I am writing a code for a simple pos software. My code is complete but while running its showing the Error: The specified child already has a parent. You must call removeView() on the child's parent first.....I read some similar questions about this and they stated to use" View view = inflater.inflate(R.layout.fragment_reject, container, false);"....but I don't know where to implement this line in my code....here is the main activity code for my project. b1 is the button and ed1-ed6 are EditText declared earlier

    @Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    ed1 = findViewById(R.id.ed1);
    ed2 = findViewById(R.id.ed2);
    ed3 = findViewById(R.id.ed3);
    ed4 = findViewById(R.id.ed4);
    ed5 = findViewById(R.id.ed5);
    ed6 = findViewById(R.id.ed6);

    b1 = findViewById(R.id.b1);

    b1.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            add();




        }
    });

}

public void add(){
    String productName = ed1.getText().toString();
    int price = Integer.parseInt(ed2.getText().toString());
    int quantity = Integer.parseInt(ed3.getText().toString());
    int total =  price * quantity;

    data1.add(productName);
    data2.add(String.valueOf(price));
    data3.add(String.valueOf(quantity));
    data4.add(String.valueOf(total));

    TableLayout table = (TableLayout) findViewById(R.id.tb1);

    TableRow row = new TableRow(this);
    TextView t1 = new TextView(this);
    TextView t2 = new TextView(this);
    TextView t3 = new TextView(this);
    TextView t4 = new TextView(this);

    int sum = 0;
    for(int i = 0; i < data1.size(); i++){

        String pName = data1.get(i);
        String pric = data2.get(i);
        String quan = data3.get(i);
        String tot = data4.get(i);

        t1.setText(pName);
        t2.setText(pric);
        t3.setText(quan);
        t4.setText(tot);

        sum = sum + Integer.parseInt(data4.get(i).toString());


    }


    row.addView(t1);
    row.addView(t2);
    row.addView(t3);
    row.addView(t3);
    table.addView(row);



    ed4.setText(String.valueOf(sum));
    ed1.setText("");
    ed2.setText("");
    ed3.setText("");
    ed1.requestFocus();



}

Please Help!!



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source