Kotlin-android:未解析的参考数据绑定
2022-08-31 07:08:53
我有以下使用新的数据绑定库用Java编写的片段类
import com.example.app.databinding.FragmentDataBdinding;
public class DataFragment extends Fragment {
@Nullable
private FragmentDataBinding mBinding;
@Nullable
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
mBinding = DataBindingUtil.inflate(inflater, R.layout.fragment_data, container, false);
return mBinding.getRoot();
}
}
它可以编译并运行良好。
我试图在Kotlin中重写它,并提出了以下内容:
import com.example.app.databinding.FragmentDataBdinding
class ProfileFragment : Fragment() {
private var mBinding: FragmentDataBinding? = null
override fun onCreateView(inflater: LayoutInflater?, container: ViewGroup?, savedInstanceState: Bundle?): View? {
mBinding = DataBindingUtil.inflate(inflater, R.layout.fragment_data, container, false)
return mBinding!!.getRoot()
}
}
但现在步骤输出以下内容::app:compileDebugKotlin
错误:(16, 38) 未解析的引用: 数据绑定
错误:(37, 27) 未解析的引用: 片段数据绑定
如何将 android-databinding 库与 Kotlin 一起使用?
我的顶级 :build.gradle
buildscript {
repositories {
jcenter()
}
dependencies {
classpath 'com.android.tools.build:gradle:1.3.0'
classpath 'com.android.databinding:dataBinder:1.0-rc4'
}
}
allprojects {
repositories {
jcenter()
}
}
我的应用内目录(仅相关部分):build.gradle
apply plugin: 'com.android.application'
apply plugin: 'com.android.databinding'
apply plugin: 'kotlin-android'
dependencies {
compile "org.jetbrains.kotlin:kotlin-stdlib:$kotlin_version"
}
buildscript {
ext.kotlin_version = '0.14.451'
repositories {
mavenCentral()
}
dependencies {
classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version"
classpath "org.jetbrains.kotlin:kotlin-android-extensions:$kotlin_version"
}
}
repositories {
mavenCentral()
maven {
url 'http://oss.sonatype.org/content/repositories/snapshots'
}
}
我使用的是 Android Studio 1.4、Build tools version 23.0.1、Android SDK 23、SDK tools 24.4.0