首页 > 解决方案 > Kotlin Extensions - Inflate Custom Views

问题描述

I'm new to kotlin, I Have an adapter class and I need to inflate a view. but recently I'm facing an error saying "Unresolved reference: R"

Like in the image below :

enter image description here

So, how can I inflate this view? I have imported the following:

import kotlinx.android.synthetic.main.slide_layout.view.*

But I can't figure out what should i do next, I searched everywhere, but they all seem outdated! so what should i do? Thank you!

标签: javaandroidandroid-studiokotlinkotlin-extension

解决方案


There are two possible issues i can think of,

  1. The first is a missing import to the R class, if your adapter is in an inner package from your domain package name, i.e.
    • assume this is the domain package 'your.doamin.package'
    • if adapter is not in that package i.e. adapter is in 'your.domain.package.adapter' then you need to import the R class into that file, i.e write this import statement

import your.domain.package.R

That should fix your problem for this scenario

  1. If you have already done '1' above then the other issue is, Sometimes Android studio just misbehaves, so close the file and clean and build your project by first clicking, Build -> Clean Project, once the build is done reopen that file.

推荐阅读