简体   繁体   中英

How to compare java class in kotlin

In my Kotlin project, I use android-gpuimage . Basically I want to convert the following from Java:

    if (mFilter == null || (filter != null && !mFilter.getClass().equals(filter.getClass()))) {
        mFilter = filter;
        mGPUImage.setFilter(mFilter);
        mFilterAdjuster = new GPUImageFilterTools.FilterAdjuster(mFilter);
    }

The auto conversion of Android Studio 3 gives me:

    if (mFilter == null || filter != null && mFilter.javaClass != filter.javaClass) {
        mFilter = filter
        mGPUImage.setFilter(mFilter)
        mFilterAdjuster = GPUImageFilterTools.FilterAdjuster(mFilter)
    }

The code does not compile with the following error:

  is not satisfied: inferred type GPUImageFilter? is not a subtype of Any

Any help will be appreciated. Thanks

What line is the error on? You're trying to put a nullable type (inferred) into something that is Any which is not nullable. You need to add a null check operator !! which throws. Or do a safe check with let or if . So instead off:

receiver.method(somethingNullable)

do either

receiver.method(somethingNullable!!)

or

somethingNullable?.let {
   receiver.method(it) 
}

or

if (somethingNullable != null) {
    receiver.method(somethingNullable)
}

instead of

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM