tags:

views:

81

answers:

1

I get this exception when I try to inflate

07-22 19:15:39.903: ERROR/AndroidRuntime(3810): Caused by: java.lang.ClassCastException: android.widget.RelativeLayout

I have a base class:

public class UIBase extends RelativeLayout {}

And a more specific class:

public class Countdown extends UIBase {}

Then I try to inflate and it exceptions:

UIBase newView = (UIBase) inflater.inflate(layoutId, parent, true);

Here's the XML file:

<?xml version="1.0" encoding="utf-8"?>
<com.Countdown xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/countdown"
    android:layout_alignParentTop="true"
    android:layout_alignParentRight="true"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    >
<TextView
    android:id="@+id/countdownText"
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content"
    android:layout_centerInParent="true"
    android:text="0" />
</com.Countdown>

Does anyone know what I'm doing wrong?

+1  A: 

As per the documentation, inflate() returns "the root View of the inflated hierarchy. If root was supplied and attachToRoot is true, this is root; otherwise it is the root of the inflated XML file."

Since you passed in true, the return value is "parent", which is apparently not derived from UIBase.

EboMike
This was it. I had to pass false though, not simply leave out the true. Good catch, thanks.
Joren