我在我的公共模块BaseActivity.kt和BaseFragment.kt中使用组件开发。当其他模块继承它们时会出现以下问题
> D:\Android\OneDaily\module_main\src\main\java\com\boco\main\MainActivity.kt
> Error:(7, 24) Unresolved reference: base
> Error:(9, 22) Unresolved reference: BaseActivity
> Error:(21, 5) 'onCreate' overrides nothing
> Error:(17, 5) 'getLayoutRes' overrides nothing
> Error:(22, 15) Unresolved reference: onCreate
> Error:(27, 22) Unresolved reference: findViewById
> Error:(42, 34) Unresolved reference: supportFragmentManager
> D:\Android\OneDaily\module_main\src\main\java\com\boco\main\TimelineFragment.kt
> Error:(7, 24) Unresolved reference: base
> Error:(10, 5) 'getLayoutRes' overrides nothing
> Error:(9, 26) Unresolved reference: BaseFragment
> Error:(14, 5) 'onCreateView' overrides nothing
> Error:(15, 22) Unresolved reference: onCreateView
BaseActivity.kt:
abstract class BaseActivity : AppCompatActivity() {
init {
AppCompatDelegate.setCompatVectorFromResourcesEnabled(true)
}
abstract fun getLayoutRes(): Int
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(getLayoutRes())
}
}
MainActivity.kt
class MainActivity : BaseActivity() {
private lateinit var mBottomNav: BottomNavigationView
private var mFragment1 = TimelineFragment() as Fragment
private var mFragment2 = TimelineFragment() as Fragment
private var mFragment3 = TimelineFragment() as Fragment
override fun getLayoutRes(): Int {
return R.layout.activity_main
}
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
}
}
答案 0 :(得分:54)
在上次更新中似乎有些功能发生了变化,
只需删除&{39; ?
'来自Bundle
像这样:
override fun onCreate(savedInstanceState: Bundle) {
super.onCreate(savedInstanceState)
}
答案 1 :(得分:45)
它也可能发生在Fragment类中:我发现方法onCreateView
存在同样的问题;为避免这种情况,只需从?
参数
LayoutInflater
即可
像这样:
override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View?
答案 2 :(得分:5)
我要添加“?”使用appcompat-v7时:27.1.1
implementation "com.android.support:appcompat-v7:27.1.1
和
override fun onCreate(savedInstanceState : Bundle?){
super.onCreate(savedInstanceState)
}
答案 3 :(得分:0)
更新功能签名或从27到26恢复到compileSdk
答案 4 :(得分:-1)
我已经解决了这个问题。因为它是您不配置您的通用模块。请在您的通用模块的build.gradle顶部添加两行代码:
apply plugin: 'kotlin-android'
apply plugin: 'kotlin-android-extensions'
希望这可以帮助您。