我在滚动视图中有一个表格布局,我想用 5 N 表格行动态填充它们。每个表格行包含一个 ImageView 和一个 EditText。应用程序崩溃了这是完整的堆栈:
09-25 10:03:07.496: E/AndroidRuntime(1695): FATAL EXCEPTION: main
09-25 10:03:07.496: E/AndroidRuntime(1695): java.lang.ClassCastException: android.widget.TableLayout$LayoutParams cannot be cast to android.widget.TableRow$LayoutParams
09-25 10:03:07.496: E/AndroidRuntime(1695): at android.widget.TableRow.mapIndexAndColumns(TableRow.java:164)
09-25 10:03:07.496: E/AndroidRuntime(1695): at android.widget.TableRow.getVirtualChildCount(TableRow.java:149)
09-25 10:03:07.496: E/AndroidRuntime(1695): at android.widget.TableRow.getColumnsWidths(TableRow.java:289)
09-25 10:03:07.496: E/AndroidRuntime(1695): at android.widget.TableLayout.findLargestCells(TableLayout.java:504)
09-25 10:03:07.496: E/AndroidRuntime(1695): at android.widget.TableLayout.measureVertical(TableLayout.java:469)
09-25 10:03:07.496: E/AndroidRuntime(1695): at android.widget.TableLayout.onMeasure(TableLayout.java:435)
09-25 10:03:07.496: E/AndroidRuntime(1695): at android.view.View.measure(View.java:15172)
09-25 10:03:07.496: E/AndroidRuntime(1695): at android.widget.ScrollView.measureChildWithMargins(ScrollView.java:1196)
09-25 10:03:07.496: E/AndroidRuntime(1695): at android.widget.FrameLayout.onMeasure(FrameLayout.java:310)
09-25 10:03:07.496: E/AndroidRuntime(1695): at android.widget.ScrollView.onMeasure(ScrollView.java:318)
09-25 10:03:07.496: E/AndroidRuntime(1695): at android.view.View.measure(View.java:15172)
09-25 10:03:07.496: E/AndroidRuntime(1695): at android.widget.RelativeLayout.measureChildHorizontal(RelativeLayout.java:617)
09-25 10:03:07.496: E/AndroidRuntime(1695): at android.widget.RelativeLayout.onMeasure(RelativeLayout.java:399)
09-25 10:03:07.496: E/AndroidRuntime(1695): at android.view.View.measure(View.java:15172)
09-25 10:03:07.496: E/AndroidRuntime(1695): at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:4814)
09-25 10:03:07.496: E/AndroidRuntime(1695): at android.widget.FrameLayout.onMeasure(FrameLayout.java:310)
09-25 10:03:07.496: E/AndroidRuntime(1695): at android.view.View.measure(View.java:15172)
09-25 10:03:07.496: E/AndroidRuntime(1695): at android.widget.LinearLayout.measureVertical(LinearLayout.java:833)
09-25 10:03:07.496: E/AndroidRuntime(1695): at android.widget.LinearLayout.onMeasure(LinearLayout.java:574)
09-25 10:03:07.496: E/AndroidRuntime(1695): at android.view.View.measure(View.java:15172)
09-25 10:03:07.496: E/AndroidRuntime(1695): at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:4814)
09-25 10:03:07.496: E/AndroidRuntime(1695): at android.widget.FrameLayout.onMeasure(FrameLayout.java:310)
09-25 10:03:07.496: E/AndroidRuntime(1695): at com.android.internal.policy.impl.PhoneWindow$DecorView.onMeasure(PhoneWindow.java:2148)
09-25 10:03:07.496: E/AndroidRuntime(1695): at android.view.View.measure(View.java:15172)
09-25 10:03:07.496: E/AndroidRuntime(1695): at android.view.ViewRootImpl.performMeasure(ViewRootImpl.java:1848)
09-25 10:03:07.496: E/AndroidRuntime(1695): at android.view.ViewRootImpl.measureHierarchy(ViewRootImpl.java:1100)
09-25 10:03:07.496: E/AndroidRuntime(1695): at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:1273)
09-25 10:03:07.496: E/AndroidRuntime(1695): at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:998)
09-25 10:03:07.496: E/AndroidRuntime(1695): at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:4212)
09-25 10:03:07.496: E/AndroidRuntime(1695): at android.view.Choreographer$CallbackRecord.run(Choreographer.java:725)
09-25 10:03:07.496: E/AndroidRuntime(1695): at android.view.Choreographer.doCallbacks(Choreographer.java:555)
09-25 10:03:07.496: E/AndroidRuntime(1695): at android.view.Choreographer.doFrame(Choreographer.java:525)
09-25 10:03:07.496: E/AndroidRuntime(1695): at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:711)
09-25 10:03:07.496: E/AndroidRuntime(1695): at android.os.Handler.handleCallback(Handler.java:615)
09-25 10:03:07.496: E/AndroidRuntime(1695): at android.os.Handler.dispatchMessage(Handler.java:92)
09-25 10:03:07.496: E/AndroidRuntime(1695): at android.os.Looper.loop(Looper.java:137)
09-25 10:03:07.496: E/AndroidRuntime(1695): at android.app.ActivityThread.main(ActivityThread.java:4745)
09-25 10:03:07.496: E/AndroidRuntime(1695): at java.lang.reflect.Method.invokeNative(Native Method)
09-25 10:03:07.496: E/AndroidRuntime(1695): at java.lang.reflect.Method.invoke(Method.java:511)
09-25 10:03:07.496: E/AndroidRuntime(1695): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:786)
09-25 10:03:07.496: E/AndroidRuntime(1695): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
09-25 10:03:07.496: E/AndroidRuntime(1695): at dalvik.system.NativeStart.main(Native Method)
代码:
private TableLayout Table;//in XML
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//here I define the TR params
Table=(TableLayout)findViewById(R.id.Table);
TableLayout.LayoutParams tableRowParams=new TableLayout.LayoutParams
(TableLayout.LayoutParams.MATCH_PARENT,TableLayout.LayoutParams.MATCH_PARENT,1.0f);
//fill the table with Table rows dynamicly
for(int i=0;i<N;i++){
TableRow TR= new TableRow(this);
TR.setLayoutParams(tableRowParams);
Table.addView(TR);
//Table Row contains EditText and ImageView
EditText editText1 =new EditText(this);
ImageView imgview=new ImageView(this);
TR.addView(editText1);
TR.addView(imgview);
editText1.setText("foo");
editText1.setLayoutParams(new TableLayout.LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT,1.0f));
int id = getResources().getIdentifier("com.example.university2:drawable/news1", null, null);
imgview.setImageResource(id);
imgview.setAdjustViewBounds(true);
imgview.setMaxHeight(50);
imgview.setMaxWidth(50);
imgview.setLayoutParams(new TableLayout.LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT,1.0f));
}
}
这是XML
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<ScrollView
android:id="@+id/scrollView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentRight="true"
android:layout_alignParentTop="true" >
<TableLayout
android:id="@+id/Table"
android:layout_width="wrap_content"
android:layout_height="wrap_content" >
</TableLayout>
</ScrollView>
我会很感激你的帮助。谢谢你。