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)
}
}
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)
}
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?
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)
}
Update function signatures or revert to compileSdk from 27 to 26
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.
Related
When I normally I app run after building android studio throws this error.
at com.shayan.test.databinding.ActivityMainBinding.inflate(ActivityMainBinding.java:113)
E/AndroidRuntime: at com.shayan.test.MainActivity.onCreate(MainActivity.kt:14)
I think I forget something to do calling method or something
package com.shayan.test
package com.shayan.test
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.view.LayoutInflater
import android.view.View
import com.shayan.test.databinding.ActivityMainBinding
class MainActivity : AppCompatActivity() {
lateinit var binding: ActivityMainBinding
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
binding = ActivityMainBinding.inflate(LayoutInflater.from(this))
(binding.root)
}
}
your using viewbinding.. setContentView is missing. and
binding = ActivityMainBinding.inflate(LayoutInflater.from(this))
change to
binding = ActivityMainBinding.inflate(layoutInflater)
Full code for example
class MainActivity : AppCompatActivity() {
private lateinit var binding: ActivityMainBinding
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
binding = ActivityMainBinding.inflate(layoutInflater)
setContentView(binding.root)
Try this methods :
//Try to add in build.gradle (Module:app)
buildFeatures {
viewBinding true
}
2:
Build -> Clean Project
Build -> Rebuild Project
If this method does not help you then It's not about the Gradle file. It is about the XML file.
The kotlin-android-extensions Gradle plugin is deprecated. Please use this migration guide to start working with View Binding and the kotlin-parcelize plugin.
C:\\Users\\ADMINISTRATOR\\AndroidStudioProjects\\MyApplication4\\app\\src
\\main\\java\\com\\denizas\\myapplication\\MainActivity.kt: (14, 21):
Unresolved reference: editText
Caused by: org.gradle.api.GradleException:
Compilation error. See log for more details
build.gradle(Module:App)
android {
....
buildFeatures {
viewBinding true
}
}
MainActivity.kt
class MainActivity : AppCompatActivity() {
private var _binding: ActivityMainBinding? = null
private val binding
get() = _binding!!
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
_binding = ActivityChartBinding.inflate(layoutInflater)
setContentView(R.layout.activity_main)
}
fun topla(view: View){
binding.apply {
//in this part it is used to access the views in the layout part
var sayi1 = editText.text.toStirng()
}
}
}
I have this code:
class SignInActivity {
private lateinit var binding: SingInBinding
fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
binding = SingInBinding.inflate(layoutInflater)
setContentView(binding.root)
} // Start GameActivity
}
But I have errors in onCreate, layoutInflater, and setContentView: unresolved reference.
How can I fix it?
I want to fix the problem.
You are not extending any Activity class, you should do something like
class SignInActivity : ComponentActivity() {
...
}
Attempting to update an older Android game project to use view binding. Android Studio version 4.1.3. I am thinking my problem has to do with the naming of my binding class.
the xml file is called activity_correct_guess.xml and I am using what I think is the name that gets generated by the view binding: ActivityCorrectGuessBinding. Appreciate and ideas!
The build errors:
Unresolved reference: ActivityCorrectGuessBinding
Unresolved reference: binding
Unresolved reference: binding
In the Gradle build module I have the following:
android {
compileSdkVersion 30
buildToolsVersion "30.0.0"
buildFeatures {
viewBinding = true
}
layout file: activity_correct_guess.xml
<Button
android:id="#+id/btnPlayAgain"
android:layout_width="wrap_content"
..... />
Activity file: CorrectGuessActivity.kt
class CorrectGuessActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
val binding = ActivityCorrectGuessBinding.inflate(layoutInflater)
val view = binding.root
setContentView(view)
playAgain()
exitGame()
}
fun playAgain() {
binding.btnPlayAgain.setOnClickListener {
val intent = Intent("com.appkotlin2021v4.MainActivity")
startActivity(intent)
}
}
Typically, you define your view binding in the root of your Activity or Fragment or in an init block:
class CorrectGuessActivity : AppCompatActivity() {
//get to inflating!
private val binding = ActivityCorrectGuessBinding.inflate(layoutInflater)
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(binding.root)
playAgain()
exitGame()
}
fun playAgain() {
binding.btnPlayAgain.setOnClickListener {
val intent = Intent("com.appkotlin2021v4.MainActivity")
startActivity(intent)
}
}
The only time you need to "wait to inflate" is when you're in a non-ViewGroup-based class like RecyclerView.Adapter, etc.
Yes, this is the correct way to define the view binding. thank you!
Code:
class MainActivity: AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
}
button3.setOnClickListener { }
}
Screenshot of code with IDE error: link
I haven't idea what the error might be. Reloading and rebuild didn't help.
You're writing your code outside of MainActivity's onCreate (or any other) method scope.
Your code is:
class MainActivity: AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
}
button3.setOnClickListener { }
}
But must be:
class MainActivity: AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
button3.setOnClickListener {
// do something
}
}
}
You can use class-level declaration for methods, inner classes etc, but not for writing code.
And calling views directly by their ids is possible only using Kotlin Android Extensions. Check if you're using it and move your code to the one of methods' scope, and code will works.
All actions must be inside an any function. Your button3.setOnClickListener { } written outside the function.
And when you add apply plugin: 'kotlin-android-extensions' your app.gradle file, widgets can be used directly via ID. Without this you need to declare your btn.
you must go in gradle scripts and select build.gradle(module.....)
and add this line:
( id 'kotlin-android-extensions' )
in plugins {
id 'com.android.application'
id 'kotlin-android'
here
}
and sync now