Android View inflateException

Darko Petkovski picture Darko Petkovski · Aug 4, 2013 · Viewed 35.7k times · Source

I'm trying to create a custom view - finger paint, and I'm having trouble with this error and I don't know how to fix it. Can anybody tell me what is wrong?

LogCat:

08-04 08:34:01.171: E/AndroidRuntime(15664): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.boenka.mk/com.boenka.mk.Draw}: android.view.InflateException: Binary XML file line #9: Error inflating class com.boenka.mk.DrawPanel
08-04 08:34:01.171: E/AndroidRuntime(15664):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1956)
08-04 08:34:01.171: E/AndroidRuntime(15664):    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1981)
08-04 08:34:01.171: E/AndroidRuntime(15664):    at android.app.ActivityThread.access$600(ActivityThread.java:123)
08-04 08:34:01.171: E/AndroidRuntime(15664):    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1147)
08-04 08:34:01.171: E/AndroidRuntime(15664):    at android.os.Handler.dispatchMessage(Handler.java:99)
08-04 08:34:01.171: E/AndroidRuntime(15664):    at android.os.Looper.loop(Looper.java:137)
08-04 08:34:01.171: E/AndroidRuntime(15664):    at android.app.ActivityThread.main(ActivityThread.java:4424)
08-04 08:34:01.171: E/AndroidRuntime(15664):    at java.lang.reflect.Method.invokeNative(Native Method)
08-04 08:34:01.171: E/AndroidRuntime(15664):    at java.lang.reflect.Method.invoke(Method.java:511)
08-04 08:34:01.171: E/AndroidRuntime(15664):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:784)
08-04 08:34:01.171: E/AndroidRuntime(15664):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551)
08-04 08:34:01.171: E/AndroidRuntime(15664):    at dalvik.system.NativeStart.main(Native Method)
08-04 08:34:01.171: E/AndroidRuntime(15664): Caused by: android.view.InflateException: Binary XML file line #9: Error inflating class com.boenka.mk.DrawPanel
08-04 08:34:01.171: E/AndroidRuntime(15664):    at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:691)
08-04 08:34:01.171: E/AndroidRuntime(15664):    at android.view.LayoutInflater.rInflate(LayoutInflater.java:739)
08-04 08:34:01.171: E/AndroidRuntime(15664):    at android.view.LayoutInflater.inflate(LayoutInflater.java:489)
08-04 08:34:01.171: E/AndroidRuntime(15664):    at android.view.LayoutInflater.inflate(LayoutInflater.java:396)
08-04 08:34:01.171: E/AndroidRuntime(15664):    at android.view.LayoutInflater.inflate(LayoutInflater.java:352)
08-04 08:34:01.171: E/AndroidRuntime(15664):    at com.android.internal.policy.impl.PhoneWindow.setContentView(PhoneWindow.java:251)
08-04 08:34:01.171: E/AndroidRuntime(15664):    at android.app.Activity.setContentView(Activity.java:1835)
08-04 08:34:01.171: E/AndroidRuntime(15664):    at com.boenka.mk.Draw.onCreate(Draw.java:49)
08-04 08:34:01.171: E/AndroidRuntime(15664):    at android.app.Activity.performCreate(Activity.java:4465)
08-04 08:34:01.171: E/AndroidRuntime(15664):    at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1049)
08-04 08:34:01.171: E/AndroidRuntime(15664):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1920)
08-04 08:34:01.171: E/AndroidRuntime(15664):    ... 11 more
08-04 08:34:01.171: E/AndroidRuntime(15664): Caused by: java.lang.ClassNotFoundException: com.boenka.mk.DrawPanel
08-04 08:34:01.171: E/AndroidRuntime(15664):    at dalvik.system.BaseDexClassLoader.findClass(BaseDexClassLoader.java:61)
08-04 08:34:01.171: E/AndroidRuntime(15664):    at java.lang.ClassLoader.loadClass(ClassLoader.java:501)
08-04 08:34:01.171: E/AndroidRuntime(15664):    at java.lang.ClassLoader.loadClass(ClassLoader.java:461)
08-04 08:34:01.171: E/AndroidRuntime(15664):    at android.view.LayoutInflater.createView(LayoutInflater.java:552)
08-04 08:34:01.171: E/AndroidRuntime(15664):    at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:680)
08-04 08:34:01.171: E/AndroidRuntime(15664):    ... 21 more

