0

i have a DrawingView class, that class creates an drawing view for. Code of that class you can see below. But the problem occurs when i want to create an instance of that class in xml file. What im doing wrong?

code of xml :

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#FFCCCCCC"
android:orientation="vertical"
tools:context=".MainActivity" >

<LinearLayout
    android:layout_width="wrap_content"
    android:layout_height="50dp"
    android:layout_gravity="center"
    android:orientation="horizontal" >

    <ImageButton
        android:id="@+id/new_btn"
        android:layout_width="wrap_content"
        android:layout_height="fill_parent"
        android:contentDescription="@string/start_new"
        android:src="@drawable/new_pic" />

    <ImageButton
        android:id="@+id/draw_btn"
        android:layout_width="wrap_content"
        android:layout_height="fill_parent"
        android:contentDescription="@string/brush"
        android:src="@drawable/brush" />

    <ImageButton
        android:id="@+id/erase_btn"
        android:layout_width="wrap_content"
        android:layout_height="fill_parent"
        android:contentDescription="@string/erase"
        android:src="@drawable/eraser" />

    <ImageButton
        android:id="@+id/save_btn"
        android:layout_width="wrap_content"
        android:layout_height="fill_parent"
        android:contentDescription="@string/save"
        android:src="@drawable/save" />
</LinearLayout>

<com.example.drawingapp.DrawingView
    android:id="@+id/drawing"
    android:layout_width="fill_parent"
    android:layout_height="0dp"
    android:layout_marginBottom="3dp"
    android:layout_marginLeft="5dp"
    android:layout_marginRight="5dp"
    android:layout_marginTop="3dp"
    android:layout_weight="1"
    android:background="#FFFFFFFF" />

<LinearLayout
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_gravity="center"
    android:orientation="vertical" >

    <!-- Top Row -->

    <LinearLayout
        android:id="@+id/paint_colors"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:orientation="horizontal" >

        <ImageButton
            android:layout_width="@dimen/large_brush"
            android:layout_height="@dimen/large_brush"
            android:layout_margin="2dp"
            android:background="#FF660000"
            android:contentDescription="@string/paint"
            android:onClick="paintClicked"
            android:src="@drawable/paint"
            android:tag="#FF660000" />

        <ImageButton
            android:layout_width="@dimen/large_brush"
            android:layout_height="@dimen/large_brush"
            android:layout_margin="2dp"
            android:background="#FFFF0000"
            android:contentDescription="@string/paint"
            android:onClick="paintClicked"
            android:src="@drawable/paint"
            android:tag="#FFFF0000" />

        <ImageButton
            android:layout_width="@dimen/large_brush"
            android:layout_height="@dimen/large_brush"
            android:layout_margin="2dp"
            android:background="#FFFF6600"
            android:contentDescription="@string/paint"
            android:onClick="paintClicked"
            android:src="@drawable/paint"
            android:tag="#FFFF6600" />

        <ImageButton
            android:layout_width="@dimen/large_brush"
            android:layout_height="@dimen/large_brush"
            android:layout_margin="2dp"
            android:background="#FFFFCC00"
            android:contentDescription="@string/paint"
            android:onClick="paintClicked"
            android:src="@drawable/paint"
            android:tag="#FFFFCC00" />

        <ImageButton
            android:layout_width="@dimen/large_brush"
            android:layout_height="@dimen/large_brush"
            android:layout_margin="2dp"
            android:background="#FF009900"
            android:contentDescription="@string/paint"
            android:onClick="paintClicked"
            android:src="@drawable/paint"
            android:tag="#FF009900" />

        <ImageButton
            android:layout_width="@dimen/large_brush"
            android:layout_height="@dimen/large_brush"
            android:layout_margin="2dp"
            android:background="#FF009999"
            android:contentDescription="@string/paint"
            android:onClick="paintClicked"
            android:src="@drawable/paint"
            android:tag="#FF009999" />
    </LinearLayout>
    <!-- Bottom Row -->

    <LinearLayout
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:orientation="horizontal" >

        <ImageButton
            android:layout_width="@dimen/large_brush"
            android:layout_height="@dimen/large_brush"
            android:layout_margin="2dp"
            android:background="#FF0000FF"
            android:contentDescription="@string/paint"
            android:onClick="paintClicked"
            android:src="@drawable/paint"
            android:tag="#FF0000FF" />

        <ImageButton
            android:layout_width="@dimen/large_brush"
            android:layout_height="@dimen/large_brush"
                android:layout_margin="2dp"
                android:background="#FF990099"
                android:contentDescription="@string/paint"
                android:onClick="paintClicked"
                android:src="@drawable/paint"
                android:tag="#FF990099" />

            <ImageButton
                android:layout_width="@dimen/large_brush"
                android:layout_height="@dimen/large_brush"
                android:layout_margin="2dp"
                android:background="#FFFF6666"
                android:contentDescription="@string/paint"
                android:onClick="paintClicked"
                android:src="@drawable/paint"
                android:tag="#FFFF6666" />

            <ImageButton
                android:layout_width="@dimen/large_brush"
                android:layout_height="@dimen/large_brush"
                android:layout_margin="2dp"
                android:background="#FFFFFFFF"
                android:contentDescription="@string/paint"
                android:onClick="paintClicked"
                android:src="@drawable/paint"
                android:tag="#FFFFFFFF" />

            <ImageButton
                android:layout_width="@dimen/large_brush"
                android:layout_height="@dimen/large_brush"
                android:layout_margin="2dp"
                android:background="#FF787878"
                android:contentDescription="@string/paint"
                android:onClick="paintClicked"
                android:src="@drawable/paint"
                android:tag="#FF787878" />

            <ImageButton
                android:layout_width="@dimen/large_brush"
                android:layout_height="@dimen/large_brush"
                android:layout_margin="2dp"
                android:background="#FF000000"
                android:contentDescription="@string/paint"
                android:onClick="paintClicked"
                android:src="@drawable/paint"
                android:tag="#FF000000" />
        </LinearLayout>
    </LinearLayout>

