自定义ImageView的崩溃程序 [英] Custom ImageView crashes program

查看:126
本文介绍了自定义ImageView的崩溃程序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想提出一个小的应用程序为Android,在那里我有一个RelativeLayout的,其中除其他事项外包含自定义ImageView的。在我的Java code我有这个类:

I am making a small app for Android, where I have a RelativeLayout, which amongst other things contains a custom ImageView. In my Java code I have this class:

package com.example.android.helloactivity;


class ArrowImageView extends ImageView {
    public ArrowImageView(Context context) {
        super(context);
    }
    public void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        canvas.drawCircle(10,10,10,null);
    }
}

然后在我的RelativeLayout的XML我有以下几点:

Then in my RelativeLayout xml I have the following:

<RelativeLayout android:layout_width="fill_parent"
            android:layout_height="fill_parent"
            android:orientation="vertical"
            android:background="#FFF"
            xmlns:android="http://schemas.android.com/apk/res/android">
    <Button ...... />
    <TextView ......./>

    <com.example.android.helloactivity.ArrowImageView
        android:id="@+id/hello_activity_bearingarrow"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" />
</RelativeLayout>

当我运行我的主类(这里没有显示),然后我的程序崩溃。如果我省略了XML参考ArrowImageView,那么它不会崩溃。

When I run my main class (not shown here) then my program crashes. If I omit the xml reference to ArrowImageView, then it does not crash.

难道我指的是我的自定义类TE方法不对,或者是怎么回事?

Am I referring to my custom class te wrong way, or what is going on?

推荐答案

在扩展视图窗口小部件,如果你打算使用它们,你还需要覆盖乘坐的AttributeSet参数的构造XML布局。

When extending the View widgets, if you plan to use them in XML layouts you need to also override the constructors that take the AttributeSet argument.

这篇关于自定义ImageView的崩溃程序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