Home » Android » android – kotlin 'onCreate' overrides nothing

android – kotlin 'onCreate' overrides nothing

Posted by: admin April 23, 2020 Leave a comment

Questions:

I am using component development, in my common module BaseActivity.kt and 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:

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)

    }
}
How to&Answers:

It seems some functions have changed in the last update,
just remove the ‘?‘ from the Bundle

Like this:

override fun onCreate(savedInstanceState: Bundle) {
    super.onCreate(savedInstanceState)
}

Answer:

It can happen also in a Fragment class: I found the same issue for the method onCreateView; to avoid that, just remove the ? from the LayoutInflater parameter,

Like this:

override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View?

Answer:

I have to add “?” when using appcompat-v7:27.1.1

implementation "com.android.support:appcompat-v7:27.1.1

and

override fun onCreate(savedInstanceState : Bundle?){
    super.onCreate(savedInstanceState)
}

Answer:

Update function signatures or revert to compileSdk from 27 to 26

Answer:

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.