Problem referring to R globally - android

I'm trying to declare some final Strings for using as keys later, but I keep getting a null pointer exception.
private final String KEY = getString(R.string.key);
This is declared at the top of my activity, before onCreate(). Does the problem stem from referring to R in the class?

The problem is, that you cannot access the getString(..) there.
Why dont you just save the int value?
private static final int KEY = R.string.key
and than where you need it call the yourContext.getString(KEY);
:)

These initializers will be called when the object is constructed, which may be before it has a valid context.
Don't call getString() before onCreate(). You won't be able to have the strings final, though.

Related

How To Assign Instance ID When Initializing LoaderManager

In order to use AsynTaskLoader, I first need to initialize LoaderManger. The first argument is a unique ID (integer) of the instance.
Is there a best practice to assign each ID?
Is it simple as several examples I found and assign it 0 or 1?
Do I need to worry about instances clashing with each other?
Below is an example code with ID set to 0.
getLoaderManager().initLoader(0, null, this);
Firstly to use AsyncTaskLoader you need to initialize corresponding Loader and not LoaderManager.
Is there a best practice to assign each ID?
Is it simple as several examples I found and assign it 0 or 1?
You should declare ID as constants like -
private static final int TASK_A = 1;
private static final int TASK_B = 2;
Do I need to worry about instances clashing with each other?
No, even if you call initLoader again being called before it just skips the onCreateLoader method and directly calls onLoadFinished with cached copy of previous data

Android DB controller error

Hello everyone :) can you explain why I get this and tell me what how I have to change my code. Waiting for the answers. Have a nice day:)
controller
cannot be used in a static method. You need to make the controller object static to be able to use in a static method.
It appears that you are attempting to use a non-static member field inside a static method. This is not allowed since you need an instance of the class to access the member field. You have two choices:
Make the member field a static class variable by adding the static modifier to its declaration.
Remove the static modifier from the method to make it non-static.
You should definitely prefer the second solution over the first. As a general rule of thumb, all methods and variables should be non-static unless you have a very good, specific reason to make them static. One common use of static variables is for final constants. You also might find a situation where all instances of a class share a single value. Note that these are the exception, not the rule.

Bitmap loosing reference on static hashmap?

I got an static hashmap in order to be able to instantiate multiple times the same class, and the hashmap will hold all the key-value reference.
class A {
public static final Map<String,Bitmap> map = new HashMap<String,Bitmap>();
// methods
}
So on every instance of the class A, when you call map.get(KEY), you'll always get the same bitmap. I'm getting an "Released unknown bitmap reference" IllegalStateException What am I doing wrong?
Sometimes, if Android needs memory, it deletes some variables.
So if your app is having a lot of big vars like Bitmaps in static variable, they can be deleted sometimes by the system. I had this problem with an app. The only solution I found is that you have to check on your getter if the object exists, if it doesn't, you'll have to recreate it...
Are you calling .recycle() on the bitmap?
If so when you come to get the bitmap from the hashmap it will no longer exist and throw that exception

Android result of a method as a static property

Will the code below be effective? I mean, will it actually be run just one and the result will be cached in the property, so the next call will not re-run the method anymore?
public static final String LOG_TAG = MyApplication.getContext().getApplicationInfo().name;
Somehow I cannot find it anywhere.
comment.
Since you guys seem to focus on not important things, here's the changed code:
public static final String SomeVar = MyApplication.someMethod();
You can't do that, since Application.getContext() method isn't static.
EDIT: I see you edited your question: So if your MyApplication.someMethod() method is static, than ofcourse it works, and someMethod() will be only called once, unless your class gets destroyed and recreated by the garbage collector.
I'm not sure. I usually get it this way:
My(Activity)Class.class.getSimpleName();

Instantiate variables in class instead of onCreate() wrong?

Is something wrong with this construct in Android?
class A extends Activity {
private Object myObject = new Object();
#Override
protected void onCreate(Bundle savedInstanceState) {
//myObject = new Object();
}
}
Because at some point(s) later I get (sometimes, not reproducible yet) exceptions because myObject is null. I don't know if it's because I have to initialize in onCreate.
Edit: Additional details:
The actual class of myObject is List<Object> (Where Object is a domain specific type)
At some point later in the activity I'm storing myObject as a static field of a "Parameter passer" class and starting other Activity (because I'm avoiding to implement Parcelable. If this is good or bad practice should not be discussed here, unless that's causing my error). In the other Activity I pick up myObject. There it's (sometimes) null.
Edit 2: I don't understand why this object becomes null if I'm storing a reference to it as static field of my parameter passer class (a standalone, dedicated class). That's how garbage collection works, right, it just removes when the objects are not referenced anymore. So since I have a static reference this object should not be removed. According to this thoughts, if they are correct, the problem should be somewhere else.
When you start a new activity your old one goes on the block for possible garbage collection (including any classes instantiated in it, including your parameter passer class), so your object is not necessarily going to be available (which is why you see an intermittent failure.).
I see two option:
1) Pass it along in the bundle with your intent that starts the new activity. As you were trying to avoid this, probably not your best choice.
2) Extend the Application class and store the object in there.
EDIT
I think the accepted answer to this SO Question might fix your issue (and explain what is actually happening).
No. That code is just fine. You can create objects in the constructor.
You may want to check a previous question about it Instance variable initialization in java and the section 3.2.4. Field Defaults and Initializers which basically states that the first case:
private Object myObject = new Object();
is identical to an initialization in the class constructor. (NOTICE onCreate is NOT the constructor).
So, myObject should never be null, except in the case the "new Object()" instruction failed, generating an exception.
Isn't this possible your code is changing the contents of myObject later on the code?

Categories

Resources