9

I am getting following exception...

08-12 14:19:41.564: ERROR/AndroidRuntime(797): Caused by: java.lang.ClassNotFoundException: com.widgets.utils.CustomRoundedCornerImageView in loader dalvik.system.PathClassLoader[.]

I have created a custom ImageView i.e. com.widgets.utils.CustomRoundedCornerImageView and using it in a layout xml . The CustomRoundedCornerImageView.java is in class path with other classes.

CustomRoundedCornerImageView.java

package com.widgets.utils;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.Bitmap.Config;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.graphics.PorterDuff.Mode;
import android.graphics.PorterDuffXfermode;
import android.graphics.Rect;
import android.graphics.RectF;
import android.graphics.drawable.BitmapDrawable;
import android.graphics.drawable.Drawable;
import android.util.AttributeSet;
import android.widget.ImageView;

public class CustomRoundedCornerImageView extends ImageView {

    public CustomRoundedCornerImageView(Context context) {
        super(context);
    }
    public CustomRoundedCornerImageView(Context context, AttributeSet attrs) {
        super(context, attrs);
    }

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


    @Override
    protected void onDraw(Canvas canvas) {
            Drawable drawable = getDrawable();

            Bitmap b =  ((BitmapDrawable)drawable).getBitmap() ;
            Bitmap bitmap = b.copy(Bitmap.Config.ARGB_8888, true);

            Bitmap roundBitmap = getRoundedCornerBitmap(bitmap,30);
            canvas.drawBitmap(roundBitmap, 0,0 , null);
    }

    public static Bitmap getRoundedCornerBitmap(Bitmap bitmap, int pixels) {
        Bitmap roundedBitmap = Bitmap.createBitmap(bitmap.getWidth(), bitmap
                .getHeight(), Config.ARGB_8888);
        Canvas canvas = new Canvas(roundedBitmap);

        final int color = 0xff424242;
        final Paint paint = new Paint();
        final Rect rect = new Rect(0, 0, bitmap.getWidth(), bitmap.getHeight());
        final RectF rectF = new RectF(rect);
        final float roundPx = pixels;

        paint.setAntiAlias(true);
        canvas.drawARGB(0, 0, 0, 0);
            paint.setColor(color);
        canvas.drawRoundRect(rectF, roundPx, roundPx, paint);

        paint.setXfermode(new PorterDuffXfermode(Mode.SRC_IN));
        canvas.drawBitmap(bitmap, rect, rect, paint);

        return roundedBitmap;
    }

}

layout.xml

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:id="@+id/picture_frame"
        android:layout_width="70px"
        android:layout_height="70px"
        android:gravity="center"
        android:layout_marginTop="5px"
        android:layout_marginLeft="40px"
        android:focusable="false"
        android:visibility="invisible"
        android:background="@drawable/picture_frame">
    <com.widgets.utils.CustomRoundedCornerImageView 
            android:id="@+id/picture"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:scaleType="centerCrop"
            android:focusable="false" />
</LinearLayout>

Can you help me why I am getting ClassNotFoundException ?

6
  • have u defined the class in the manifest Commented Aug 12, 2011 at 9:18
  • 1
    @Raghu Its a class not Activity Commented Aug 12, 2011 at 9:20
  • @Raghu yes, if it is an Activity then I could have defined it in AndroidManifest.xml , It is a simple ImageVIew . Commented Aug 12, 2011 at 9:32
  • I built a project using your ImageView and it worked fine. Perhaps you should clean your project and refresh your directories. Commented Aug 12, 2011 at 9:38
  • @CaspNZ Thanks for trying this, however I have already done the same couple of times, Only difference is, I am trying to add this new custom class in an existing Project which is already in my device. Any other idea? Commented Aug 12, 2011 at 9:45

2 Answers 2

21
<com.widgets.utils.CustomRoundedCornerImageView 
        android:id="@+id/picture"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:scaleType="centerCrop"
        android:focusable="false" />

in this you have to change

<ImageView class="com.widgets.utils.CustomRoundedCornerImageView" 
        android:id="@+id/picture"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:scaleType="centerCrop"
        android:focusable="false" />
Sign up to request clarification or add additional context in comments.

3 Comments

is there reason as to why this must be done? It causes further problems down the line, and is contradictory to all of the Android tutorial information
According to docs, you need to explicitly use the class attribute if the extended one is an inner class, otherwise it can be used as an xml attribute directly.
I have got warning 'Unknown attribute class' ; any idea how to remove it? (indeed, your solution doesn't seem to work for me, probably linked to this warning) Thanks.
0

If None of the existing solutions are working, then it is a problem with multidex file.

One of the main disadvantages of using multidex is - When we have multiple dex files and suppose We have a CustomClass that is contained in dex-1 file and Activity where we are using that class is contained in another dex file lets say dex-2. Then it will generate a runtime error: ClassNotFoundException.

This is because both files are not placed in same dex file and we have little control over which classes will be placed in which dex file.

Probable Solutions:

Remove unused functions or libraries - it will reduce number of functions in your project that can lead to fewer dex files.

Comments

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.