【问题标题】:android.widget.TableRow cannot be cast to android.widget.TableLayoutandroid.widget.TableRow 无法转换为 android.widget.TableLayout
【发布时间】:2014-01-23 18:12:00
【问题描述】:

我编写了我的第一个 android 应用程序,简单的 CRUD。但是在我将 GridView 更改为 TableLayout 后,出现以下错误:

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

这是我的活动:

public class AllActivity extends Activity {

    private SQLiteDatabase sampleDB;
    private TableLayout tableList;

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





        sampleDB = this.openOrCreateDatabase("my_app",
                SQLiteDatabase.CREATE_IF_NECESSARY, null);


        String query = "SELECT * FROM BOOKS;";
        Cursor cursor = sampleDB.rawQuery(query, null);


        tableList = (TableLayout)findViewById(R.id.tableList);

        if(cursor.moveToFirst())
        {
            do
            {
                TableRow row = new TableRow(this);

                TextView id = new TextView(this);
                id.setText(""+cursor.getLong(0));

                TextView title = new TextView(this);
                title.setText(cursor.getString(1));

                TextView author = new TextView(this);
                author.setText(cursor.getString(2));


                row.addView(id);
                row.addView(title);
                row.addView(author);

                tableList.addView(row);



            }while(cursor.moveToNext());

        }

    }


}

我的看法:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >


    <TableRow
        android:id="@+id/tableList"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" >
    </TableRow>

</LinearLayout>

任何提示我做错了什么?
我做了所有像 Print out ArrayList content in Android TableLayout 一样的事情,但无法正常工作。

【问题讨论】:

    标签: android android-layout exception


    【解决方案1】:
    android.widget.TableRow cannot be cast to android.widget.TableLayout
    

    你有

     <TableRow
        android:id="@+id/tableList" // table row
        android:layout_width="match_parent"
        android:layout_height="wrap_content" >
    </TableRow>
    

    但是在初始化时你有

     tableList = (TableLayout)findViewById(R.id.tableList);
     // casting tablerow to tablelayout
    

    您可能需要检查

    http://www.mkyong.com/android/android-tablelayout-example/

    您还可以在 sdk 示例中找到表格布局示例@

    android-sdk/samples/android-17/ApiDemos/res/layout/table_layout_1.xml

    【讨论】:

    • 当然!该死的……愚蠢的错误。我确定我从可视化编辑器中拖动了 TableLayout。感谢帮助。我会在 8 分钟内接受答复
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-02-02
    • 2016-06-30
    • 2016-01-14
    • 2013-07-25
    • 2016-07-05
    • 2017-01-24
    相关资源
    最近更新 更多