在android api 16中膨胀类BezeImage视图但在api 23中工作时出错?

在android api 16中膨胀类BezeImage视图但在api 23中工作时出错?,android,xml,Android,Xml,我正在尝试使用google iosched 2015应用程序中的导航抽屉。我还创建了类my BaseActivity和navdrawer.xml 我已将基本活动扩展到我的家庭活动。HomeActivity的布局代码为 <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layo

我正在尝试使用google iosched 2015应用程序中的导航抽屉。我还创建了类my BaseActivity和navdrawer.xml

我已将基本活动扩展到我的家庭活动。HomeActivity的布局代码为

<RelativeLayout 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"
    tools:context="windo.com.wind_o.HomeScreen">

    <!--<include layout="@layout/tool_bar"></include>-->



    <ImageView
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:scaleType="centerCrop"

        android:id="@+id/imageView"
        android:src="@drawable/home_page_one"




        android:layout_alignParentLeft="false" />
    <android.support.design.widget.AppBarLayout
        xmlns:android="http://schemas.android.com/apk/res/android"
        android:id="@+id/app_bar"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:theme="@style/ThemeOverlay.AppCompat.ActionBar">
        <android.support.v7.widget.Toolbar
            android:id="@+id/tool_bar"
            android:layout_width="match_parent"
            android:layout_height="?attr/actionBarSize"
            app:theme="@style/ToolbarTheme"
            app:popupTheme="@style/ThemeOverlay.AppCompat.Light"
            app:layout_collapseMode="pin"
            style="@style/Base.ThemeOverlay.AppCompat.Dark.ActionBar" />

    </android.support.design.widget.AppBarLayout>

    <!--<TextView-->
        <!--android:id="@+id/txtview1"-->
        <!--android:layout_width="match_parent"-->
        <!--android:background="#a0000000"-->
        <!--android:layout_height="60dp"-->
        <!--android:textSize="24sp"-->
        <!--android:gravity="center_vertical|center_horizontal"-->
        <!--android:singleLine="true"-->
        <!--android:text="WIND-O"-->
        <!--android:textStyle="bold"-->
        <!--android:textColor="@color/ColorPrimary" />-->

    <LinearLayout
        android:orientation="horizontal"
        android:layout_width="match_parent"
        android:paddingRight="@dimen/activity_horizontal_margin"
        android:layout_marginTop="32dp"
        android:paddingLeft="@dimen/activity_horizontal_margin"
        android:layout_marginBottom="32dp"
        android:layout_alignParentBottom="true"
        android:gravity="bottom"
        android:layout_height="match_parent"

  >

        <!--<Button-->
            <!--android:layout_width="match_parent"-->
            <!--android:layout_height="40dp"-->

            <!--android:text="CATEGORIES"-->
            <!--android:background="@drawable/transparent_with_white_border"-->

            <!--android:id="@+id/categoryBtn"-->
            <!--android:layout_marginBottom="6dp"-->
            <!--android:textColor="@color/ColorPrimary"-->
            <!--android:layout_gravity="center"-->
            <!--android:textSize="18sp"-->
            <!--android:gravity="center_vertical|center_horizontal"-->
            <!--android:textStyle="bold"-->
            <!--android:typeface="monospace" />-->

        <!--<Button-->
            <!--android:layout_width="match_parent"-->
            <!--android:layout_height="40dp"-->
            <!--android:text="TRENDING"-->
            <!--android:background="@drawable/transparent_with_white_border"-->
            <!--android:id="@+id/home_btn_2"-->
            <!--android:layout_marginBottom="6dp"-->
            <!--android:textColor="@color/ColorPrimary"-->
            <!--android:layout_gravity="center"-->
            <!--android:textSize="18sp"-->
            <!--android:gravity="center_vertical|center_horizontal"-->
            <!--android:textStyle="bold"-->
            <!--android:typeface="monospace"/>-->

        <!--<Button-->
            <!--android:layout_width="match_parent"-->
            <!--android:layout_height="40dp"-->
            <!--android:text="DISCOVER"-->
            <!--android:background="@drawable/transparent_with_white_border"-->
            <!--android:id="@+id/discover_btn"-->
            <!--android:layout_marginBottom="6dp"-->
            <!--android:textColor="@color/ColorPrimary"-->
            <!--android:layout_gravity="center"-->
            <!--android:textSize="18sp"-->
            <!--android:gravity="center_vertical|center_horizontal"-->
            <!--android:textStyle="bold"-->
            <!--android:typeface="monospace"/>-->
        <View
            android:layout_width="0dp"
            android:layout_height="fill_parent"
            android:layout_weight="1"
            />
        <Button

            android:layout_width="wrap_content"

            android:padding="16dp"
            android:layout_height="wrap_content"
            android:background="@drawable/category_icon"
            android:drawablePadding="12dp"
            android:gravity="center"
            android:textColor="#FFFFFF"
            android:id="@+id/categoryBtn"
            android:paddingTop="15sp"

            ></Button>
        <View
            android:layout_width="0dp"
            android:layout_height="fill_parent"
            android:layout_weight="0.5"
            />

        <Button

            android:layout_width="wrap_content"

            android:layout_height="wrap_content"
            android:padding="16dp"
            android:background="@drawable/trending_icon"

            android:drawablePadding="12dp"
            android:gravity="center"
            android:textColor="#FFFFFF"
            android:id="@+id/home_btn_2"
            android:paddingTop="15sp"
            ></Button>
        <View
            android:layout_width="0dp"
            android:layout_height="fill_parent"
            android:layout_weight="0.5"
            />
        <Button

            android:layout_width="wrap_content"

            android:layout_height="wrap_content"
            android:padding="16dp"
            android:background="@drawable/explore_icon"
            android:textColor="#FFFFFF"
            android:id="@+id/discover_btn"

            android:paddingTop="15sp"
            ></Button>
        <View
            android:layout_width="0dp"
            android:layout_height="fill_parent"
            android:layout_weight="1"
            />
    </LinearLayout>
