Why do I get the error message The method getResources() is undefined for the type ColorObjectManager?
I use this line to load a Bitmap image:
orange = BitmapFactory.decodeResource(getResources(), R.drawable.pearl_orange);
It's working fine if I'm doing this in another class that I call GameLoop which I make an object of inside the MainActivity class. But it's not working when I trying to do this in the class ColorObjectManager which I make an object of inside the GameLoop class. Do you follow?
Why am I limited to just use this loading part in the GameLoop class and not in the ColorObjectManager class? I thought it would help if I passed the Context to the constructor of ColorObjectManager, but it didn't! I guess I'm missing some knowledge here where I can create objects and not. Can I get some help to sort this out? Thanks!
getResource needs a Context object. If you pass the context to ColorObjectManager you can retrieve resources with context.getResources()
I agree with the answer posted by blackbelt. Pass the activity context to the constructor of ColorObjectManager from your activity class.
new ColorObjectManger(ActivityName.this);
Constructor
Context mContext;
public ColorObjectManager(Context context)
{
this.mContext= context;
}
Then use the context to get resources.
Edit:
If you want to use the context only in your load method
public ColorObjectManager(Context context)
{
load(context);
}
To get access getResources() , Activity context is required. Your ColorObjectManager is not an Activity. So you need to pass the Activity context to this class.
Related
How do you pass a Context from a running fragment to another class? I need to get resource data out of it, but for some reason, it appears to be null when I send it through the arguments.
class BeatBoxFragment : Fragment() {
...
var mBeatBox: BeatBox = BeatBox(this.context!!)
...
}
I will be using the context of this fragment to access assets from the activity.
class BeatBox(context: Context) {
...
val mAssets: AssetManager = context.getAssets()
...
}
The program crashes if I try to call BeatBox with the arguments this.context!!. Apparently it is null (according to what my stack trace suggests: android.beatbox.BeatBoxActivity}: kotlin.KotlinNullPointerException")
Any help is greatly appreciated.
Use requireContext() which ensure that Fragment is attached and returns a valid non-null Context which we can use without any trouble.
Also you can use applicationContext to access asset which is not depends on Activity Life Cycle
I have seen in many places android context is initialized twice ? Why is it required ?
For Example :
Context context;
context = null;
again they will initialize it to some values in onCreate, onReceive or in some other Method
context = myContext;
If anyway we have to initialize it to value, why then initializing it to null ?
Anything specific to Android ?
Thanks
compiler never assigns a default value to an uninitialized local variable.
If you cannot initialize your local variable where it is declared, make sure to assign it
a value before you attempt to use it. Accessing an uninitialized local variable will result
in a compile-time error.
Example:
someMethod(Context myContext){
Context context;
context = null; //Have to be done to avoid compile-time error
context = myContext;
}
Example 2:
private Context context;
someMethod(Context myContext){
context = myContext; //No initialization required, because context variable is not local
}
To Answer your question:
We are initializing variable to null to keep consistency intact
No, this is not Android specific, but rather Java specific
I would guess this is done of old habits only. Java should by default set field object references to null. I doubt Android handle this differently.
Setting them to null might even result in duplicate operations on byte code level. For further information refer to this page.
Let's use custom or extending View as an example.
Is it more effective to save Context parameter from constructor as a field, than calling getContext() everywhere (supposing there are, let's say, 10 or more places where it is needed)?
Instead of using getContext() every where, it is better to pass current context as argument in constructor where you wanna to use.
View#getContext() is
class View {
protected Context mContext;
public final Context getContext() {
return mContext;
}
}
and a locally cached implementation:
class X {
private final Context mLocalContext;
public X(Context ctx) {
mLocalContext = ctx;
}
}
Now there is a very small difference when you use mLocalContext instead of getContext(). The JVM can get to the required reference of the context object without having to execute the method (which takes a tiny bit of extra time). That call can't be optimized away since View#mContext is mutable (can change). In the local example it can assume that mLocalContext can't change and optimize the code a little better. [Note: I am not 100% sure about what optimizations are / can be done]
The difference might be measurable if you use the context a lot but in this case it does not matter much. It's still a good idea to cache Objects locally if you need them often. Especially when their (re)construction takes time (e.g. when getContext() would create a new Context() or so).
It looks like, from the source code, the View's constructor stores the context parameter and that's what getContext() returns:
http://grepcode.com/file/repository.grepcode.com/java/ext/com.google.android/android/2.2_r1.1/android/view/View.java#View.getContext%28%29
So, my instinct would be that storing the parameter yourself would be redundant, since the super class is already doing it for you.
I can't figure out why the context is getting passed in to this method: http://code.google.com/p/cloud-tasks-io/source/browse/trunk/CloudTasks-Android/src/com/cloudtasks/Util.java#119
The keys of the URL_MAP are the Contexts. So you need the context to get the base url.
In my simple method in non activity class, I am using code:
mgr=(DownloadManager)mContext.getSystemService(DOWNLOAD_SERVICE);
in non activity class, my constructor looks like:
public Download23(Context context){
this.mContext=context;
}
But compilator won't accept DOWNLOAD_SERVICE string. Do you know how to solve that?
Instead you could just write
(DownloadManager)mContext.getSystemService(Context.DOWNLOAD_SERVICE);
That would compile.
DOWNLOAD_SERVICE is a constant of Context class.
you can use
(DownloadManager)mContext.getSystemService(Context.DOWNLOAD_SERVICE);