3

スクロールビュー内にテーブルレイアウトがあり、5Nのテーブル行で動的に入力したいと思います。各テーブル行には、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>

よろしくお願いします。ありがとうございました。

4

1 に答える 1

3

問題の原因はTR.setLayoutParams(tableRowParams);
( java.lang.ClassCastException: android.widget.TableLayout$LayoutParams cannot be cast to android.widget.TableRow$LayoutParams ) である
ように見え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 の不適切な作成によって引き起こされました。 ...など...

于 2012-09-25T10:53:53.047 に答える