Android Studio无法识别自定义视图类

时间:2014-10-06 23:46:18

标签: java android xml android-studio

我使用Android工作室进行开发,这一切都在我的脸上。我尝试了下面显示的提示,但似乎仍然没有任何效果。我得到的唯一错误行是" java.lang.NullPointerException"没有比这个和下面的窗口更多的解释

    Rendering Problems The following classes could not be instantiated:
- com.xxx.xxx.util.CircleImageView 
           (Open Class, Show Exception)
   Tip: Use View.isInEditMode() in your custom views to skip code
      or show sample data when shown in the IDE  Exception Details
      java.lang.NullPointerException 

这是我的XML代码....希望你能帮助他们:/

<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@color/white">

    <LinearLayout
        android:layout_height="match_parent"
        android:layout_width="match_parent">

        <LinearLayout
            android:layout_height="240dp"
            android:layout_width="match_parent"
            android:orientation="vertical">

            <com.xxx.xxx.util.CustomImageButton
                android:layout_height="match_parent"
                android:layout_width="match_parent"
                android:background="@drawable/ic_launcher" />

            <com.xxx.xxx.util.CircleImageView
                android:layout_width="100dp"
                android:layout_height="100dp"
                android:background="@drawable/ic_plusone_tall_off_client" />
        </LinearLayout>
    </LinearLayout>

</FrameLayout>

类骨架

public class CircleImageView extends ImageView {
    // Border & Selector configuration variables
    private boolean hasBorder;
    private boolean hasSelector;
    private boolean isSelected;
    private int borderWidth;
    private int canvasSize;
    private int selectorStrokeWidth;

    // Objects used for the actual drawing
    private BitmapShader shader;
    private Bitmap image;
    private Paint paint;
    private Paint paintBorder;
    private Paint paintSelectorBorder;
    private ColorFilter selectorFilter;

    public CircleImageView(Context context) {
        this(context, null);
    }

    public CircleImageView(Context context, AttributeSet attrs) {
        this(context, attrs, R.attr.CircleImageViewStyle);
    }

    public CircleImageView(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
        init(context, attrs, defStyle);
    }

    /**
     * Initializes paint objects and sets desired attributes.
     *
     * @param context
     * @param attrs
     * @param defStyle
     */
    private void init(Context context, AttributeSet attrs, int defStyle) {
       ...code
    }

    /**
     * Sets the CircleImageView's border width in pixels.
     *
     * @param borderWidth
     */
    public void setBorderWidth(int borderWidth) {
    ...code
    }

    /**
     * Sets the CircleImageView's basic border color.
     *
     * @param borderColor
     */
    public void setBorderColor(int borderColor) {
    ...code
    }

    /**
     * Sets the color of the selector to be draw over the
     * CircleImageView. Be sure to provide some opacity.
     *
     * @param selectorColor
     */
    public void setSelectorColor(int selectorColor) {
    ...code
    }

    /**
     * Sets the stroke width to be drawn around the CircleImageView
     * during click events when the selector is enabled.
     *
     * @param selectorStrokeWidth
     */
    public void setSelectorStrokeWidth(int selectorStrokeWidth) {
    ...code
    }

    /**
     * Sets the stroke color to be drawn around the CircleImageView
     * during click events when the selector is enabled.
     */
    public void setSelectorStrokeColor(int selectorStrokeColor) {
    ...code
    }

    /**
     * Adds a dark shadow to this CircleImageView.
     */
    public void addShadow() {
    ...code
    }

    @Override
    public void onDraw(Canvas canvas) {
        ...code
    }

    @Override
    public boolean dispatchTouchEvent(MotionEvent event) {
    ...code
    }

    public void invalidate(Rect dirty) {
    ...code
    }

    public void invalidate(int l, int t, int r, int b) {
    ...code
    }

    @Override
    public void invalidate() {
    ...code
    }

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
    ...code
    }

    private int measureWidth(int measureSpec) {
    ...code
    }

    private int measureHeight(int measureSpecHeight) {
    ...code
    }

    /**
     * Convert a drawable object into a Bitmap
     *
     * @param drawable
     * @return
     */
    public Bitmap drawableToBitmap(Drawable drawable) {
    ...code
    }

    /**
     * Reinitializes the shader texture used to fill in
     * the Circle upon drawing.
     */
    public void refreshBitmapShader() {
    ...code
    }

    /**
     * Returns whether or not this view is currently
     * in its selected state.
     */
    public boolean isSelected() {
    ...code
    }

    @Override
    public boolean isInEditMode() {
    ...code
    }
} 

1 个答案:

答案 0 :(得分:3)

使用

if (!isInEditMode())

在CircleImageView中的init()之前。

有很多帖子都在谈论这个问题,例如this one

编辑:我不确定你是否使用了这样的提示,请提供一个例子,说明你是否已经尝试过。