[英]kotlin 'onCreate' overrides nothing
I am using component development, in my common module BaseActivity.kt and BaseFragment.kt.我在我的公共模块 BaseActivity.kt 和 BaseFragment.kt 中使用组件开发。 The following problems occur when other modules inherit them
其他模块继承时出现如下问题
> 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:基础活动.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主活动.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)
}
}
It seems some functions have changed in the last update, just remove the ' ?
似乎有些功能在上次更新时发生了变化,只需删除'
?
' from the Bundle
'来自
Bundle
Like this: 像这样:
override fun onCreate(savedInstanceState: Bundle) {
super.onCreate(savedInstanceState)
}
It can happen also in a Fragment class: I found the same issue for the method onCreateView
; 它也可能发生在Fragment类中:我发现
onCreateView
方法存在同样的问题; to avoid that, just remove the ?
为了避免这种情况,只需删除
?
from the LayoutInflater
parameter, 从
LayoutInflater
参数,
Like this: 像这样:
override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View?
I have to add "?" 我要添加“?” when using appcompat-v7:27.1.1
使用appcompat-v7时:27.1.1
implementation "com.android.support:appcompat-v7:27.1.1
and 和
override fun onCreate(savedInstanceState : Bundle?){
super.onCreate(savedInstanceState)
}
更新函数签名或从27到26恢复到compileSdk
If someone is facing similar issue with onCreateOptionsMenu 'onCreateOptionsMenu' overrides nothing
:如果有人面临与 onCreateOptionsMenu
'onCreateOptionsMenu' overrides nothing
类似的问题,则'onCreateOptionsMenu' overrides nothing
:
I updated Kotlin plugin and it started giving me this error我更新了 Kotlin 插件,它开始给我这个错误
This is my code before override fun onCreateOptionsMenu(menu: Menu?): Boolean {...}
这是我在
override fun onCreateOptionsMenu(menu: Menu?): Boolean {...}
Followed @Hadi answer removed ?
按照@Hadi 的回答删除了
?
in Menu?
在
Menu?
so code is now:所以代码现在是:
override fun onCreateOptionsMenu(menu: Menu): Boolean {...}
It's working perfectly now!它现在工作得很好!
i have fixed this problem. 我已经解决了这个问题。 The because of it is you don't config you common module.
因为它是你没有配置你的通用模块。 Please add two lines code at your common module's build.gradle top:
apply plugin: 'kotlin-android' apply plugin: 'kotlin-android-extensions'
hope this can help you. 请在你的公共模块的build.gradle顶部添加两行代码:
apply plugin: 'kotlin-android' apply plugin: 'kotlin-android-extensions'
希望这可以帮到你。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.