Android TextView casting error: android.widget.LinearLayout cannot be cast to android.widget.TextView

StackOverflow https://stackoverflow.com/questions/7535396

Question

I have the following textview in a layout file, this is the full contents of the layout file:

<?xml version="1.0" encoding="utf-8"?>
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/seasonTitle"
    android:padding="3dip"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:textColor="#FFFFFFFF"
    android:textSize="14sp"
    android:textStyle="bold"
    android:maxHeight="26sp"
    android:background="#FFCC3333"
     />

It has been in my app for a year or so with no problems. All of the sudden I'm getting the error: android.widget.LinearLayout cannot be cast to android.widget.TextView

I can't figure it out.

The layout file is inflated with code like this:

TextView seasonTv = (TextView)mInflater.inflate(R.layout.section_title, null);

Any insight on why this is happening?

Thanks!

Was it helpful?

Solution

It appears that you are passing in null as the root element of the view group. I would assume that the inflater is defaulting your view group to a linear layout, when you don't actually have a view group at all. I would try to modify your inflate call, as your layout file appears OK.

source: http://developer.android.com/reference/android/view/LayoutInflater.html

OTHER TIPS

try this

View season = (View)mInflater.inflate(R.layout.section_title, null);
TextView seasonTv =  (TextView)findViewById(R.id.seasonTitle);

You trying to inflate layout and cast it to TextView widget. You should inflate layout and find widget

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top