问题
I've tried to extend Resources
class, so i can override getString(int id)
methode, so can do some effects to every String
needed within my Activity
, here i provide some my code:
public class CustomResource extends Resources {
public CustomResource(AssetManager assets, DisplayMetrics metrics, Configuration config) {
super(assets, metrics, config);
}
@Override
public String getString(int id) throws NotFoundException {
return super.getString(id) + " $$";
}
}
and in my Activity :
CustomResource customResource;
@Override
public Resources getResources() {
if (customResource == null) {
DisplayMetrics metrics = new DisplayMetrics();
super.getWindowManager().getDefaultDisplay().getMetrics(metrics);
customResource = new CustomResource(getAssets(), metrics, super.getResources().getConfiguration());
}
return customResource;
}
but there's error shown on Runtime
:
Caused by: java.lang.NullPointerException
at com.test.TestActivity.getResources(TestActivity.java:75)
and it points to:
super.getWindowManager().getDefaultDisplay().getMetrics(metrics);
回答1:
i solved it:
@Override
public Resources getResources() {
if (customResource == null) {
customResource = new CustomResource(super.getAssets(), super.getResources().getDisplayMetrics(), super
.getResources().getConfiguration());
}
return customResource;
}
all of that, to add some text to all Strings
needed in XML, when i use setContentView(R.layout.sample)
, but it did not work :(
来源:https://stackoverflow.com/questions/6889633/how-to-override-getresources-in-activity