我正在尝试用代码将TextViews
添加到我的xml定义的布局中。我有一个xml-sheet,其中定义了很多Views
。但是我必须在代码中添加一些视图,所以在xml-sheet中创建一个LinearLayout
:
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:id="@+id/info"
android:layout_height="wrap_content"
android:orientation="vertical">
</LinearLayout>
在这个布局中,我喜欢添加我的TextView
View linearLayout = findViewById(R.id.info);
//LinearLayout layout = (LinearLayout) findViewById(R.id.info);
TextView valueTV = new TextView(this);
valueTV.setText("hallo hallo");
valueTV.setId(5);
valueTV.setLayoutParams(new LayoutParams(
LayoutParams.FILL_PARENT,
LayoutParams.WRAP_CONTENT));
((LinearLayout) linearLayout).addView(valueTV);
但我只得到以下错误消息:
: java.lang.ClassCastException: android.widget.TextView
我该怎么做呢?
谢谢你的帮助。马丁
https://stackoverflow.com/questions/3204852
复制相似问题