</LinearLayout>

Code of DrawingView class :

package com.example.drawingapp;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Path;
import android.util.AttributeSet;
import android.view.MotionEvent;
import android.view.View;
import android.widget.ImageButton;
import android.widget.LinearLayout;
import android.graphics.PorterDuff;
import android.graphics.PorterDuffXfermode;
import android.util.TypedValue;

public class DrawingView extends View {
    // drawing path
    private Path drawPath;
    // drawing and canvas paint
    private Paint drawPaint, canvasPaint;
    // initial color
    private int paintColor = 0xFF660000;
    // canvas
    private Canvas drawCanvas;
    // canvas bitmap
    private Bitmap canvasBitmap;
    private ImageButton currPaint;
    private DrawingView drawView;
    private boolean erase = false;

    private float brushSize, lastBrushSize;

    public DrawingView(Context context, AttributeSet attrs) {
        super(context, attrs);
        setupDrawing();
        drawView = (DrawingView) findViewById(R.id.drawing);
        LinearLayout paintLayout = (LinearLayout) findViewById(R.id.paint_colors);
        currPaint = (ImageButton) paintLayout.getChildAt(0);
        currPaint.setImageDrawable(getResources().getDrawable(
                R.drawable.paint_pressed));

    }

    private void setupDrawing() {
        // get drawing area setup for interacion
        drawPath = new Path();
        drawPaint = new Paint();

        drawPaint.setColor(paintColor);
        drawPaint.setAntiAlias(true);
        drawPaint.setStrokeWidth(20);
        drawPaint.setStyle(Paint.Style.STROKE);
        drawPaint.setStrokeJoin(Paint.Join.ROUND);
        drawPaint.setStrokeCap(Paint.Cap.ROUND);
        canvasPaint = new Paint(Paint.DITHER_FLAG);
        brushSize = getResources().getInteger(R.integer.medium_size);
        lastBrushSize = brushSize;
        drawPaint.setStrokeWidth(brushSize);
    }

    @Override
    protected void onSizeChanged(int w, int h, int oldw, int oldh) {
        // TODO Auto-generated method stub
        super.onSizeChanged(w, h, oldw, oldh);
        canvasBitmap = Bitmap.createBitmap(w, h, Bitmap.Config.ARGB_8888);
        drawCanvas = new Canvas(canvasBitmap);
    }

    @Override
    protected void onDraw(Canvas canvas) {
        // TODO Auto-generated method stub
        canvas.drawBitmap(canvasBitmap, 0, 0, canvasPaint);
        canvas.drawPath(drawPath, drawPaint);
    }

    @Override
    public boolean onTouchEvent(MotionEvent event) {
        // TODO Auto-generated method stub
        float touchX = event.getX();
        float touchY = event.getY();
        switch (event.getAction()) {
        case MotionEvent.ACTION_DOWN:
            drawPath.moveTo(touchX, touchY);
            break;
        case MotionEvent.ACTION_MOVE:
            drawPath.lineTo(touchX, touchY);
            break;
        case MotionEvent.ACTION_UP:
            drawCanvas.drawPath(drawPath, drawPaint);
            drawPath.reset();
            break;
        default:
            return false;
        }
        invalidate();
        return true;
    }

