我正在尝试在我的Android项目中使用DataBinding,我在Kotlin中进行编码。首先,我使用DataBindingUtil.setContentView(layoutResId)方法,它可以运行。然后我尝试将DataBinding封装到BaseActivity,gradle build失败,我不知道为什么。 这是我的代码。
class BaseActivity<B : ViewDataBinding>
: AppCompatActivity(){
protected lateinit var mContext: AppCompatActivity
protected lateinit var mBinding: B
override fun onCreate(savedInstanceState: Bundle?) {
// Dagger2 注入
AndroidInjection.inject(this)
super.onCreate(savedInstanceState)
// 保存当前 Context 对象
mContext = this
// 添加到 AppManager 应用管理
AppManager.INSTANCE.addActivity(this)
}
override fun onDestroy() {
// 从应用管理移除当前 Activity 对象
AppManager.INSTANCE.removeActivity(this)
super.onDestroy()
}
override fun setContentView(layoutResID: Int) {
// 加载布局,初始化 DataBinding
mBinding = DataBindingUtil.inflate(
LayoutInflater.from(mContext),
layoutResID, null, false
)
super.setContentView(mBinding.root)
}
}
class MainActivity : BaseActivity<ActivityMainBinding>() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
}
}
Error:Execution failed for task ':app:compileDebugJavaWithJavac'.
> Compilation failed; see the compiler error output for details.
2017-10-17 13:10:33,992 [se-915-b01] INFO - ild.invoker.GradleBuildInvoker - About to execute Gradle tasks: [:SwipToLoad:generateDebugSources, :SwipToLoad:generateDebugAndroidTestSources, :SwipToLoad:mockableAndroidJar, :SwipToLoad:compileDebugAndroidTestSources, :SwipToLoad:compileDebugUnitTestSources, :SwipToLoad:compileDebugSources, :app:generateDebugSources, :app:generateDebugAndroidTestSources, :app:mockableAndroidJar, :app:compileDebugAndroidTestSources, :app:compileDebugUnitTestSources, :app:compileDebugSources]
2017-10-17 13:10:33,998 [thread 138] INFO - s.plugins.gradle.GradleManager - Instructing gradle to use java from C:/Program Files/Java/jdk1.8.0_101
2017-10-17 13:10:34,086 [thread 138] INFO - oject.common.GradleInitScripts - init script file sync.local.repo contents "allprojects {\n buildscript {\n repositories {\n maven { url 'D:\\\\android-studio3.0\\\\gradle\\\\m2repository'}\n }\n }\n repositories {\n maven { url 'D:\\\\android-studio3.0\\\\gradle\\\\m2repository'}\n }\n}\n"
2017-10-17 13:10:34,087 [thread 138] INFO - ild.invoker.GradleBuildInvoker - Build command line options: [--configure-on-demand, -Pandroid.injected.invoked.from.ide=true, --init-script, C:\Users\Administrator\AppData\Local\Temp\sync.local.repo43033.gradle]
2017-10-17 13:10:34,087 [thread 138] INFO - xecution.GradleExecutionHelper - Passing command-line args to Gradle Tooling API: --configure-on-demand -Pandroid.injected.invoked.from.ide=true --init-script C:\Users\Administrator\AppData\Local\Temp\sync.local.repo43033.gradle
2017-10-17 13:10:35,800 [se-915-b01] INFO - ild.invoker.GradleBuildInvoker - Gradle build finished with 1 error(s) in 1s 783ms
2017-10-17 13:10:35,835 [se-915-b01] INFO - pl.ProjectRootManagerComponent - project roots have changed
2017-10-17 13:10:35,860 [thread 138] INFO - .diagnostic.PerformanceWatcher - Pushing properties took 1ms; general responsiveness: ok; EDT responsiveness: ok
2017-10-17 13:10:35,910 [thread 138] INFO - .diagnostic.PerformanceWatcher - Indexable file iteration took 50ms; general responsiveness: ok; EDT responsiveness: ok
2017-10-17 13:10:36,405 [J pool 2/4] WARN - hes.resolve.KotlinCacheService - Could not find correct module information.
Reason: Analyzing element of type class com.android.tools.idea.databinding.LightGeneratedComponentClass with no containing file
Text:
null
2017-10-17 13:10:36,405 [J pool 2/4] WARN - .resolve.jvm.JvmAnalyzerFacade - Java referenced null from LibraryInfo(libraryName=library-1.3.1)
Referenced class was: JavaClassImpl: DATA binding component class
2017-10-17 13:10:36,405 [J pool 2/4] WARN - hes.resolve.KotlinCacheService - Could not find correct module information.
Reason: Analyzing element of type class com.android.tools.idea.databinding.LightGeneratedComponentClass with no containing file
Text:
null
2017-10-17 13:10:36,405 [J pool 2/4] WARN - .resolve.jvm.JvmAnalyzerFacade - Java referenced null from LibraryInfo(libraryName=library-1.3.1)
Referenced class was: JavaClassImpl: DATA binding component class
我不知道如何解决这个问题,我的所有代码都在KotlinTest,谢谢你的帮助!