【发布时间】:2012-01-11 20:05:11
【问题描述】:
问题已解决:我应该清理项目。感谢您的回答
嘿伙计们,我正在尝试在执行期间向表格布局添加行,但它给出了一个错误
12-02 20:29:47.588:错误/AndroidRuntime(569):java.lang.RuntimeException:无法启动活动 ComponentInfo{com.project.andr/com.project.andr.MainActivity}:java.lang。 ClassCastException: android.widget.ScrollView
这是我的 xml 文件
<?xml version="1.0" encoding="utf-8"?>
<TableLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/myTableLayout"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<TableRow
android:layout_width="fill_parent"
android:layout_height="wrap_content">
<Button android:text="Static Button" android:id="@+id/myButton"/>
</TableRow>
</TableLayout>
这是我的java代码
setContentView(R.layout.main);
TableLayout tl = (TableLayout)findViewById(R.id.myTableLayout);
/* Create a new row to be added. */
TableRow tr = new TableRow(this);
tr.setLayoutParams(new LayoutParams(
LayoutParams.FILL_PARENT,
LayoutParams.WRAP_CONTENT));
/* Create a Button to be the row-content. */
Button b = new Button(this);
b.setText("Dynamic Button");
b.setLayoutParams(new LayoutParams(
LayoutParams.FILL_PARENT,
LayoutParams.WRAP_CONTENT));
/* Add Button to row. */
tr.addView(b);
/* Add row to TableLayout. */
tl.addView(tr,new TableLayout.LayoutParams(
LayoutParams.FILL_PARENT,
LayoutParams.WRAP_CONTENT));
setContentView(tl);
【问题讨论】:
-
错误指向哪一行代码?
-
错误提到了一个ScrollView?我在您的代码中看不到任何滚动视图。如果您知道它的来源,您可能会找到答案
-
尝试清理和构建您的项目,因为使用此布局和此代码,您无法获得 ClassCastException 由于 ScrollView...
标签: android dynamic row tablelayout classcastexception