0

xml から ScrollView 内の RelativeLayout にカスタム ビューを追加しようとしています。カスタム ビューには、いくつかの TextView が含まれています。TextViews のテキストを設定し、ビューの幅、高さ、および topMargin を設定して、relativeLayout に追加します。しかし、エラーが発生し続け、その理由がわかりません。

@Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        RelativeLayout board = (RelativeLayout) findViewById(R.id.Dashboard);
        List<Data> dummyData = new InitData().getDummyData();

        for (Data data : dummyData) {
            View view = null;
            LayoutInflater inflater = LayoutInflater.from(MainActivity.this);
            view = inflater.inflate(R.layout.my_view, board);
            TextView titleTextView = (TextView) view
                    .findViewById(R.id.title_text_view);
            titleTextView.setText(data.getTitle()); //I think error occurs here

            int width = LayoutParams.MATCH_PARENT;
            int height = data.getHeight();
            RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(
                    width, height);
            params.topMargin = data.getTopMargin();
            view.setLayoutParams(params);
        }
    }

LogCat エラー

02-21 20:33:56.903: E/AndroidRuntime(3322): FATAL EXCEPTION: main
02-21 20:33:56.903: E/AndroidRuntime(3322): java.lang.ClassCastException: android.widget.RelativeLayout$LayoutParams
02-21 20:33:56.903: E/AndroidRuntime(3322):     at android.widget.LinearLayout.measureHorizontal(LinearLayout.java:654)
02-21 20:33:56.903: E/AndroidRuntime(3322):     at android.widget.LinearLayout.onMeasure(LinearLayout.java:306)
02-21 20:33:56.903: E/AndroidRuntime(3322):     at android.view.View.measure(View.java:8171)
02-21 20:33:56.903: E/AndroidRuntime(3322):     at android.widget.ScrollView.measureChildWithMargins(ScrollView.java:989)
02-21 20:33:56.903: E/AndroidRuntime(3322):     at android.widget.FrameLayout.onMeasure(FrameLayout.java:245)
02-21 20:33:56.903: E/AndroidRuntime(3322):     at android.widget.ScrollView.onMeasure(ScrollView.java:286)
02-21 20:33:56.903: E/AndroidRuntime(3322):     at android.view.View.measure(View.java:8171)
02-21 20:33:56.903: E/AndroidRuntime(3322):     at android.widget.RelativeLayout.measureChildHorizontal(RelativeLayout.java:578)
02-21 20:33:56.903: E/AndroidRuntime(3322):     at android.widget.RelativeLayout.onMeasure(RelativeLayout.java:362)
02-21 20:33:56.903: E/AndroidRuntime(3322):     at android.view.View.measure(View.java:8171)
02-21 20:33:56.903: E/AndroidRuntime(3322):     at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:3132)
02-21 20:33:56.903: E/AndroidRuntime(3322):     at android.widget.FrameLayout.onMeasure(FrameLayout.java:245)
02-21 20:33:56.903: E/AndroidRuntime(3322):     at android.view.View.measure(View.java:8171)
02-21 20:33:56.903: E/AndroidRuntime(3322):     at android.widget.LinearLayout.measureVertical(LinearLayout.java:526)
02-21 20:33:56.903: E/AndroidRuntime(3322):     at android.widget.LinearLayout.onMeasure(LinearLayout.java:304)
02-21 20:33:56.903: E/AndroidRuntime(3322):     at android.view.View.measure(View.java:8171)
02-21 20:33:56.903: E/AndroidRuntime(3322):     at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:3132)
02-21 20:33:56.903: E/AndroidRuntime(3322):     at android.widget.FrameLayout.onMeasure(FrameLayout.java:245)
02-21 20:33:56.903: E/AndroidRuntime(3322):     at android.view.View.measure(View.java:8171)
02-21 20:33:56.903: E/AndroidRuntime(3322):     at android.view.ViewRoot.performTraversals(ViewRoot.java:801)
02-21 20:33:56.903: E/AndroidRuntime(3322):     at android.view.ViewRoot.handleMessage(ViewRoot.java:1727)
02-21 20:33:56.903: E/AndroidRuntime(3322):     at android.os.Handler.dispatchMessage(Handler.java:99)
02-21 20:33:56.903: E/AndroidRuntime(3322):     at android.os.Looper.loop(Looper.java:123)
02-21 20:33:56.903: E/AndroidRuntime(3322):     at android.app.ActivityThread.main(ActivityThread.java:4627)
02-21 20:33:56.903: E/AndroidRuntime(3322):     at java.lang.reflect.Method.invokeNative(Native Method)
02-21 20:33:56.903: E/AndroidRuntime(3322):     at java.lang.reflect.Method.invoke(Method.java:521)
02-21 20:33:56.903: E/AndroidRuntime(3322):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868)
02-21 20:33:56.903: E/AndroidRuntime(3322):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)
02-21 20:33:56.903: E/AndroidRuntime(3322):     at dalvik.system.NativeStart.main(Native Method)

R.layout.my_view

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="vertical" >

    <TextView
        android:id="@+id/title_text_view"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" />

    <TextView
        android:id="@+id/data_text_view"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" />

</LinearLayout>
4

1 に答える 1

0

間違った種類の を使用しているようですLayoutParams。LayoutParams は、子を追加する親ビューと一致する必要があることに注意してください。たとえば LinearLayout があり、それに RelativeLayout を追加したい場合は、LinearLayout.LayoutParamsではなくを使用する必要がありますRelativeLayout.LayoutParams

于 2013-02-21T13:49:17.373 に答える