【问题标题】:Table w/MYSQL "java.lang.IllegalStateException: The specified child already has a parent. You must call removeView() on the child's parent first."带有 MYSQL 的表“java.lang.IllegalStateException:指定的孩子已经有一个父母。您必须首先在孩子的父母上调用 removeView()。”
【发布时间】:2022-01-18 21:40:53
【问题描述】:

我正在 Android Studio 上创建一个从 MYSQL 数据库获取信息的表,它应该返回 2 行,但它返回的是这个错误代码。我需要它能够根据我从数据库获得的行数自动创建行。

请帮忙!

这是来自表 xml 的代码。

<?xml version="1.0" encoding="utf-8"?>

<TableLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/table"
    tools:context=".foliosMonitor"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    tools:layout_editor_absoluteX="1dp"
    tools:layout_editor_absoluteY="1dp"
    >

    <TableRow

        android:layout_width="match_parent"
        android:layout_height="73dp"
        android:background="#205FA1">

        <TextView
            android:id="@+id/ticketIdName"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginLeft="8dp"
            android:fontFamily="@font/raleway"
            android:textColor="@color/white"
            android:layout_marginRight="8dp"
            android:text="TicketId" />





        <TextView
            android:id="@+id/truckplatesName"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginLeft="8dp"
            android:fontFamily="@font/raleway"
            android:textColor="@color/white"
            android:layout_marginRight="8dp"
            android:text="TruckPlates" />

        <TextView
            android:id="@+id/ecoTruckName"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginLeft="8dp"
            android:fontFamily="@font/raleway"
            android:textColor="@color/white"
            android:layout_marginRight="8dp"
            android:text="EcoTruck" />

        <TextView
            android:id="@+id/statusName"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginLeft="8dp"
            android:fontFamily="@font/raleway"
            android:textColor="@color/white"
            android:layout_marginRight="8dp"
            android:text="Status" />







    </TableRow>

    <TableRow

        android:layout_width="match_parent"
        android:layout_height="73dp"
        android:layout_marginTop="5dp"
        android:background="#D8D8D8">

        <TextView
            android:id="@+id/ticketId"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginLeft="8dp"
            android:layout_marginRight="8dp"
            android:fontFamily="@font/raleway"
            android:text="TicketId"
            android:onClick="editTabla"
            android:textColor="@color/black" />

        <TextView
            android:id="@+id/truckplates"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginLeft="8dp"
            android:layout_marginRight="8dp"
            android:fontFamily="@font/raleway"
            android:text="TruckPlates"
            android:textColor="@color/black" />

        <TextView
            android:id="@+id/ecoTruck"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginLeft="8dp"
            android:layout_marginRight="8dp"
            android:fontFamily="@font/raleway"
            android:text="EcoTruck"
            android:textColor="@color/black" />

        <TextView
            android:id="@+id/status"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginLeft="8dp"
            android:fontFamily="@font/raleway"
            android:textColor="@color/black"
            android:layout_marginRight="8dp"
            android:text="Status" />



    </TableRow>

</TableLayout>

这是在活动上创建表格的空白

public void readTable(){
    
            View registro = LayoutInflater.from(this).inflate(R.layout.table_row_monitor, null, false);
            String URLFETCHTABLE = "https://www.cedepaser.com.mx/TruckParking/App/php/getTicketTable.php?customerId=" + customerId;
            JsonObjectRequest jsonObjectRequest = new JsonObjectRequest(Request.Method.GET,
                    URLFETCHTABLE,
                    null, new Response.Listener<JSONObject>() {
                @Override
                public void onResponse(JSONObject response) {
                    try {
                        JSONArray jsonArray = response.getJSONArray("ticket");
                        for(int i = 0; i<=jsonArray.length() - 1; i++){
    
                            Log.d("valueOfI", String.valueOf(i));
                            Log.d("ARRAYLENGTH", String.valueOf(jsonArray.length()));
    
                            JSONObject jsonObject = jsonArray.getJSONObject(i);
                            TextView ticketId = registro.findViewById(R.id.ticketId);
                            TextView truckPlates = registro.findViewById(R.id.truckplates);
                            TextView ecoTruck = registro.findViewById(R.id.ecoTruck);
                            TextView status = registro.findViewById(R.id.status);
    
                            ticketId.setText(jsonObject.getString("ticketId"));
                            truckPlates.setText(jsonObject.getString("truckPlates"));
                            ecoTruck.setText(jsonObject.getString("ecoTruck"));
                            status.setText(jsonObject.getString("idStatus"));
                            tbUsuarios.addView(registro);
    
                            iCount = i;
                        }
                        Log.d("iCount", String.valueOf(iCount));
    
    
                    } catch (JSONException e) {
                        e.printStackTrace();
                        Log.d("ARRAYLENGTH", "ENTRO AL CATCH");
    
                    }
                }
            }, new Response.ErrorListener() {
                @Override
                public void onErrorResponse(VolleyError error) {
    
                }
            });
            requestQueue.add(jsonObjectRequest);
    
    
        }

【问题讨论】:

    标签: android mysql database android-studio row


    【解决方案1】:

    您不能将相同的“registro”视图重新添加到“tbUsuarios”。我不知道什么是“tbUsuarios”,但它似乎是一个 ViewGroup(LinearLayout、RelativeLayout、ConstraintLayout、FrameLayout 等),如果您在第一个 FOR 循环循环中添加“registro”,则无法重新添加相同的“registro”,因为它已分配给父级。

    我想你不想重新添加相同的“registro”,所以我想你只需要移动:

    View registro = LayoutInflater.from(this).inflate(R.layout.table_row_monitor, null, false);
    

    在 FOR 循环的开头。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2015-09-06
      • 1970-01-01
      • 2015-03-20
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多