我在滚动视图中有一个表格布局,我想用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)
CODE:
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>
感谢您的帮助。 谢谢。
答案 0 :(得分:3)
看起来问题是由TR.setLayoutParams(tableRowParams);
造成的( java.lang.ClassCastException:android.widget.TableLayout $ LayoutParams无法转换为android.widget.TableRow $ LayoutParams )
因为tableRowParams(声明:TableLayout.LayoutParams tableRowParams
)不是 TableRow.LayoutParams
尝试替换
TableLayout.LayoutParams tableRowParams=new TableLayout.LayoutParams (TableLayout.LayoutParams.MATCH_PARENT,TableLayout.LayoutParams.MATCH_PARENT,1.0f);
(已更新)
TableRow.LayoutParams tableRowParams=new TableRow.LayoutParams (TableRow.LayoutParams.MATCH_PARENT,TableRow.LayoutParams.MATCH_PARENT,1.0f);
和修改
TR.addView(editText1, new TableRow.LayoutParams (TableRow.LayoutParams.MATCH_PARENT,TableRow.LayoutParams.MATCH_PARENT,1.0f));
TR.addView(imgview, new TableRow.LayoutParams (TableRow.LayoutParams.MATCH_PARENT,TableRow.LayoutParams.MATCH_PARENT,1.0f));
除去
imgview.setLayoutParams(new TableLayout.LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT,1.0f));
和
editText1.setLayoutParams(new TableLayout.LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT,1.0f));
问题是由于添加到特定所有者的特定控件的LayoutParams创建不正确引起的,例如:如果您要向LinearLayout添加控件 - 您应该使用LinearLayout.LayoutParams作为该控件,TableLayout.LayoutParams - 以防您将控件添加到TableLayout,......等......