Parsing current activity to functions in android studio [closed] - android

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 3 years ago.
Improve this question
I want to get read and write rights on the external storage. Therefore I need to give an activity as parameter.
In the Android tutorial and on Stackoverflow the common answer is to use the keyword. I use this https://github.com/tbruyelle/RxPermissions as I read that it is commonly used to make things easier.
this
or
myActivity.this
and on fragment
getActivity()
none of these seem to work and I get can't resolve Activity. Or in the Debug that he cannot cast the activity to fragment
Here is my Class:
public class WebViewActivity extends Activity {
...
...
private void grantPermissions() {
new RxPermissions(WebViewActivity)
.request(Manifest.permission_group.STORAGE) // ask single or multiple permission once
.subscribe( new Consumer<Boolean>() {
#Override
public void accept(Boolean granted) throws Exception {
if (granted) {
// All requested permissions are granted
} else {
// At least one permission is denied
}
}
} );
}
}
How can I parse my activity, am I missing a step?
Thanks for help
Found the solution. I had to clear my cashe.... that was the only reason it was not working.

Instead of
new RxPermissions(WebViewActivity)
You need to use:
new RxPermissions(this)

Related

JetpackCompose get _graph: NavGraph? in NavController get NullPointerException [closed]

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 2 days ago.
Improve this question
My Crashlytics services often notify me about this error. I tried to understand the reason, but I can' reproduce it. I think Jetpack-Compose is quite new and some devices will get errors with it. My navController object provided by local composition.
val navController = localAppNavController.current
java.lang.NullPointerException:
androidx.navigation.NavController.navigate(NavController.kt:1652)
androidx.navigation.NavController.navigate(NavController.kt:1984)
androidx.navigation.NavController.navigate$default(NavController.kt:1979)
It said that the _graph object is null
LaunchedEffect(key1 = contactAction){
when (contactAction) {
is ContactAction.EditContactAction -> {
navController.navigate(Screen.EditContact.withOptionalArgs(mapOf(Constants.RAW_CONTACT_ID to contactAction.editContactId.toString())))
}
is ContactAction.ViewContactAction -> {
navController.navigate(Screen.ContactDetail.withArgs(contactAction.viewContactId.toString()))
}
is ContactAction.CreateContactAction -> {
val phone = contactAction.data[Constants.PHONE]?:""
navController.navigate(Screen.EditContact.withOptionalArgs(mapOf(Constants.PHONE to phone)))
}}

Kotlin: Is this a good way to avoid memory leaks? [closed]

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

Material Slider OnSliderTouchListener's methods can only be called from within the same library group [closed]

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) {
[...]
}
})

I get an error (unresolved reference) in Android Studio when trying to use "new View.OnClickListener" [closed]

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
Im a beginner in Android Studio and have a school project where I have to create an login screen with password and username. When trying to follow some instructions online I get an error even though I have done the same as the instructor. Can you see what I have done wrong?
Example image
Your code is in Kotlin while the video you linked uses Java, so the error indicates that the onClickListener is not following Kotlin syntax properly.
The equivalent in Kotlin is:
logIn.setOnClickListener {
// Do some work here
}
or
logIn.setOnClickListener(object : View.OnClickListener {
override fun onClick(view: View?) {
// Do some work here
}
})
Both will behave similarly. See alternative ways here.
// declare
private lateinit var logIn: Button
// cast
logIn = findViewById(R.id.logIn)
// execute the func you want
logIn.setOnClickListener {
executeLogInApi()
}

Android & Azure Mobile Service Query: how to halt code execution until query finishes [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions concerning problems with code you've written must describe the specific problem — and include valid code to reproduce it — in the question itself. See SSCCE.org for guidance.
Closed 9 years ago.
Improve this question
I need to know how to halt code execution until my mobile service query finishes. I am getting a null pointer exception because the code is attempting to access the query data before the query finishes.
---Code---
mEmployeeTable.execute(new TableQueryCallback<Employee>() {
//get employees
...
});
//In this activity i attempt to use the info loaded from the query above
//however since the query has not completed i get null pointer exception
startActivity(intent);
You need to start the activity only when you receive the callback, something similar to the code below:
mEmployeeTable.execute(new TableQueryCallback<Employee>() {
#Override
public void onCompleted(List<Employee> employees, int count, Exception exception, ServiceFilterResponse response) {
if (exception != null) {
// error happened during query, deal with it appropriately
} else {
// store the loaded employees somewhere
startActivity(intent);
}
}
});

Categories

Resources