Here is my DrawingPanel class:

public class DrawingPanel extends View implements OnTouchListener {

    private Canvas mCanvas;
    private Path mPath;
    private Paint mPaint, mBitmapPaint;
    private ArrayList<PathPoints> paths = new ArrayList<PathPoints>();
    private ArrayList<PathPoints> undonePaths = new ArrayList<PathPoints>();
    private Bitmap mBitmap;
    private int x, y;
    private int color;

    public DrawingPanel(Context context, AttributeSet attrs) {
        super(context, attrs);
        this.color = Color.BLACK;
        // setFocusable(true);
        // setFocusableInTouchMode(true);
        this.setOnTouchListener(this);
        mBitmapPaint = new Paint(Paint.DITHER_FLAG);
        mPaint = new Paint();
        mPaint.setAntiAlias(true);
        mPaint.setDither(true);
        mPaint.setColor(color);
        mPaint.setStyle(Paint.Style.STROKE);
        mPaint.setStrokeJoin(Paint.Join.ROUND);
        mPaint.setStrokeCap(Paint.Cap.ROUND);
        mPaint.setStrokeWidth(3);
        mPaint.setTextSize(30);

        mPath = new Path();
        paths.add(new PathPoints(mPath, color, false));
        mCanvas = new Canvas();
    }

    public void colorChanged(int color) {
        this.color = color;
        mPaint.setColor(color);
    }

    @Override
    protected void onSizeChanged(int w, int h, int oldw, int oldh) {
        super.onSizeChanged(w, h, oldw, oldh);
        // mBitmap = AddReportItemActivity.mPhoto;
        float xscale = (float) w / (float) mBitmap.getWidth();
        float yscale = (float) h / (float) mBitmap.getHeight();
        if (xscale > yscale) // make sure both dimensions fit (use the
                                // smaller scale)
            xscale = yscale;
        float newx = (float) w * xscale;
        float newy = (float) h * xscale; // use the same scale for both
                                            // dimensions
        // if you want it centered on the display (black borders)
        mBitmap = Bitmap.createScaledBitmap(mBitmap, this.getWidth(),
                this.getHeight(), true);
        // mCanvas = new Canvas(mBitmap);
    }

    @Override
    protected void onDraw(Canvas canvas) {
        canvas.drawBitmap(mBitmap, 0, 0, mBitmapPaint);
        for (PathPoints p : paths) {
            mPaint.setColor(p.getColor());
            Log.v("", "Color code : " + p.getColor());
            if (p.isTextToDraw()) {
                canvas.drawText(p.textToDraw, p.x, p.y, mPaint);
            } else {
                canvas.drawPath(p.getPath(), mPaint);
            }
        }
    }

    private float mX, mY;
    private static final float TOUCH_TOLERANCE = 0;

    private void touch_start(float x, float y) {
        mPath.reset();
        mPath.moveTo(x, y);
        mX = x;
        mY = y;
    }

    private void touch_move(float x, float y) {
        float dx = Math.abs(x - mX);
        float dy = Math.abs(y - mY);
        if (dx >= TOUCH_TOLERANCE || dy >= TOUCH_TOLERANCE) {
            mPath.quadTo(mX, mY, (x + mX) / 2, (y + mY) / 2);
            mX = x;
            mY = y;
        }
    }

    private void touch_up() {
        mPath.lineTo(mX, mY);
        // commit the path to our offscreen
        mCanvas.drawPath(mPath, mPaint);
        // kill this so we don't double draw
        mPath = new Path();
        paths.add(new PathPoints(mPath, color, false));

    }

    @Override
    public boolean onTouch(View arg0, MotionEvent event) {
        float x = event.getX();
        float y = event.getY();

        switch (event.getAction()) {
        case MotionEvent.ACTION_DOWN:
            touch_start(x, y);
            invalidate();

            break;
        case MotionEvent.ACTION_MOVE:
            touch_move(x, y);
            invalidate();
            break;
        case MotionEvent.ACTION_UP:

            touch_up();
            invalidate();

            break;
        }
        return true;
    }

    public void onClickUndo() {
        if (paths.size() > 0) {
            undonePaths.add(paths.remove(paths.size() - 1));
            invalidate();
        } else {

        }
        // toast the user
    }