    public void paintClicked(View view) {
        if (view != currPaint) {
            // update color
            drawView.setErase(false);
            drawView.setBrushSize(drawView.getLastBrushSize());
            ImageButton imgView = (ImageButton) view;
            String color = view.getTag().toString();
            drawView.setColor(color);
            imgView.setImageDrawable(getResources().getDrawable(
                    R.drawable.paint_pressed));
            currPaint.setImageDrawable(getResources().getDrawable(
                    R.drawable.paint));
            currPaint = (ImageButton) view;
        }
    }

    public void setColor(String newColor) {
        // set color
        invalidate();
        paintColor = Color.parseColor(newColor);
        drawPaint.setColor(paintColor);

    }

    public void setBrushSize(float newSize) {
        // update size
        float pixelAmount = TypedValue.applyDimension(
                TypedValue.COMPLEX_UNIT_DIP, newSize, getResources()
                        .getDisplayMetrics());
        brushSize = pixelAmount;
        drawPaint.setStrokeWidth(brushSize);
    }

    public void setLastBrushSize(float lastSize) {
        lastBrushSize = lastSize;
    }

    public float getLastBrushSize() {
        return lastBrushSize;
    }

    public void setErase(boolean isErase) {
        // set erase true or false
        erase = isErase;
        if (erase)
            drawPaint
                    .setXfermode(new PorterDuffXfermode(PorterDuff.Mode.CLEAR));
        else
            drawPaint.setXfermode(null);
    }

    public void startNew() {
        drawCanvas.drawColor(0, PorterDuff.Mode.CLEAR);
        invalidate();
    }
}

Logcat shows me the error:

   10-16 11:57:56.171: E/AndroidRuntime(17594): FATAL EXCEPTION: main
10-16 11:57:56.171: E/AndroidRuntime(17594): Process: com.example.drawingapp, PID: 17594
10-16 11:57:56.171: E/AndroidRuntime(17594): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.drawingapp/com.example.drawingapp.MainActivity}: android.view.InflateException: Binary XML file line #44: Error inflating class com.example.drawingapp.DrawingView.java
10-16 11:57:56.171: E/AndroidRuntime(17594):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2184)
10-16 11:57:56.171: E/AndroidRuntime(17594):    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2233)
10-16 11:57:56.171: E/AndroidRuntime(17594):    at android.app.ActivityThread.access$800(ActivityThread.java:135)
10-16 11:57:56.171: E/AndroidRuntime(17594):    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1196)
10-16 11:57:56.171: E/AndroidRuntime(17594):    at android.os.Handler.dispatchMessage(Handler.java:102)
10-16 11:57:56.171: E/AndroidRuntime(17594):    at android.os.Looper.loop(Looper.java:136)
10-16 11:57:56.171: E/AndroidRuntime(17594):    at android.app.ActivityThread.main(ActivityThread.java:5001)
10-16 11:57:56.171: E/AndroidRuntime(17594):    at java.lang.reflect.Method.invokeNative(Native Method)
10-16 11:57:56.171: E/AndroidRuntime(17594):    at java.lang.reflect.Method.invoke(Method.java:515)
10-16 11:57:56.171: E/AndroidRuntime(17594):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:785)
10-16 11:57:56.171: E/AndroidRuntime(17594):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:601)
10-16 11:57:56.171: E/AndroidRuntime(17594):    at dalvik.system.NativeStart.main(Native Method)
10-16 11:57:56.171: E/AndroidRuntime(17594): Caused by: android.view.InflateException: Binary XML file line #44: Error inflating class com.example.drawingapp.DrawingView.java
10-16 11:57:56.171: E/AndroidRuntime(17594):    at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:707)
10-16 11:57:56.171: E/AndroidRuntime(17594):    at android.view.LayoutInflater.rInflate(LayoutInflater.java:755)
10-16 11:57:56.171: E/AndroidRuntime(17594):    at android.view.LayoutInflater.inflate(LayoutInflater.java:492)
10-16 11:57:56.171: E/AndroidRuntime(17594):    at android.view.LayoutInflater.inflate(LayoutInflater.java:397)
10-16 11:57:56.171: E/AndroidRuntime(17594):    at android.view.LayoutInflater.inflate(LayoutInflater.java:353)
10-16 11:57:56.171: E/AndroidRuntime(17594):    at com.android.internal.policy.impl.PhoneWindow.setContentView(PhoneWindow.java:290)
10-16 11:57:56.171: E/AndroidRuntime(17594):    at android.app.Activity.setContentView(Activity.java:1929)
10-16 11:57:56.171: E/AndroidRuntime(17594):    at android.support.v7.app.ActionBarActivity.superSetContentView(ActionBarActivity.java:217)
10-16 11:57:56.171: E/AndroidRuntime(17594):    at android.support.v7.app.ActionBarActivityDelegateICS.setContentView(ActionBarActivityDelegateICS.java:110)
10-16 11:57:56.171: E/AndroidRuntime(17594):    at android.support.v7.app.ActionBarActivity.setContentView(ActionBarActivity.java:77)
10-16 11:57:56.171: E/AndroidRuntime(17594):    at com.example.drawingapp.MainActivity.onCreate(MainActivity.java:28)
10-16 11:57:56.171: E/AndroidRuntime(17594):    at android.app.Activity.performCreate(Activity.java:5231)
10-16 11:57:56.171: E/AndroidRuntime(17594):    at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087)
10-16 11:57:56.171: E/AndroidRuntime(17594):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2148)
10-16 11:57:56.171: E/AndroidRuntime(17594):    ... 11 more
10-16 11:57:56.171: E/AndroidRuntime(17594): Caused by: java.lang.ClassNotFoundException: Didn't find class "com.example.drawingapp.DrawingView.java" on path: DexPathList[[zip file "/data/app/com.example.drawingapp-1.apk"],nativeLibraryDirectories=[/data/app-lib/com.example.drawingapp-1, /vendor/lib, /system/lib]]
10-16 11:57:56.171: E/AndroidRuntime(17594):    at dalvik.system.BaseDexClassLoader.findClass(BaseDexClassLoader.java:56)
10-16 11:57:56.171: E/AndroidRuntime(17594):    at java.lang.ClassLoader.loadClass(ClassLoader.java:497)
10-16 11:57:56.171: E/AndroidRuntime(17594):    at java.lang.ClassLoader.loadClass(ClassLoader.java:457)
10-16 11:57:56.171: E/AndroidRuntime(17594):    at android.view.LayoutInflater.createView(LayoutInflater.java:559)
10-16 11:57:56.171: E/AndroidRuntime(17594):    at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:696)
10-16 11:57:56.171: E/AndroidRuntime(17594):    ... 24 more
4
  • What is on Line 44 in XML? Commented Oct 16, 2014 at 9:16
  • <com.example.drawingapp.DrawingView this line Commented Oct 16, 2014 at 9:17
  • Ok, the text "Error inflating class com.example.drawingapp.DrawingView " states there is an error in Your constructor, or Your setters. You could place a breakpoint on all of them and see how it works Commented Oct 16, 2014 at 9:22
  • the problem is on currPaint = (ImageButton) paintLayout.getChildAt(0); Commented Oct 16, 2014 at 9:31

