kotlin 'onCreate' overrides nothing

36,383

Solution 1

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)
}

Solution 2

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?

Solution 3

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)
}
Share:
36,383
RootKit
Author by

RootKit

Updated on July 09, 2022

Comments

  • RootKit
    RootKit almost 2 years

    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)
    
        }
    }
    
    • alex
      alex over 6 years
      Would help to see your code.
    • Kiran Benny Joseph
      Kiran Benny Joseph over 6 years
      please share your code snippet
    • Haresh Chhelana
      Haresh Chhelana over 6 years
      I have try your given code with Android Studio 3.0 stable version and it's working fine. have you using same or other version of android studio ?
    • RootKit
      RootKit over 6 years
      I am also using Android studio 3.0 But my MainActivity.kt is a baseActivity.kt that inherits from module. BaseActivity.kt inside the common module, MainActivity.kt inside module_main.I do not know if you are trying to do so
    • saiedmomen
      saiedmomen over 6 years
      I'm seeing the same error after moving to support library version 27.0.0
    • tynn
      tynn over 6 years
      Unresolved reference: BaseActivity – this might be related to a configuration issue with Gradle...
    • RootKit
      RootKit over 6 years
      oh, thank you, really is the problem of Gradle. My common module did not apply plugin: 'kotlin-android' apply plugin: 'kotlin-android-extensions'
    • Aba
      Aba over 6 years
  • RootKit
    RootKit over 6 years
    Which version can remove '?'
  • Emmanuel Guther
    Emmanuel Guther over 6 years
    Also, if you are getting same error but in a fragment, remove '?' in fragmentManager: FragmentManager
  • romaneso
    romaneso over 6 years
    This answer should be selected as the top-answer. Helped me out!
  • jbass
    jbass about 6 years
    Worked for me for 27.0.2 as well.
  • onmyway133
    onmyway133 almost 6 years
    Yeah, otherwise you will get "Caused by: java.lang.IllegalArgumentException: Parameter specified as non-null is null: method kotlin.jvm.internal.Intrinsics.checkParameterIsNotNull, parameter savedInstanceState"
  • zeeshan
    zeeshan over 5 years
    Worked for me on Android Studio 3.3, appcompat-v7:28.0.0, compileSdkVersion 28
  • Hannah S.
    Hannah S. almost 5 years
    I'm using Android 3.5 and had the same problem the OP had but for onCreateDialog() not onCreate(). I was using the example from developer.android.com/guide/topics/ui/dialogs.html#kotlin which had no ? after Bundle, but adding it fixed it. Thanks so much for the suggestion!
  • mr5
    mr5 almost 4 years
    Had this problem but the solution was to add ? in Bundle. I'm using AS: 4.0, Kotlin: 1.3.72, Gradle: 4.9
  • dicarlomagnus
    dicarlomagnus over 3 years
    this works as well for override fun onDismiss(dialog: DialogInterface?) removing the nullable parameter to override fun onDismiss(dialog: DialogInterface)
  • Taslim Oseni
    Taslim Oseni over 2 years
    Worked like a charm!