自定义视图上的CastClassException

tux*_*url 3 android android-custom-view

当我尝试findViewById()使用我的自定义视图时,我会继续获取ClassCastException.我已经尝试了很多东西,我确信我现在已经破坏了代码!

为了确保我不会疯狂,我将课程简化为最低限度,以便找出问题所在.

我是android编程的新手,我确信我缺少一些基本的东西.

这是BaseImageView一个扩展视图类.

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);
    }
}
Run Code Online (Sandbox Code Playgroud)

这是班级LiveImageView的延伸BaseImageView.

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);
    }
}
Run Code Online (Sandbox Code Playgroud)

这是布局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>
Run Code Online (Sandbox Code Playgroud)

这是我的活动中的onCreate 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();
    }
Run Code Online (Sandbox Code Playgroud)

最后,这是堆栈跟踪.

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)
Run Code Online (Sandbox Code Playgroud)

xan*_*ndy 6

在布局xml文件中使用时,您的自定义视图应为:

<com.company.product.client.android.gui.views.LiveImageView
    android:layout_width="..."
    ...
/>
Run Code Online (Sandbox Code Playgroud)

此外,请确保为视图定义构造函数的其他两个变体:

public BaseImageView(Context, AttributeSet);
public BaseImageView(Context, AttributeSet, int);
Run Code Online (Sandbox Code Playgroud)

由于布局inflater实际上需要两个变体来创建实例.

  • 这将起作用,(defStyle构造函数参数类型应该是一个int),但问题中发布的代码不是"View"与标记名称中的"view"不同.`view`(小写)允许您使用该属性指定类名.`View`指的是Android框架类`android.view.View`. (4认同)