我一直在使用customViewClass,效果很好,直到将compileSdkVersiontargetSdkVersion从19升级到22为止。
compileSdkVersion 19上仍然可以正常工作。

在stacktrace中,您还可能会看到它也抛出ClassNotFoundException,因为它不存在DexPathList

这是我的build.gradle

apply plugin: 'com.android.application'

android {
    compileSdkVersion 22
    buildToolsVersion "21.1.2"

    defaultConfig {
        applicationId "uk.org.example.hfi"
        minSdkVersion 16
        targetSdkVersion 22
        multiDexEnabled true
    }
    dexOptions {
        jumboMode = true
    }
    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.txt'
        }
    }
}

dependencies {
    compile project(':dragSortListview')
    compile 'com.github.bumptech.glide:glide:3.5.2'
    compile 'com.android.support:support-v4:22.1.0'
    compile 'com.google.code.gson:gson:2.2.4'
    compile files('libs/android-query-full.0.26.7.jar')
    compile files('libs/aws-android-sdk-2.1.5-cognito.jar')
    compile files('libs/aws-android-sdk-2.1.5-core.jar')
    compile files('libs/aws-android-sdk-2.1.5-s3.jar')
    compile files('libs/isoviewer-2.0-RC-22.jar')
    compile files('libs/javacpp.jar')
    compile files('libs/javacv.jar')
    compile files('libs/libGoogleAnalyticsServices.jar')
}


这是我自定义的ViewClass

package uk.org.example.hfi.CustomClasses;

import android.content.Context;
import android.util.AttributeSet;
import android.widget.ImageView;

public class RatioImageView extends ImageView {

    public RatioImageView(Context context) {
        super(context);
    }

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

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

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
            int h = MeasureSpec.getSize(heightMeasureSpec);
            int w = h*3/4;
            setMeasuredDimension(w, h);
    }
}


这就是我的使用方式

<uk.org.example.hfi.CustomClasses.RatioImageView
                    android:id="@+id/iv_ProfileImage"
                    android:layout_width="wrap_content"
                    android:layout_height="match_parent"
                    android:src="@drawable/profile_pic"
                    android:scaleType="centerInside"/>


这是完整的堆栈跟踪:Dex还会引发未找到类的异常