</RelativeLayout>
<!-- Nav drawer -->
<include layout="@layout/navdrawer" />
<!--<android.support.design.widget.NavigationView-->
    <!--android:id="@+id/navigation_drawer"-->
    <!--android:layout_width="wrap_content"-->
    <!--android:layout_height="match_parent"-->
    <!--android:layout_gravity="start"-->
    <!--app:menu="@menu/menu_navigation_drawer"/>-->

显示充气异常的第68行是navdrawer.xml中的BezeImage视图自定义视图

在过去的两天里一直在研究它,但不明白为什么它在api 23中有效,而在api 16中无效

编辑:BezeImage视图的代码

package windo.com.wind_o.views;

/**
 * Borrowed from google iosched
 */

import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.ColorMatrix;
import android.graphics.ColorMatrixColorFilter;
import android.graphics.Paint;
import android.graphics.PorterDuff;
import android.graphics.PorterDuffXfermode;
import android.graphics.Rect;
import android.graphics.RectF;
import android.graphics.drawable.Drawable;
import android.support.v4.view.ViewCompat;
import android.util.AttributeSet;
import android.widget.ImageView;


import windo.com.wind_o.R;


/**
 * An {@link android.widget.ImageView} that draws its contents inside a mask and draws a border
 * drawable on top. This is useful for applying a beveled look to image contents, but is also
 * flexible enough for use with other desired aesthetics.
 */
public class BezelImageView extends ImageView {
    private Paint mBlackPaint;
    private Paint mMaskedPaint;

    private Rect mBounds;
    private RectF mBoundsF;

    private Drawable mBorderDrawable;
    private Drawable mMaskDrawable;

    private ColorMatrixColorFilter mDesaturateColorFilter;
    private boolean mDesaturateOnPress = false;

    private boolean mCacheValid = false;
    private Bitmap mCacheBitmap;
    private int mCachedWidth;
    private int mCachedHeight;

    public BezelImageView(Context context) {
        this(context, null);
    }

    public BezelImageView(Context context, AttributeSet attrs) {
        this(context, attrs, 0);
    }

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

        // Attribute initialization.
        final TypedArray a = context.obtainStyledAttributes(attrs, R.styleable.BezelImageView,
                defStyle, 0);

        mMaskDrawable = a.getDrawable(R.styleable.BezelImageView_maskDrawable);
        if (mMaskDrawable != null) {
            mMaskDrawable.setCallback(this);
        }

        mBorderDrawable = a.getDrawable(R.styleable.BezelImageView_borderDrawable);
        if (mBorderDrawable != null) {
            mBorderDrawable.setCallback(this);
        }

        mDesaturateOnPress = a.getBoolean(R.styleable.BezelImageView_desaturateOnPress,
                mDesaturateOnPress);

