Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Class cast exception when inflating a custom Relative Layout?

Tags:

android

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?

like image 648
Joren Avatar asked Jul 23 '10 02:07

Joren


1 Answers

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.

like image 118
EboMike Avatar answered Oct 13 '22 01:10

EboMike