I'm trying to programmatically add TextViews to my layout. I want these TextViews to have a custom style, so I put this in res/layout/listitem_tpl.xml
:
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="match_parent"
android:textSize="25sp" >
</TextView>
Blindly following this answer with no real understanding of what I was doing, I put this in my onCreate:
LayoutInflater inflater = LayoutInflater.from(this);
TextView text = (TextView) inflater.inflate(R.layout.listitem_tpl, (ViewGroup) getWindow().getDecorView().getRootView());
text.setText("Hello, World!");
I wasn't sure what to put in for the second argument. I knew I needed the "parent view", which I assume is what I set with setContentView, but there's no getContentView so I cannibalized together that expression based on some information I found through googling. Is that line the problem?
In any case, the error I'm getting is:
12-24 11:52:12.370: E/AndroidRuntime(2677): Caused by: java.lang.ClassCastException: com.android.internal.policy.impl.PhoneWindow$DecorView cannot be cast to android.widget.Button
I looked up LayoutInflater.inflate and found that it should return a View. TextView is a subclass of View, so where's the problem?