Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed last month.
Improve this question
Modifier 'override' is not applicable to 'local function'
I have tried checking every opening and closing braces and also restarted Android Studio.
move your method below closing bracket of onCreate call
override fun onCreate(savedInstanceState: Bundle?) {
... current code
}
override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
... new code
}
private fun generateSampleData(): List<UserMap> {
... rest of current code
this method belongs to whole class extending Activity not "local function", as on the screenshot you are placing it inside another overriden method
btw. onActivityResult is deprecated now, you should move to new approach
Related
Closed. This question is opinion-based. It is not currently accepting answers.
Want to improve this question? Update the question so it can be answered with facts and citations by editing this post.
Closed 6 months ago.
Improve this question
I was always facing this memory leak warning without paying much attention to it, but now I'm looking into how to deal with it and, as I know I shouldn't use WeakReference and that sort of "tricks" to avoid it, come to what I think could be a possible and simple solution.
My idea is as follows:
I have a singleton class (object) which holds all my app configuration, where I initialize a context from the Application class like this:
object AppSettings {
lateinit var context: Context
fun init(appContext: Context){
this.context = appContext
}
/* OTHER STUFF */
}
typealias aw = AppSettings
#HiltAndroidApp
class AWApplication : MultiDexApplication() {
override fun onCreate() {
super.onCreate()
AppSettings.init(this)
}
/* OTHER STUFF */
}
I initialize that context not only in ApplicationClass, but in every activity OnCreate (which inherits from BaseActivity):
#AndroidEntryPoint
open class BaseActivity {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
AppSettings.init(this)
}
}
And finally, I can access context wherever it is needed as follows:
object RandomObject {
fun DoWhatever() {
PlayAFreddieMercurySong(aw.context,)
}
}
Well, this is my possible solution and I would like to know Android gurus from SO opinion about it.
Maybe I'm leaking memory in my App Settings -where I had initially store context-, but Android Studio is not complaining about it, so I am not sure.
In the end, I'm trying to avoid passing context as a method parameter in every place it is needed for code simplicity.
"other stuff" are common between all activities and they need just ApplicationContext, then why you don't use application context in AppSettings. and thats it. BTW your solution will not leak, if and only if you call your AppSettings.init(this) in all activities.
and you don't guarantee that .
in other words "the code doesn't leak now but may be in the future" - vulnerable
if you have functions thats related to activities, fragments or any
class you want, you can use extension functions for that
you should create a kotlin file with name for ex ActivityExt. and write all of your cases that you need for activities . if you need functions for fragments you should also create another kotlin file with name FragmentExt..etc
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 12 months ago.
Improve this question
Here's the code
class MainActivity : AppCompatActivity() {
private lateinit var binding: ActivityMainBinding
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
binding = ActivityMainBinding.inflate(layoutInflater)
setContentView(binding.root)
binding.myButton.setOnClickListener (object: View.OnClickListener {
override fun onClick(**v: View?**) {
binding.statusText.text = "Button Clicked"
}
})
}
}
Why does this code work when no argument is passed to onClick. I mean 'onClick()' takes a 'View' so we must call it like this: 'onClick(myButton)'.
The user interface control that is clicked, in this case a button, is a View, so it is passed as the argument to onClick().
Because the View.OnClickListener interface explicitly defines that the view that has been clicked is going to be passed to you.
You can certainly omit the value, but it's going to be there. Since it's a compiled language, in order to satisfy the interface you need to take that parameter regardless of whether you'll use it.
In JS world, which you may be getting confused with; is a dynamically typed language and hence in order for things to work you're just required to provide your callback. Either way, the parameters are still going to get passed into the function, but you'll not be referencing/using them
You typically never call onClick yourself. The framework does it for you when you physically touch the view. And it will pass the view you actually touched.
You might wonder:
Why does it even pass it?, I know which view I put the listener on
The thing is. You can give the same listener to multiple views. Having this as parameter makes you able to distinguish in the listener which of the views was clicked.
As a side note, this is a more idiomatic way to write it, and does exactly the same as your code
binding.myButton.setOnClickListener {
binding.statusText.text = "Button Clicked"
}
Writing it like this, will have the view parameter available as it
Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 12 months ago.
Improve this question
Implementing BaseOnSliderTouchListener's onStartTrackingTouch and onStopTrackingTouch (documentation) give the lint the following error message :
Error: BaseOnSliderTouchListener.onStartTrackingTouch can only be called from within the same library group (referenced groupId=com.google.android.material from groupId=your-group-id) [RestrictedApi]
I had the same issue. The workaround with #SuppressLint("RestrictedApi") works.
The root cause is that the BaseOnSliderTouchListener class has a library restricted scope and the methods exposed there are not overridden in OnSliderTouchListener.
The issue is tracked in the material-components library here: https://github.com/material-components/material-components-android/issues/2493
It has been fixed in the 1.6.0-alpha02 release of material-components.
Temporary solution:
Add #SuppressLint("RestrictedApi") annotation.
Example:
slider.addOnSliderTouchListener(object : Slider.OnSliderTouchListener {
#SuppressLint("RestrictedApi")
override fun onStartTrackingTouch(slider: Slider) {
[...]
}
#SuppressLint("RestrictedApi")
override fun onStopTrackingTouch(slider: Slider) {
[...]
}
})
Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 1 year ago.
Improve this question
None of the following functions can be called with the arguments supplied:
public open fun makeText(context: Context!, text: CharSequence!, duration: Int):
Toast! defined in android.widget.Toast
public open fun makeText(context: Context!, resId: Int, duration: Int): Toast! defined in android.widget.Toast
You call makeText() function with wrong arguments.
if you have overloaded functions for same amount of arguments, but call it with wrong type arguments, you'll have this error. In this situation there is 2 function with 3 arguments.
Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 1 year ago.
Improve this question
I am currently in a android app development course and am brand new to kotlin. Right now I'm working on building a basic pizza ordering application that once the user makes selections and hits "submit" a textview is edited to show the total price... Except that I cant get the textview to display anything. What am i missing here because i followed the book step-by-step (but it seems to be fairly old) thank you so much!
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.view.View
import kotlinx.android.synthetic.main.activity_main.*
import kotlinx.android.synthetic.main.activity_main.view.*
class MainActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
}
fun onPlaceOrderButtonClicked(view: View) {
var pizzaSizePrice=0.0
var toppingsTotal = 0.0
when {
radioGroup.smallpizza.isChecked -> pizzaSizePrice=5.0
radioGroup.mediumpizza.isChecked -> pizzaSizePrice=7.0
radioGroup.largepizza.isChecked -> pizzaSizePrice=9.0
}
if (OnionsCheckBox.isChecked){toppingsTotal+=1}
if (OlivesCheckBox.isChecked){toppingsTotal+=2}
if (TomatoesCheckBox.isChecked){toppingsTotal+=3}
Totalprice.text=("Total order price= $" + (pizzaSizePrice+toppingsTotal))
}
}
It doesn't look like your code calls onPlaceOrderButtonClicked, try adding this:
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
// Calls your function when button is clicked
orderButton.setOnClickListener {
onPlaceOrderButtonClicked()
}
}
This will direct clicks from the button with id orderButton to onPlaceOrderButtonClicked(). Also, remove view: View from that function, it's not being used.
I'm guessing you aren't calling the onPlaceOrderButtonClicked(), there a number of ways to handle this. I'm my guess is your book is using the XML approach because the onPlacedORderButtonClicked() has a view argument. Try including an onClick in the button XML.
<Button
android:id="#+id/btn_orderpizza"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="PLUS"
android:onClick="onPlaceOrderButtonClicked" />