NW52
NW52

Reputation: 33

Android Studio can't recognize custom view classes

I'm using Android studio for my development and this came in my face. I tried the tip which is showing below, but still nothing seem to work. The only error line I get is "java.lang.NullPointerException " no more explanation than this and the window below

    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 

and here is my XML code .... hope you can help guys :/

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

class skeleton

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

Upvotes: 1

Views: 4648

Answers (1)

jbiral
jbiral

Reputation: 1459

Use

if (!isInEditMode())

before your init() in CircleImageView.

There is multiple posts talking about this issue like this one.

EDIT: I am not sure you used the tip like this, please provide an example of what you have tried if it is the case.

Upvotes: 3

Related Questions