i have activity final variables. extracted values (let's assume they're strings) resources file.
the problem:
if directly assign them on instantiation (as following):
private final string preference_name = getresources().getstring(r.string.preference_name);
i following error:
caused by: java.lang.nullpointerexception: attempt invoke virtual method 'android.content.res.resources android.content.context.getresources()' on null object reference
i understand problem; oncreate() method has not been called yet means cannot access context-related methods (getresources()
).
if want assign value in oncreate() method of activity, error cannot assign value final variable 'preference_name'
the question is: how can final variables assigned resources file? , if not possible, best practice solution?
thanks in advance.
in case think best thing make multiple calls resources. still have change value in 1 place , call getresources()
isn't expensive one.