2014-01-23 69 views
0

我写了我的第一个android应用程序,简单的CRUD。但我改变了GridView控件之后TableLayout我获得以下错误:android.widget.TableRow不能转换为android.widget.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一样的一切,但不能得到它的工作。

回答

2
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 /样品/ Android的17/ApiDemos/RES /布局/ table_layout_1.xml

+0

当然!该死......愚蠢的错误。我确定我从可视化编辑器拖动了TableLayout。感谢帮助。我会在8分钟内接受答案 – user1409508

相关问题