My software involves changing images a lot. Rather than look up the resource number repeatedly, I would like to create some constants to represent each image reference. Here are my attempts so far:
This attempt produces a force close on start up.
private final int EMPTY = getResources().getIdentifier("dotted_circle", "resId", "en.deco.android.livehud");
@Override
public void onCreate(Bundle savedInstanceState)
{
.....
}
This Attempt returns an error "final variables may not be initialized". Removing final allows it to work.
private final int EMPTY
@Override
public void onCreate(Bundle savedInstanceState)
{
EMPTY = getResources().getIdentifier("dotted_circle", "resId", "en.deco.android.livehud");
}
Is using constants here a good idea in the first place?
Is using constants here a good idea in the first place?
No. It's better a routine that read the value from resources:
public class ImageUtils {
private static int empty = -1;
public static int getEmpty(Context context) {
if(empty == -1)
empty = context.getResources().getIdentifier("dotted_circle", "resId", "en.deco.android.livehud");
return empty;
}
}