将行添加到表布局时发生类转换异常
问题已解决:我应该清理该项目。感谢您的回答,
嘿伙计们,我试图在执行期间向表布局添加行,但它给出了错误
12-02 20:29:47.588: ERROR/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);
problem solved: i should have cleaned the project. thanks for the answers
hey guys i'm trying to add rows to table layout durign execution but it gives an error
12-02 20:29:47.588: ERROR/AndroidRuntime(569): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.project.andr/com.project.andr.MainActivity}: java.lang.ClassCastException: android.widget.ScrollView
here is my xml file
<?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>
and here is my java code
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);
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
看起来类转换异常是与您的 ScrollView 相关,而不是与您的 TableLayout 相关。提供整个堆栈跟踪或检查以确保正确使用 ScrollView。
It looks like the class cast exception is with your ScrollView, not your TableLayout. Give your whole stack trace or check to make sure you're using your ScrollView properly.
清理项目为我完成了工作。
cleaning the project did the work for me.