android.widget.LinearLayout 无法转换为 android.widget.TableRow

android.widget.LinearLayout cannot be cast to android.widget.TableRow

我正在尝试向 table布局添加行。但是 android.widget.LinearLayout 无法转换为 android.widget.TableRow 发生错误。

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 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:layout_width="wrap_content"
    android:layout_height="wrap_content"
    tools:context="com.example.khash.santotootsoolol.MainActivity"
    android:orientation="vertical">

    <HorizontalScrollView
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:scrollbars="horizontal"
        android:fillViewport="true">

    <TableLayout
        android:id="@+id/table_result"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent">

        <TableRow>
            <TextView
                android:text="Date"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:gravity="center"
                android:padding="10dp"
                android:background="@drawable/table_header"
                android:textColor="@android:color/white"/>

            <TextView
                android:text="Payment"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:gravity="center"
                android:padding="10dp"
                android:background="@drawable/table_header"
                android:textColor="@android:color/white"/>


            <TextView
                android:text="Remainder"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:gravity="center"
                android:padding="10dp"
                android:background="@drawable/table_header"
                android:textColor="@android:color/white"/>


            <TextView
                android:padding="10dp"
                android:text="Interest"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:gravity="center"
                android:background="@drawable/table_header"
                android:textColor="@android:color/white" />

        </TableRow>


    </TableLayout>

    </HorizontalScrollView>

</LinearLayout>

和我的activity

public class MainActivity extends AppCompatActivity {

    private TableLayout tableLayout = null;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        tableLayout = (TableLayout)findViewById(R.id.table_result);
        TableRow tr = (TableRow)LayoutInflater.from(this).inflate(R.layout.table_row, null);
        tableLayout.addView(tr);
        setContentView(R.layout.activity_main);}
}

我的自定义 table 行是

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="horizontal" android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:weightSum="4"
    android:layout_marginBottom="3dp">

    <TextView
        android:id="@+id/tv_pay_date"
        android:text="7/31/2017"
        android:layout_weight="1"
        android:layout_width="0dp"
        android:layout_height="match_parent"
        android:gravity="center"
        android:background="@drawable/table_row_item"/>

    <TextView
        android:id="@+id/tv_monthly_payment"
        android:text="13,933,700"
        android:layout_weight="1"
        android:layout_width="0dp"
        android:layout_height="match_parent"
        android:gravity="center"
        android:background="@drawable/table_row_item"/>


    <TextView
        android:id="@+id/tv_remainder_payment"
        android:text="125,403,300"
        android:layout_weight="1"
        android:layout_width="0dp"
        android:layout_height="match_parent"
        android:gravity="center"
        android:background="@drawable/table_row_item"/>


    <TextView
        android:id="@+id/tv_interest"
        android:text=""
        android:layout_weight="1"
        android:layout_width="0dp"
        android:layout_height="match_parent"
        android:gravity="center"
        android:background="@drawable/table_row_item"/>

</LinearLayout>

我更改了线性布局和充气器,但仍然出现错误。或者我无法在 table 布局中扩充自定义布局?

试试这个:

<?xml version="1.0" encoding="utf-8"?>
<TableRow xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="horizontal" 
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:weightSum="4"
    android:layout_marginBottom="3dp">

<TextView
    android:id="@+id/tv_pay_date"
    android:text="7/31/2017"
    android:layout_weight="1"
    android:layout_width="0dp"
    android:layout_height="match_parent"
    android:gravity="center"
    android:background="@drawable/table_row_item"/>

<TextView
    android:id="@+id/tv_monthly_payment"
    android:text="13,933,700"
    android:layout_weight="1"
    android:layout_width="0dp"
    android:layout_height="match_parent"
    android:gravity="center"
    android:background="@drawable/table_row_item"/>


<TextView
    android:id="@+id/tv_remainder_payment"
    android:text="125,403,300"
    android:layout_weight="1"
    android:layout_width="0dp"
    android:layout_height="match_parent"
    android:gravity="center"
    android:background="@drawable/table_row_item"/>


<TextView
    android:id="@+id/tv_interest"
    android:text=""
    android:layout_weight="1"
    android:layout_width="0dp"
    android:layout_height="match_parent"
    android:gravity="center"
    android:background="@drawable/table_row_item"/>

</TableRow>

希望对您有所帮助。