Using androidAnnotations in custom views
I'm running into a peculiar issue: I'm writing a custom view that is composed of other views and using R.strings.
Here is a sample of the code:
@EViewGroup(R.layout.reborn_section_view)
public class SectionView extends RelativeLayout {
@StringRes(android.R.string.copy)
String dont_crash;
@StringRes(R.string.expand_less)
String crash;
private final AttributeSet attrs;
public SectionView(Context context, AttributeSet attrs) {
super(context, attrs);
this.attrs = attrs;
}
...
}
The thing is when the Eclipse visual editor displays the layout that is using my custom view, the population of the field crash... well crashes.
It seems that the string cannot be found at design time if the string is defined by the app/library that holds that custom view.
The field dont_crash is populated allright.
For reference, here's the complete stack trace:
android.content.res.Resources$NotFoundException: Could not resolve resource value: 0x7F080072.
at android.content.res.BridgeResources.throwException(BridgeResources.java:693)
at android.content.res.BridgeResources.getString(BridgeResources.java:506)
at de.robv.android.xposed.installer.reborn.ui.module.SectionView_.init_(SectionView_.java:70)
at de.robv.android.xposed.installer.reborn.ui.module.SectionView_.<init>(SectionView_.java:39)
at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
at sun.reflect.NativeConstructorAccessorImpl.newInstance(Unknown Source)
at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(Unknown Source)
To be honest, this is such an edge case that I don't know if something should be done.
And if that's the case, what could be done.
Maybe swallow the exception while retrieving string values if view.isInEditMode()?
Meanwhile, the workaround is obvious: do the stuff the old way.
if (!isInEditMode()) {
Resources resources = getContext().getResources();
more = resources.getString(R.string.expand_less);
}
Well, I don't really know what we should do about it. Like you said, it's an edge case and the right behavior for this is really dependent of the final code.
Of course, we could safely retrieve resources by always checking if view.isInEditMode() is false, but I don't like this, as it add extra instructions for almost nothing.
I think we should just ignore this.
+1 I opened this ticket more to document the behavior and workaround
You did right :)
What about extracting the resource finding into another method, that is only called when not in edit mode? This would solve the preview crashing and should not impact runtime very much if at all noticeable. Example below.
One could probably distinguish between android.R-resources and others in the generated code - the first ones are "edit mode independent".
Just for completeness: there are Designtime Layout attributes (http://tools.android.com/tips/layout-designtime-attributes) which can be used to show some content in the layout editor.
private void init_() {
OnViewChangedNotifier previousNotifier = OnViewChangedNotifier.replaceNotifier(onViewChangedNotifier_);
{
OnViewChangedNotifier.registerOnViewChangedListener(this);
if (!isInEditMode()) {
findResources();
}
}
OnViewChangedNotifier.replaceNotifier(previousNotifier);
}
private void findResources(){
Resources resources_ = getContext().getResources();
s = resources_.getString(string.abc_action_bar_home_description);
stati = resources_.getStringArray(array.stati);
}´´´
Edit: comment layout was weird.
I am encountering a related issue. I have a custom ViewGroup using @EViewGroup, it has references to various beans injected with @Bean. Some of these beans call into things in @AfterInject that throw exceptions when in edit mode. I have no good way to know if I am in edit mode from the @Bean classes to avoid the bad behavior. I wish there was some type of hook or annotation in the creation process that allowed me to conditionally initialize things based on edit mode.
My work around was from within @EViewGroup
@AfterViews protected void init() {
if (!isInEditMode()) { myBean = MyBean_.getInstance_(getContext()); } }