.545 10025-10025/uk.org.humanfocus.hfi E/AndroidRuntime: FATAL EXCEPTION: main
10-28 17:28:50.545 10025-10025/uk.org.humanfocus.hfi E/AndroidRuntime: Process: uk.org.humanfocus.hfi, PID: 10025
10-28 17:28:50.545 10025-10025/uk.org.humanfocus.hfi E/AndroidRuntime: java.lang.RuntimeException: Unable to start activity ComponentInfo{uk.org.humanfocus.hfi/uk.org.humanfocus.hfi.Home.MainActivity}: android.view.InflateException: Binary XML file line #32: Error inflating class uk.org.humanfocus.hfi.CustomClasses.RatioImageView
10-28 17:28:50.545 10025-10025/uk.org.humanfocus.hfi E/AndroidRuntime:     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2436)
10-28 17:28:50.545 10025-10025/uk.org.humanfocus.hfi E/AndroidRuntime:     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2495)
10-28 17:28:50.545 10025-10025/uk.org.humanfocus.hfi E/AndroidRuntime:     at android.app.ActivityThread.access$900(ActivityThread.java:170)
10-28 17:28:50.545 10025-10025/uk.org.humanfocus.hfi E/AndroidRuntime:     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1304)
10-28 17:28:50.545 10025-10025/uk.org.humanfocus.hfi E/AndroidRuntime:     at android.os.Handler.dispatchMessage(Handler.java:102)
10-28 17:28:50.545 10025-10025/uk.org.humanfocus.hfi E/AndroidRuntime:     at android.os.Looper.loop(Looper.java:146)
10-28 17:28:50.545 10025-10025/uk.org.humanfocus.hfi E/AndroidRuntime:     at android.app.ActivityThread.main(ActivityThread.java:5635)
10-28 17:28:50.545 10025-10025/uk.org.humanfocus.hfi E/AndroidRuntime:     at java.lang.reflect.Method.invokeNative(Native Method)
10-28 17:28:50.545 10025-10025/uk.org.humanfocus.hfi E/AndroidRuntime:     at java.lang.reflect.Method.invoke(Method.java:515)
10-28 17:28:50.545 10025-10025/uk.org.humanfocus.hfi E/AndroidRuntime:     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1291)
10-28 17:28:50.545 10025-10025/uk.org.humanfocus.hfi E/AndroidRuntime:     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1107)
10-28 17:28:50.545 10025-10025/uk.org.humanfocus.hfi E/AndroidRuntime:     at dalvik.system.NativeStart.main(Native Method)
10-28 17:28:50.545 10025-10025/uk.org.humanfocus.hfi E/AndroidRuntime:  Caused by: android.view.InflateException: Binary XML file line #32: Error inflating class uk.org.humanfocus.hfi.CustomClasses.RatioImageView
10-28 17:28:50.545 10025-10025/uk.org.humanfocus.hfi E/AndroidRuntime:     at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:713)
10-28 17:28:50.545 10025-10025/uk.org.humanfocus.hfi E/AndroidRuntime:     at android.view.LayoutInflater.rInflate(LayoutInflater.java:761)
10-28 17:28:50.545 10025-10025/uk.org.humanfocus.hfi E/AndroidRuntime:     at android.view.LayoutInflater.rInflate(LayoutInflater.java:769)
10-28 17:28:50.545 10025-10025/uk.org.humanfocus.hfi E/AndroidRuntime:     at android.view.LayoutInflater.rInflate(LayoutInflater.java:769)
10-28 17:28:50.545 10025-10025/uk.org.humanfocus.hfi E/AndroidRuntime:     at android.view.LayoutInflater.rInflate(LayoutInflater.java:769)
10-28 17:28:50.545 10025-10025/uk.org.humanfocus.hfi E/AndroidRuntime:     at android.view.LayoutInflater.inflate(LayoutInflater.java:498)
10-28 17:28:50.545 10025-10025/uk.org.humanfocus.hfi E/AndroidRuntime:     at android.view.LayoutInflater.inflate(LayoutInflater.java:398)
10-28 17:28:50.545 10025-10025/uk.org.humanfocus.hfi E/AndroidRuntime:     at android.view.LayoutInflater.inflate(LayoutInflater.java:354)
10-28 17:28:50.545 10025-10025/uk.org.humanfocus.hfi E/AndroidRuntime:     at com.android.internal.policy.impl.PhoneWindow.setContentView(PhoneWindow.java:352)
10-28 17:28:50.545 10025-10025/uk.org.humanfocus.hfi E/AndroidRuntime:     at android.app.Activity.setContentView(Activity.java:2057)
10-28 17:28:50.545 10025-10025/uk.org.humanfocus.hfi E/AndroidRuntime:     at uk.org.humanfocus.hfi.Home.MainActivity.onCreate(MainActivity.java:259)
10-28 17:28:50.545 10025-10025/uk.org.humanfocus.hfi E/AndroidRuntime:     at android.app.Activity.performCreate(Activity.java:5580)
10-28 17:28:50.545 10025-10025/uk.org.humanfocus.hfi E/AndroidRuntime:     at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1093)
10-28 17:28:50.545 10025-10025/uk.org.humanfocus.hfi E/AndroidRuntime:     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2400)
10-28 17:28:50.545 10025-10025/uk.org.humanfocus.hfi E/AndroidRuntime:     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2495) 
10-28 17:28:50.545 10025-10025/uk.org.humanfocus.hfi E/AndroidRuntime:     at android.app.ActivityThread.access$900(ActivityThread.java:170) 
10-28 17:28:50.545 10025-10025/uk.org.humanfocus.hfi E/AndroidRuntime:     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1304) 
10-28 17:28:50.545 10025-10025/uk.org.humanfocus.hfi E/AndroidRuntime:     at android.os.Handler.dispatchMessage(Handler.java:102) 
10-28 17:28:50.545 10025-10025/uk.org.humanfocus.hfi E/AndroidRuntime:     at android.os.Looper.loop(Looper.java:146) 
10-28 17:28:50.545 10025-10025/uk.org.humanfocus.hfi E/AndroidRuntime:     at android.app.ActivityThread.main(ActivityThread.java:5635) 
10-28 17:28:50.545 10025-10025/uk.org.humanfocus.hfi E/AndroidRuntime:     at java.lang.reflect.Method.invokeNative(Native Method) 
10-28 17:28:50.545 10025-10025/uk.org.humanfocus.hfi E/AndroidRuntime:     at java.lang.reflect.Method.invoke(Method.java:515) 
10-28 17:28:50.545 10025-10025/uk.org.humanfocus.hfi E/AndroidRuntime:     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1291) 
10-28 17:28:50.545 10025-10025/uk.org.humanfocus.hfi E/AndroidRuntime:     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1107) 
10-28 17:28:50.545 10025-10025/uk.org.humanfocus.hfi E/AndroidRuntime:     at dalvik.system.NativeStart.main(Native Method) 
10-28 17:28:50.545 10025-10025/uk.org.humanfocus.hfi E/AndroidRuntime:  Caused by: java.lang.ClassNotFoundException: Didn't find class "uk.org.humanfocus.hfi.CustomClasses.RatioImageView" on path: DexPathList[[zip file "/data/app/uk.org.humanfocus.hfi-1.apk"],nativeLibraryDirectories=[/data/app-lib/uk.org.humanfocus.hfi-1, /vendor/lib, /system/lib]]
10-28 17:28:50.545 10025-10025/uk.org.humanfocus.hfi E/AndroidRuntime:     at dalvik.system.BaseDexClassLoader.findClass(BaseDexClassLoader.java:67)
10-28 17:28:50.545 10025-10025/uk.org.humanfocus.hfi E/AndroidRuntime:     at java.lang.ClassLoader.loadClass(ClassLoader.java:497)
10-28 17:28:50.545 10025-10025/uk.org.humanfocus.hfi E/AndroidRuntime:     at java.lang.ClassLoader.loadClass(ClassLoader.java:457)
10-28 17:28:50.545 10025-10025/uk.org.humanfocus.hfi E/AndroidRuntime:     at android.view.LayoutInflater.createView(LayoutInflater.java:565)
10-28 17:28:50.545 10025-10025/uk.org.humanfocus.hfi E/AndroidRuntime:     at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:702)
10-28 17:28:50.545 10025-10025/uk.org.humanfocus.hfi E/AndroidRuntime:     at android.view.LayoutInflater.rInflate(LayoutInflater.java:761) 
10-28 17:28:50.545 10025-10025/uk.org.humanfocus.hfi E/AndroidRuntime:     at android.view.LayoutInflater.rInflate(LayoutInflater.java:769) 
10-28 17:28:50.545 10025-10025/uk.org.humanfocus.hfi E/AndroidRuntime:     at android.view.LayoutInflater.rInflate(LayoutInflater.java:769) 
10-28 17:28:50.545 10025-10025/uk.org.humanfocus.hfi E/AndroidRuntime:     at android.view.LayoutInflater.rInflate(LayoutInflater.java:769) 
10-28 17:28:50.545 10025-10025/uk.org.humanfocus.hfi E/AndroidRuntime:     at android.view.LayoutInflater.inflate(LayoutInflater.java:498) 
10-28 17:28:50.545 10025-10025/uk.org.humanfocus.hfi E/AndroidRuntime:     at android.view.LayoutInflater.inflate(LayoutInflater.java:398) 
10-28 17:28:50.545 10025-10025/uk.org.humanfocus.hfi E/AndroidRuntime:     at android.view.LayoutInflater.inflate(LayoutInflater.java:354) 
10-28 17:28:50.545 10025-10025/uk.org.humanfocus.hfi E/AndroidRuntime:     at com.android.internal.policy.impl.PhoneWindow.setContentView(PhoneWindow.java:352) 
10-28 17:28:50.545 10025-10025/uk.org.humanfocus.hfi E/AndroidRuntime:     at android.app.Activity.setContentView(Activity.java:2057) 
10-28 17:28:50.545 10025-10025/uk.org.humanfocus.hfi E/AndroidRuntime:     at uk.org.humanfocus.hfi.Home.MainActivity.onCreate(MainActivity.java:259) 
10-28 17:28:50.545 10025-10025/uk.org.humanfocus.hfi E/AndroidRuntime:     at android.app.Activity.performCreate(Activity.java:5580) 
10-28 17:28:50.545 10025-10025/uk.org.humanfocus.hfi E/AndroidRuntime:     at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1093) 
10-28 17:28:50.545 10025-10025/uk.org.humanfocus.hfi E/AndroidRuntime:     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2400) 
10-28 17:28:50.545 10025-10025/uk.org.humanfocus.hfi E/AndroidRuntime:     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2495) 
10-28 17:28:50.545 10025-10025/uk.org.humanfocus.hfi E/AndroidRuntime:     at android.app.ActivityThread.access$900(ActivityThread.java:170) 
10-28 17:28:50.545 10025-10025/uk.org.humanfocus.hfi E/AndroidRuntime:     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1304) 
10-28 17:28:50.545 10025-10025/uk.org.humanfocus.hfi E/AndroidRuntime:     at android.os.Handler.dispatchMessage(Handler.java:102) 
10-28 17:28:50.545 10025-10025/uk.org.humanfocus.hfi E/AndroidRuntime:     at android.os.Looper.loop(Looper.java:146) 
10-28 17:28:50.545 10025-10025/uk.org.humanfocus.hfi E/AndroidRuntime:     at android.app.ActivityThread.main(ActivityThread.java:5635) 
10-28 17:28:50.545 10025-10025/uk.org.humanfocus.hfi E/AndroidRuntime:     at java.lang.reflect.Method.invokeNative(Native Method) 
10-28 17:28:50.545 10025-10025/uk.org.humanfocus.hfi E/AndroidRuntime:     at java.lang.reflect.Method.invoke(Method.java:515) 
10-28 17:28:50.545 10025-10025/uk.org.humanfocus.hfi E/AndroidRuntime:     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1291) 
10-28 17:28:50.545 10025-10025/uk.org.humanfocus.hfi E/AndroidRuntime:     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1107) 
10-28 17:28:50.545 10025-10025/uk.org.humanfocus.hfi E/AndroidRuntime:     at dalvik.system.NativeStart.main(Native Method) 


请不要将其标记为重复,因为我已经尝试了所有其他答案,没有其他为我工作!

最佳答案

由于您将compileSdkVersion和targetSdkVersion从19升级到22,因此您的buildToolsVersion应该相应地升级,例如:

compileSdkVersion 22
buildToolsVersion "22.0.0"


如果您的buildToolsVersion> = 22,则将buildToolsVersion更改为它,否则,应使用sdkmanager下载它。

09-13 05:15