2 Answers 2

0

You'll probably be able to see a NullPointerException in the LogCat too.

findViewById(R.id.paint_colors) will be returning null because there is no child view with that ID. In fact, a View class cannot have any child views.

I suspect you mean to be inflating a layout into the view, in which case you should extend ViewGroup instead of View and include the following lines underneath super(context, attrs):

LayoutInflater layoutInflater = (LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
layoutInflater.inflate(R.layout.my_layout_name, this);
Sign up to request clarification or add additional context in comments.

6 Comments

Please see my update, i don't see NullPointerException
Ah I see. It's not actually finding the class at all. I'm not quite sure how to fix your existing problem, but I guarantee you will see the NullPointerException once you have the ClassNotFoundException fixed.
Instead of Imageview, what i should use to try this ?
Just use View. It's worth a shot since it seems to have worked for them. But I've definitely seen it work the way you currently have it too, I'm just not sure why yours is failing.
can i ask for similar problem here or i should create new question ?
|
0

Why you called in your constructor this:

drawView = (DrawingView) findViewById(R.id.drawing);
LinearLayout paintLayout = (LinearLayout) findViewById(R.id.paint_colors);

You are already in your DrawingView, so you can't find DrawingView with drawing id. It's the same for paintLayout, your DrawingView haven't LinearLayout with id paint_colors. Your DrawingView is only a View not a ViewGroup

I think you have a NullPointerException at this line:

currPaint = (ImageButton) paintLayout.getChildAt(0);

Before this line test if your paintLayout is null please.

3 Comments

yes, i can see that paintlayout is nullbefore executing currPaint = (ImageButton) paintLayout.getChildAt(0); line
ok it's normal you can't do this. Your DrawingView can't see the paint_colors LinearLayout because the LinearLayout isn't include in your DrawingView. The LinearLayout and the DrawingView are at the same level in your view hierarchy.
@DenleyBihari oops yes sorry! :s

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.