Search code examples
androidkotlinanko

Receiver Type Mismatch with Anko Layouts


I am trying to create layouts with Anko DSL in my support v4 Fragment but the IDE shows receiver type mismatch. DSL works ok from the v7 AppCompatActivity. In trying to get this to work I have updated to Android Studio 3.0. I have also updated the dependencies in build.gradle's as some other threads suggested.

Here are the dependencies in my app's build.gradle

dependencies {
    compile fileTree(include: ['*.jar'], dir: 'libs')

    androidTestCompile('com.android.support.test.espresso:espresso-core:2.2.2', {
        exclude group: 'com.android.support', module: 'support-annotations'
    })
    compile 'com.android.support:appcompat-v7:25.4.0'
    compile 'com.android.support.constraint:constraint-layout:1.0.2'
    compile 'com.android.support:design:25.4.0'
    compile 'com.android.support:support-v4:25.4.0'
    compile "org.jetbrains.kotlin:kotlin-stdlib-jre7:$kotlin_version"
    compile "org.jetbrains.anko:anko:$anko_version"
    compile "org.jetbrains.anko:anko-appcompat-v7-commons:$anko_version"
    compile "org.jetbrains.anko:anko-appcompat-v7:$anko_version"
    compile "org.jetbrains.anko:anko-support-v4-commons:$anko_version"
    compile "org.jetbrains.anko:anko-support-v4:$anko_version"
}

and the dependencies in project's build.gradle

dependencies {
    classpath 'com.android.tools.build:gradle:3.0.0'
    classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version"
    classpath "org.jetbrains.kotlin:kotlin-android-extensions:$kotlin_version"
}

I have also seen this Question 'receiver type mismatch' with Fragment and Anko toast

But I am still stuck.

Screenshot


Solution

  • DSL works ok from the v7 AppCompatActivity

    And in the list of candidates in the screenshot you can see it's defined as an extension function on Activity, and an AppCompatActivity is an Activity.

    Your receiver (this) is the HomeAdapter, which doesn't extend Activity, Context or ViewManager.

    If you call context.verticalLayout it will compile, but I don't know whether the result is what you want.