        a.recycle();

        // Other initialization.
        mBlackPaint = new Paint();
        mBlackPaint.setColor(0xff000000);

        mMaskedPaint = new Paint();
        mMaskedPaint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.SRC_IN));

        // Always want a cache allocated.
        mCacheBitmap = Bitmap.createBitmap(1, 1, Bitmap.Config.ARGB_8888);

        if (mDesaturateOnPress) {
            // Create a desaturate color filter for pressed state.
            ColorMatrix cm = new ColorMatrix();
            cm.setSaturation(0);
            mDesaturateColorFilter = new ColorMatrixColorFilter(cm);
        }
    }

    @Override
    protected boolean setFrame(int l, int t, int r, int b) {
        final boolean changed = super.setFrame(l, t, r, b);
        mBounds = new Rect(0, 0, r - l, b - t);
        mBoundsF = new RectF(mBounds);

        if (mBorderDrawable != null) {
            mBorderDrawable.setBounds(mBounds);
        }
        if (mMaskDrawable != null) {
            mMaskDrawable.setBounds(mBounds);
        }

        if (changed) {
            mCacheValid = false;
        }

        return changed;
    }

    @Override
    protected void onDraw(Canvas canvas) {
        if (mBounds == null) {
            return;
        }

        int width = mBounds.width();
        int height = mBounds.height();

        if (width == 0 || height == 0) {
            return;
        }

        if (!mCacheValid || width != mCachedWidth || height != mCachedHeight) {
            // Need to redraw the cache.
            if (width == mCachedWidth && height == mCachedHeight) {
                // Have a correct-sized bitmap cache already allocated. Just erase it.
                mCacheBitmap.eraseColor(0);
            } else {
                // Allocate a new bitmap with the correct dimensions.
                mCacheBitmap.recycle();
                //noinspection AndroidLintDrawAllocation
                mCacheBitmap = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888);
                mCachedWidth = width;
                mCachedHeight = height;
            }

            Canvas cacheCanvas = new Canvas(mCacheBitmap);
            if (mMaskDrawable != null) {
                int sc = cacheCanvas.save();
                mMaskDrawable.draw(cacheCanvas);
                mMaskedPaint.setColorFilter((mDesaturateOnPress && isPressed())
                        ? mDesaturateColorFilter : null);
                cacheCanvas.saveLayer(mBoundsF, mMaskedPaint,
                        Canvas.HAS_ALPHA_LAYER_SAVE_FLAG | Canvas.FULL_COLOR_LAYER_SAVE_FLAG);
                super.onDraw(cacheCanvas);
                cacheCanvas.restoreToCount(sc);
            } else if (mDesaturateOnPress && isPressed()) {
                int sc = cacheCanvas.save();
                cacheCanvas.drawRect(0, 0, mCachedWidth, mCachedHeight, mBlackPaint);
                mMaskedPaint.setColorFilter(mDesaturateColorFilter);
                cacheCanvas.saveLayer(mBoundsF, mMaskedPaint,
                        Canvas.HAS_ALPHA_LAYER_SAVE_FLAG | Canvas.FULL_COLOR_LAYER_SAVE_FLAG);
                super.onDraw(cacheCanvas);
                cacheCanvas.restoreToCount(sc);
            } else {
                super.onDraw(cacheCanvas);
            }

            if (mBorderDrawable != null) {
                mBorderDrawable.draw(cacheCanvas);
            }
        }

        // Draw from cache.
        canvas.drawBitmap(mCacheBitmap, mBounds.left, mBounds.top, null);
    }

    @Override
    protected void drawableStateChanged() {
        super.drawableStateChanged();
        if (mBorderDrawable != null && mBorderDrawable.isStateful()) {
            mBorderDrawable.setState(getDrawableState());
        }
        if (mMaskDrawable != null && mMaskDrawable.isStateful()) {
            mMaskDrawable.setState(getDrawableState());
        }
        if (isDuplicateParentStateEnabled()) {
            ViewCompat.postInvalidateOnAnimation(this);
        }
    }

    @Override
    public void invalidateDrawable(Drawable who) {
        if (who == mBorderDrawable || who == mMaskDrawable) {
            invalidate();
        } else {
            super.invalidateDrawable(who);
        }
    }

    @Override
    protected boolean verifyDrawable(Drawable who) {
        return who == mBorderDrawable || who == mMaskDrawable || super.verifyDrawable(who);
    }
}

所以我终于找到了问题的症结所在。 由于应用程序找不到类
BezelImageView
,因此错误导致视图膨胀。 我的应用程序中有另一个自定义视图,它也抛出了相同的错误

该类存在,但应用程序找不到它。我解压缩了我的apk文件,发现有两个dex文件:classes.dexclasses2.dex和我的
CustomeView
s在classes2.dex文件中

经过进一步检查,问题在于android 5以下的应用程序无法从classes2.dex加载类。由于Android L默认从所有dex文件加载类,这是因为新的运行时环境ART,因此我的应用程序在Android L上运行良好

要使类从所有文件中加载,我必须执行以下操作

  • 将multidex库添加到my build.gradle中,如下所示

    编译'com.android.support:multidex:1.0.0'

  • 将我的应用程序类中的
    attachBaseContext
    重写为

    受保护的void attachBaseContext(Context-base){
    super.attachBaseContext(base);
    多索引安装(本);
    }

  • 现在,所有来自dex文件的类都可以在android L下面的应用程序中正确找到

    如果不扩展应用程序类,则可以在AndroidManifest.xml中添加以下行

    ...
    <application
    ...
    android:name="android.support.multidex.MultiDexApplication"/>
    ...
    
    。。。
    

    张贴代码BeizelImageView@Raghunandan更新我看不出BezeImage视图有问题。您可以检查是否有任何不推荐的方法。但这应该是一个警告,而不是崩溃。android studio没有显示任何不推荐的方法。没有任何明显的警告。异常的根本原因是找不到类。但为什么呢?全班同学就坐在那里。
    package windo.com.wind_o.views;
    
    /**
     * Borrowed from google iosched
     */
    
    import android.content.Context;
    import android.content.res.TypedArray;
    import android.graphics.Bitmap;
    import android.graphics.Canvas;
    import android.graphics.ColorMatrix;
    import android.graphics.ColorMatrixColorFilter;
    import android.graphics.Paint;
    import android.graphics.PorterDuff;
    import android.graphics.PorterDuffXfermode;
    import android.graphics.Rect;
    import android.graphics.RectF;
    import android.graphics.drawable.Drawable;
    import android.support.v4.view.ViewCompat;
    import android.util.AttributeSet;
    import android.widget.ImageView;
    
    
    import windo.com.wind_o.R;
    
    
    /**
     * An {@link android.widget.ImageView} that draws its contents inside a mask and draws a border
     * drawable on top. This is useful for applying a beveled look to image contents, but is also
     * flexible enough for use with other desired aesthetics.
     */
    public class BezelImageView extends ImageView {
        private Paint mBlackPaint;
        private Paint mMaskedPaint;
    
        private Rect mBounds;
        private RectF mBoundsF;
    
        private Drawable mBorderDrawable;
        private Drawable mMaskDrawable;
    
        private ColorMatrixColorFilter mDesaturateColorFilter;
        private boolean mDesaturateOnPress = false;
    
        private boolean mCacheValid = false;
        private Bitmap mCacheBitmap;
        private int mCachedWidth;
        private int mCachedHeight;
    
        public BezelImageView(Context context) {
            this(context, null);
        }
    
        public BezelImageView(Context context, AttributeSet attrs) {
            this(context, attrs, 0);
        }
    
        public BezelImageView(Context context, AttributeSet attrs, int defStyle) {
            super(context, attrs, defStyle);
    
            // Attribute initialization.
            final TypedArray a = context.obtainStyledAttributes(attrs, R.styleable.BezelImageView,
                    defStyle, 0);
    
            mMaskDrawable = a.getDrawable(R.styleable.BezelImageView_maskDrawable);
            if (mMaskDrawable != null) {
                mMaskDrawable.setCallback(this);
            }
    
            mBorderDrawable = a.getDrawable(R.styleable.BezelImageView_borderDrawable);
            if (mBorderDrawable != null) {
                mBorderDrawable.setCallback(this);
            }
    
            mDesaturateOnPress = a.getBoolean(R.styleable.BezelImageView_desaturateOnPress,
                    mDesaturateOnPress);
    
            a.recycle();
    
            // Other initialization.
            mBlackPaint = new Paint();
            mBlackPaint.setColor(0xff000000);
    
            mMaskedPaint = new Paint();
            mMaskedPaint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.SRC_IN));
    
            // Always want a cache allocated.
            mCacheBitmap = Bitmap.createBitmap(1, 1, Bitmap.Config.ARGB_8888);
    
            if (mDesaturateOnPress) {
                // Create a desaturate color filter for pressed state.
                ColorMatrix cm = new ColorMatrix();
                cm.setSaturation(0);
                mDesaturateColorFilter = new ColorMatrixColorFilter(cm);
            }
        }
    
        @Override
        protected boolean setFrame(int l, int t, int r, int b) {
            final boolean changed = super.setFrame(l, t, r, b);
            mBounds = new Rect(0, 0, r - l, b - t);
            mBoundsF = new RectF(mBounds);
    
            if (mBorderDrawable != null) {
                mBorderDrawable.setBounds(mBounds);
            }
            if (mMaskDrawable != null) {
                mMaskDrawable.setBounds(mBounds);
            }
    
            if (changed) {
                mCacheValid = false;
            }
    
            return changed;
        }
    
        @Override
        protected void onDraw(Canvas canvas) {
            if (mBounds == null) {
                return;
            }
    
            int width = mBounds.width();
            int height = mBounds.height();
    
            if (width == 0 || height == 0) {
                return;
            }
    
            if (!mCacheValid || width != mCachedWidth || height != mCachedHeight) {
                // Need to redraw the cache.
                if (width == mCachedWidth && height == mCachedHeight) {
                    // Have a correct-sized bitmap cache already allocated. Just erase it.
                    mCacheBitmap.eraseColor(0);
                } else {
                    // Allocate a new bitmap with the correct dimensions.
                    mCacheBitmap.recycle();
                    //noinspection AndroidLintDrawAllocation
                    mCacheBitmap = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888);
                    mCachedWidth = width;
                    mCachedHeight = height;
                }
    
                Canvas cacheCanvas = new Canvas(mCacheBitmap);
                if (mMaskDrawable != null) {
                    int sc = cacheCanvas.save();
                    mMaskDrawable.draw(cacheCanvas);
                    mMaskedPaint.setColorFilter((mDesaturateOnPress && isPressed())
                            ? mDesaturateColorFilter : null);
                    cacheCanvas.saveLayer(mBoundsF, mMaskedPaint,
                            Canvas.HAS_ALPHA_LAYER_SAVE_FLAG | Canvas.FULL_COLOR_LAYER_SAVE_FLAG);
                    super.onDraw(cacheCanvas);
                    cacheCanvas.restoreToCount(sc);
                } else if (mDesaturateOnPress && isPressed()) {
                    int sc = cacheCanvas.save();
                    cacheCanvas.drawRect(0, 0, mCachedWidth, mCachedHeight, mBlackPaint);
                    mMaskedPaint.setColorFilter(mDesaturateColorFilter);
                    cacheCanvas.saveLayer(mBoundsF, mMaskedPaint,
                            Canvas.HAS_ALPHA_LAYER_SAVE_FLAG | Canvas.FULL_COLOR_LAYER_SAVE_FLAG);
                    super.onDraw(cacheCanvas);
                    cacheCanvas.restoreToCount(sc);
                } else {
                    super.onDraw(cacheCanvas);
                }
    
                if (mBorderDrawable != null) {
                    mBorderDrawable.draw(cacheCanvas);
                }
            }
    
            // Draw from cache.
            canvas.drawBitmap(mCacheBitmap, mBounds.left, mBounds.top, null);
        }
    
        @Override
        protected void drawableStateChanged() {
            super.drawableStateChanged();
            if (mBorderDrawable != null && mBorderDrawable.isStateful()) {
                mBorderDrawable.setState(getDrawableState());
            }
            if (mMaskDrawable != null && mMaskDrawable.isStateful()) {
                mMaskDrawable.setState(getDrawableState());
            }
            if (isDuplicateParentStateEnabled()) {
                ViewCompat.postInvalidateOnAnimation(this);
            }
        }
    
        @Override
        public void invalidateDrawable(Drawable who) {
            if (who == mBorderDrawable || who == mMaskDrawable) {
                invalidate();
            } else {
                super.invalidateDrawable(who);
            }
        }
    
        @Override
        protected boolean verifyDrawable(Drawable who) {
            return who == mBorderDrawable || who == mMaskDrawable || super.verifyDrawable(who);
        }
    }
    
    ...
    <application
    ...
    android:name="android.support.multidex.MultiDexApplication"/>
    ...