Kotlin inheritnce - No value passed for parameter context - android

I'm trying to build an AccountAuthenticator class with kotlin for android. But when trying to implement the AbstractAccountAuthenticator class I get the following exception at compile:
No value passed for parameter context
I'm not entirely sure what it means and can't find anything on how to solve it.
Here is the relevant code:
import android.accounts.AbstractAccountAuthenticator
import android.accounts.Account
import android.accounts.AccountAuthenticatorResponse
import android.os.Bundle
class AccountAuthenticator: AbstractAccountAuthenticator() {}
Does anyone know what this means, why, and how to fix it?

AbstractAccountAuthenticator's constructor takes a Context context parameter. So you'll have to pass a Context to it somehow, for example, your AccountAuthenticator could also have a Context parameter:
class AccountAuthenticator(context: Context): AbstractAccountAuthenticator(context) {}

I don't know much about Kotlin but AbstractAccountAuthenticator constructor takes a Context see here.
So I guess you have to implement this constructor and other related abstract methods.

Related

Actual typealias 'ApplicationContext' has no corresponding expected declaration

I got this issue when trying to custom context for kotlin multiplatform
Actual typealias 'ApplicationContext' has no corresponding expected declaration
The following declaration is incompatible because modality is different:
public final expect class ApplicationContext
androidMain
import android.app.Application
actual typealias ApplicationContext = Application
commonMain
expect class ApplicationContext
iosMain
import platform.UIKit.UIView
actual typealias ApplicationContext = UIView
The expect class structure should match the class you're typealiasing to.
Android's context class is an abstract class
public abstract class Context {}
So the modality error comes from there.
You would need the expect class to be abstract as well to fix that particular error
expect abstract class ApplicationContext
Making it abstract would break modality for UIView as it's not abstract. So what you're trying to do is not possible in a straightforward manner.
You would need to think about a different strategy for what you're trying to achieve.

Import Android Context into a function file in Kotlin

I try to create a POST request with Android Volley but when I type volleyRequestQueue = Volley.newRequestQueue(this), this is a error "this is not defined in this context".
I know that I have this problem because my function can't resolve Android Context but I don't understand how to import the context in my file.
Here you have my project scheme:
and the error
The function is called in the "FormsAddAliments" file.
Thanks you !
The error "this is not defined in this context" is not referring to the abstract class android.content.Context, but rather the context of the code.
You are using this in a top level function in a file. There is no object that this can be a reference to, if you want it to be a top level function and have an object that this can reference, you will need to declare it as an extension function on the type that you want this to reference like Context.sendFoodToServer(...). This should work, but it is likely violating some design principles to do it this way (usually passing a context around is a bad idea because it can lead to context leaking).

CoordinatorLayout.DefaultBehaviour deprecated, no other option

So according to android documentation, defaultBehaviour is deprecated and AttachedBehaviour should be used instead.
However:
does not "exist" in android. I always receive the Annotation type expected error.
My import is:
import androidx.coordinatorlayout.widget.CoordinatorLayout;
Am I using the wrong import?
AttachedBehavior is an interface, not an annotation.
Therefore your CustomLinearLayout must implement AttachedBehavior and override the getBehavior() method to return an instance of your MoveUpwardBehavior class.

Kotlin do not call super

I have a class that extends another, but in this class I do not want to call the super constructor.
How can I solve it?
Here is a snipet of my code
class SubarticlePagerAdapter(fragmentManager: FragmentManager, context: Context, var selectedArticleName: String) : ArticlePagerAdapter(fragmentManager, context) {
var subarticleDao: ArticleDao
var itemCount = 0
init {
ApplicationHelper().getApplication(context).appComponent.inject(this)
subarticleDao = ApplicationHelper().getApplication(context).subarticleDaoSession.articleDao
initBundles(context)
}
override fun initBundles(context: Context?) {
}
}
My problem, when this constructor is called, parent class constructor run first, and initBundles() will be called from there, but at that time subarticleDao and selectedArticleName are not set and I get exception.
TL;DR
I'd advise you to move the code from the init block to the initBundles function and use your variables there after initialization. Then there would be no need to avoid calling the superclasses constructor.
Extensive Answer
I think you should think about what you want to do with your design. Working around the idioms of a language is not very often a good idea or a sign of good design - at least when kotlin is your language :)
What you did with your code (overriding a - possibly abstract - method, initBundles from your superclass is pretty much the template method pattern. So it seems to me the purpose of initBundles is to let subclasses customize parts of the initialization... What basically is what you do in your init block.
EDIT: As Paul pointed out in the comments, you can't use the member selectedArticleName before your base classes initialization has finished. So if the base class calls initBundles during its initialization, then properties in the subclass won't be initialized as also stated at Paul's link.
Since in the snippet you don't use selectedArticleName, you could just move your initialization stuff to the initBundles function and init your subarticleDao there.
However, if you need to use your subclasses properties at that point, I'd really advise you to rethink your design. There should be several ways to solve this, but to decide what would suits your requirements best one would need further insight into the intentions you have with your design.

Cant inject null value into class

I have a fragment that I want to reuse. Its functionality is the same, only the layout changes
I am using roboguice to inject views by id into the variables
I added this view for example:
#Nullable
#InjectView(R.id.edtEventLocationAddress)
private EditText edtEventLocationAddress;
now this view may or may not be present in the given layout i provided in the onCreateView method
this is why i put #Nullable on it
however, when I run the app, with the layout that does not have this view, I get
java.lang.NullPointerException: Can't inject null value into class
com.myapp.CreateEventPageFragment.edtEventLocationAddress when field is not #Nullable
What do I need to do to make roboguice allow me to reuse fragments, and only change their view ?
A late answer but just in case anyone else stumbles on this.
You are probably using android.support.annotation.Nullable or a similar annotation that has #Retention(RetentionPolicy.CLASS) (or RetentionPolicy.SOURCE). However, you need to use a #Nullable annotation that is retained at runtime for RoboGuice to find it. E.g. this one:
import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
#Target({ElementType.FIELD, ElementType.METHOD,
ElementType.PARAMETER,
ElementType.LOCAL_VARIABLE})
#Retention(RetentionPolicy.RUNTIME)
public #interface Nullable {}
Import javax.annotation.Nullable instead of android.support.annotation.Nullable as it has runtime retention policy (see Michael.F answer).
Another possibility (which is how I ended up on this question) is that you have a typo in your XML file or in the Inject command, and it's trying to inject null because it didn't actually find the ID you specified.

Categories

Resources