android - 自定义ImageView崩溃程序

标签 android android-layout

我正在为 Android 制作一个小应用程序,其中有一个 RelativeLayout,其中包含一个自定义 ImageView。在我的 Java 代码中,我有这个类:

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 中有以下内容:

<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>

当我运行主类(此处未显示)时,我的程序崩溃了。如果我省略对 ArrowImageView 的 xml 引用,则它不会崩溃。

我是不是以错误的方式引用了我的自定义类,或者这是怎么回事?

最佳答案

当扩展 View 小部件时,如果您计划在 XML 布局中使用它们,您还需要覆盖采用 AttributeSet 参数的构造函数。

关于android - 自定义ImageView崩溃程序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5667583/

相关文章:

java - 带有每个选项卡菜单的操作栏

Android 存储多个通知 ID 并允许同时显示多个通知

android View 在动画之前暂时显示在最终位置

android - Android 应用中的分段控件类型

android - 如何在 Android 中创建像图像一样的进度条

android - 如何根据 LinearLayout 自己的样式设置 LinearLayout 的 subview 的样式?

android - Android API 级别 10 (2.3) 中的 LinearLayout,背景影响整个元素

android - 用户属性不可用于通知

java - Android Spinner getView

android - RxAndroid - 在 Activity 被销毁时保存数据