Android应用程序中自定义ImageView的ClassNotFoundException



我得到以下异常。。。

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

我创建了一个自定义ImageView,即com.widgets.utils.CustomRoundedCornerImageView,并在布局xml中使用它。CustomRoundedCornerImageView.java与其他类在类路径中。

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>

你能帮我为什么得到ClassNotFoundException吗?

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

在这种情况下,你必须更改

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

如果现有的解决方案都不起作用,那么它就是problem with multidex file

使用multidex的主要缺点之一是-当我们有multiple dex files,并且假设我们有一个CustomClass,它包含在dex-1文件中,并且我们正在使用的Activity包含在另一个dex文件中,比如说dex-2。然后它将生成一个运行时错误:ClassNotFoundException

这是因为这两个文件并没有放在同一个dex文件中,而且我们几乎无法控制哪些类将放在哪个dex文件。

可能的解决方案:

删除未使用的函数或库-这将减少项目中的函数数量,从而减少dex文件。

最新更新