我已经尝试扩展Resources
类,因此我可以覆盖getString(int id)
方法,因此可以对String
中所需的每个Activity
产生一些影响,在这里我提供一些代码:
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) + " $$";
}
}
以及在我的活动中:
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;
}
但是Runtime
上显示错误:
原因:java.lang.NullPointerException
在com.test.TestActivity.getResources(TestActivity.java:75)
它指向:
super.getWindowManager().getDefaultDisplay().getMetrics(metrics);
我解决了它: