0

I tried to create my own custom view by extending View class , it worked fine with only java code, but when I tried to add it to xml, my app crashed.

So I hope anyone can help me. Here's my code and xml.

Current code:

package com.miller.ian.drawcanvas;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.RectF;
import android.view.View;

/**
 * Created by Ian on 3/13/2017.
 */

public class MyView extends View{
    private class Size {
        private int x;
        private int y;
        private Size(){
            this(0,0);
        }
        private Size(int x,int y){
            this.x = x;
            this.y = y;
        }
    }

    private int count = 0;
    private Size size;
    private RectF rectf;
    private Paint paint = new Paint();
    public MyView(Context context, int x, int y){
        super(context);
        size = new Size(x,y);
        paint.setStyle(Paint.Style.FILL);
        rectf = new RectF(size.x/3,size.y/3,2*size.x/3,2*size.y/3);
        this.setOnClickListener(new View.OnClickListener() {
           @Override
            public void onClick(View v) {
                switch (count%3) {
                    case 0:
                        v.setBackgroundColor(Color.BLUE);
                        break;
                    case 1:
                        v.setBackgroundColor(Color.YELLOW);
                        break;
                    case 2:
                        v.setBackgroundColor(Color.GREEN);
                    default:
                }
                count++;
            }
        });
    }
    @Override
    protected void onDraw(Canvas canvas){
        canvas.drawRoundRect(rectf,size.x/9,size.y/9,paint);
    }

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        this.setMeasuredDimension(size.x,size.y);
    }


}

Current xml:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/view_group"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    tools:context="com.miller.ian.drawcanvas.MainActivity">

    <com.miller.ian.drawcanvas.MyView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"/>
    </LinearLayout>
Nrzonline
  • 1,600
  • 2
  • 18
  • 37
  • Could you post the stacktrace? – Ruben Aalders Mar 13 '17 at 14:35
  • could u please send your log too? – Mohammad Zarei Mar 13 '17 at 14:35
  • sure, it's a little bit long, so i copied those useful(i made an independent class for MyView lately.): Caused by: java.lang.ClassNotFoundException: Didn't find class "com.miller.ian.MyView" on path: DexPathList[[zip file "/data/app/com.miller.ian.drawcanvas-1/base.apk", ............ at com.miller.ian.drawcanvas.MainActivity.onCreate(MainActivity.java:12) – Ian Miller Mar 13 '17 at 14:49
  • Try restart and invalidate cache. Also is proguard enabled? If it is put exception for your class – X3Btel Mar 13 '17 at 14:55
  • Sorry, what's proguard and how can I put exeption to a class? I'm using android studio. – Ian Miller Mar 13 '17 at 15:04
  • @IanMiller in your build.gradle do you have minifyEnabled false, or is it true – X3Btel Mar 13 '17 at 15:36
  • @X3Btel Here I found: buildTypes { release { minifyEnabled false proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro' } } – Ian Miller Mar 13 '17 at 15:52
  • @IanMiller Did you restart android studio and rebuild the app? – X3Btel Mar 13 '17 at 16:10
  • @X3BTel yeah, I restarted android studio and sync projects with gradle file, and re-run app. It still didn't work. I tried to add throws Exception to MyView class, but I got errors: "class or interface expected." where should I put "throws Exception"? – Ian Miller Mar 13 '17 at 16:43
  • 1
    To allow inflation from a layout, your `View` must have a public constructor that takes a `Context` and an `AttributeSet`. Have a look at the duplicate linked at the top of your question. – Mike M. Mar 13 '17 at 17:52
  • @IanMiller Sorry to bother again but did you only restart android studio or did you File -> Invalidete Cache/ Restart chose Invalidate & restart. After it finishes Build -> Clean Project then Build -> Rebuild project – X3Btel Mar 13 '17 at 17:57
  • @MikeM. You are right, but i should crash with InflateException & NoSuchMethod exception. According to his log its searching for the wrong class `com.miller.ian.MyView` instead of `com.miller.ian.drawcanvas.MyView` – X3Btel Mar 13 '17 at 18:27
  • @X3Btel Yep, you're right. Well, that's why they need to include the stack trace in the question. :-) I don't usually go looking in the comments for that. Anyhoo, if they're still having that issue, they can edit their question with the complete stack trace, and let me know, and I'll amend the duplicate list, or reopen the question, if needed. – Mike M. Mar 13 '17 at 18:35
  • @X3Btel I didn't notice that I changed `com.miller.ian.drawcanvas.MyView` to `com.miller.ian.MyView` in my mxl. I was going to show you the stack trace so I re-run the app and I don't know how I messed up with them. Sorry about that. Thank you , @MikeM, I added the second construtor which is `public MyView(Context context, AttributeSet attrs) { super(context, attrs); }` and then everything went fine. guys ,thank you for guiding me through this, I'm very appreciated! – Ian Miller Mar 14 '17 at 04:56

0 Answers0