2017-07-04 10 views
0

テーブルレイアウトに行を追加しようとしています。しかし、android.widget.LinearLayoutはandroid.widget.TableRowエラーが発生しているにキャストできません。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> 

と私の活動

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);} 
} 

私のカスタムテーブル行は

<?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> 

である私は、リニアレイアウトとインフレータを変更していますが、エラーがまだ発生しています。それとも、私はテーブルレイアウトでカスタムレイアウトを膨らませることができませんか?

+0

レイアウトを '' <しようとしましたか? –

+0

にはtableLayoutが含まれていますか? –

+0

アクティビティに設定する前にビューを検索しています。 –

答えて

1

これを試してみてください:

<?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> 

は、この情報がお役に立てば幸いです。

+0

ありがとうございます。 R.layout.table_rowコンテナのレイアウトはtableRowではないので、エラーが発生します。 –

関連する問題