    public void onClickRedo() {
        if (undonePaths.size() > 0) {
            paths.add(undonePaths.remove(undonePaths.size() - 1));
            invalidate();
        } else {

        }
    }

    class PathPoints {
        private Path path;
        // private Paint mPaint;
        private int color;
        private String textToDraw;
        private boolean isTextToDraw;
        private int x, y;

        public PathPoints(Path path, int color, boolean isTextToDraw) {
            this.path = path;
            this.color = color;
            this.isTextToDraw = isTextToDraw;
        }

        public PathPoints(int color, String textToDraw, boolean isTextToDraw,
                int x, int y) {
            this.color = color;
            this.textToDraw = textToDraw;
            this.isTextToDraw = isTextToDraw;
            this.x = x;
            this.y = y;
        }

        public Path getPath() {
            return path;
        }

        public void setPath(Path path) {
            this.path = path;
        }

        public int getColor() {
            return color;
        }

        public void setColor(int color) {
            this.color = color;
        }

        public String getTextToDraw() {
            return textToDraw;
        }

        public void setTextToDraw(String textToDraw) {
            this.textToDraw = textToDraw;
        }

        public boolean isTextToDraw() {
            return isTextToDraw;
        }

        public void setTextToDraw(boolean isTextToDraw) {
            this.isTextToDraw = isTextToDraw;
        }

        public int getX() {
            return x;
        }

        public void setX(int x) {
            this.x = x;
        }

        public int getY() {
            return y;
        }

        public void setY(int y) {
            this.y = y;
        }

    }

    public int getColor() {
        return color;
    }

    public void clear() {
        invalidate();
    }

    public void setBrushSize(float size) {
        mPaint.setStrokeWidth(size);

    }

    public float getBrushSize() {
        return mPaint.getStrokeWidth();
    }
}

And my XML layout:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/signImageParent"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@android:color/white"
    android:orientation="vertical" >

    <com.boenka.mk.DrawingPanel
        android:id="@+id/signatureview"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent" />

    <LinearLayout
        android:gravity="center_vertical"
        android:paddingTop="10dp"
        android:paddingBottom="10dp"
        android:layout_alignParentLeft="true"
        android:id="@+id/control_panel"
        android:layout_width="50dp"
        android:background="#00AEEC"
        android:layout_height="fill_parent"
        android:orientation="vertical" >

        <Button
            android:layout_gravity="center_horizontal"
            android:id="@+id/colorPicker"
            android:background="@drawable/color_picker"
            android:layout_width="40dp"
            android:layout_margin="5dp"
            android:layout_height="40dp" />
        <Button
            android:layout_gravity="center_horizontal"
            android:id="@+id/save_img"
            android:layout_margin="5dp"
            android:background="@drawable/save_as"
            android:layout_width="40dp"
            android:layout_height="40dp" />

        <Button
            android:layout_gravity="center_horizontal"
            android:id="@+id/brush_size"
            android:layout_margin="5dp"
            android:background="@drawable/brush_size"
            android:layout_width="40dp"
            android:layout_height="40dp" />

        <Button
            android:layout_gravity="center_horizontal"
            android:id="@+id/settings"
            android:layout_margin="5dp"
            android:background="@drawable/setting"
            android:layout_width="40dp"
            android:layout_height="40dp" />

        <Button
            android:layout_gravity="center_horizontal"
            android:id="@+id/previous"
            android:layout_margin="5dp"
            android:background="@drawable/previous"
            android:layout_width="40dp"
            android:layout_height="40dp" />
    </LinearLayout>

    <ImageView
        android:id="@+id/imageView1"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:layout_toRightOf="@+id/control_panel" />

</RelativeLayout>

Answer

Vikram picture Vikram · Aug 4, 2013

From the posted Logcat output:

Caused by: android.view.InflateException: Binary XML file line #9: 
                                           Error inflating class com.boenka.mk.DrawPanel
....
....
Caused by: java.lang.ClassNotFoundException: com.boenka.mk.DrawPanel

It seems that you have accidentally misspelled the custom view's name in your xml layout file:

<com.boenka.mk.DrawPanel
    android:id="@+id/signatureview"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent" />

See if changing com.boenka.mk.DrawPanel to com.boenka.mk.DrawingPanel fixes the problem.