Search code examples
androidandroid-custom-view

CastClassException on Custom View


When I try to findViewById() on my custom view I keep getting a ClassCastException. I've tried so many things that I'm sure I've botched the code now!

To make sure I'm not going insane I stripped down the classes to their bare minimum inorder to find what was wrong.

I'm new to android programming and I'm sure I'm missing something basic.

This is BaseImageView an extended view class.

package com.company.product.client.android.gui.views;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.view.View;

public class BaseImageView
    extends View
{
    public BaseImageView(Context context)
    {
        super(context);
    }

    @Override
    protected void onDraw(Canvas canvas)
    {
        super.onDraw(canvas);
        canvas.drawColor(Color.GREEN);
    }
}

This is LiveImageView an extension of the BaseImageView class.

package com.company.product.client.android.gui.views;

import android.content.Context;
import android.util.AttributeSet;

public class LiveImageView
    extends BaseImageView
{
    public LiveImageView(Context context, AttributeSet attrs)
    {
        super(context);
    }
}

Here is the Layout my_view.xml.

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical" 
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:gravity="center">


    <View
        class="com.company.product.client.android.gui.views.LiveImageView"
        android:id="@+id/lvImage"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" />

</LinearLayout>

And here is the onCreate in my Activity LiveViewActivity.

@Override
public void onCreate(Bundle savedInstanceState)
{
    super.onCreate(savedInstanceState);

    try
    {
        setContentView(R.layout.my_view);
        final LiveImageView lvImage = (LiveImageView) findViewById(R.id.lvImage);
    }
    catch (final Exception e)
    {
        Log.e(TAG, "onCreate() Exception: " + e.toString());
        e.printStackTrace();
    }

Finally, this is stack trace.

02-11 17:25:24.829: ERROR/LiveViewActivity(1942): onCreate() Exception: java.lang.ClassCastException: android.view.View
02-11 17:25:24.839: WARN/System.err(1942): java.lang.ClassCastException: android.view.View
02-11 17:25:24.839: WARN/System.err(1942):     at com.company.product.client.android.gui.screen.LiveViewActivity.onCreate(LiveViewActivity.java:26)
02-11 17:25:24.839: WARN/System.err(1942):     at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
02-11 17:25:24.849: WARN/System.err(1942):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2459)
02-11 17:25:24.849: WARN/System.err(1942):     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2512)
02-11 17:25:24.849: WARN/System.err(1942):     at android.app.ActivityThread.access$2200(ActivityThread.java:119)
02-11 17:25:24.849: WARN/System.err(1942):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1863)
02-11 17:25:24.859: WARN/System.err(1942):     at android.os.Handler.dispatchMessage(Handler.java:99)
02-11 17:25:24.859: WARN/System.err(1942):     at android.os.Looper.loop(Looper.java:123)
02-11 17:25:24.859: WARN/System.err(1942):     at android.app.ActivityThread.main(ActivityThread.java:4363)
02-11 17:25:24.869: WARN/System.err(1942):     at java.lang.reflect.Method.invokeNative(Native Method)
02-11 17:25:24.869: WARN/System.err(1942):     at java.lang.reflect.Method.invoke(Method.java:521)
02-11 17:25:24.869: WARN/System.err(1942):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:860)
02-11 17:25:24.869: WARN/System.err(1942):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:618)
02-11 17:25:24.879: WARN/System.err(1942):     at dalvik.system.NativeStart.main(Native Method)

Solution

  • When using in Layout xml files, your custom view should be:

    <com.company.product.client.android.gui.views.LiveImageView
        android:layout_width="..."
        ...
    />
    

    Moreover, make sure you define the other two variants of constructors for views:

    public BaseImageView(Context, AttributeSet);
    public BaseImageView(Context, AttributeSet, int);
    

    Since the layout inflater actually need that two variants to create the instance.