First time trying RoboGuice. Till now everything with injecting Views works smoothly.
Since I see in the tutorial I can inject Resources
I tried to add an AnimatorSet
and a get error :
Reason: java.lang.NullPointerException: Can't inject null value into class myroboguice.teo.ram.css.myroboguicetest.MainActivity.animatorSet when field is not @Nullable
My code is :
public class MainActivity extends RoboActivity {
@InjectView(R.id.textId) TextView textView1;
@InjectView(R.id.buttonId) Button button1;
@InjectView(R.id.buttonId2) Button button2;
@InjectResource(R.animator.button_anim)AnimatorSet animatorSet;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
textView1.setText("Animator Set");
button1.setBackgroundColor(Color.YELLOW);
}
public void firstButton(View v) {
Toast toast = Toast.makeText(this, "This is a Toast", Toast.LENGTH_SHORT);
toast.show();
}
public void secondButton(View v) {
animatorSet = new AnimatorSet();
animatorSet.setTarget(button2);
animatorSet.start();
}
}
LogCat :
1) Error injecting myroboguice.teo.ram.css.myroboguicetest.MainActivity using roboguice.inject.ResourceListener$ResourceMembersInjector@41694450.
Reason: java.lang.NullPointerException: Can't inject null value into class myroboguice.teo.ram.css.myroboguicetest.MainActivity.animatorSet when field is not @Nullable
while locating myroboguice.teo.ram.css.myroboguicetest.MainActivity
1 error
at com.google.inject.internal.Errors.throwProvisionExceptionIfErrorsExist(Errors.java:451)
at com.google.inject.internal.MembersInjectorImpl.injectMembers(MembersInjectorImpl.java:65)
at com.google.inject.internal.InjectorImpl.injectMembers(InjectorImpl.java:944)
at roboguice.inject.ContextScopedRoboInjector.injectMembersWithoutViews(ContextScopedRoboInjector.java:243)
at roboguice.activity.RoboActivity.onCreate(RoboActivity.java:78)
at myroboguice.teo.ram.css.myroboguicetest.MainActivity.onCreate(MainActivity.java:26)
at android.app.Activity.performCreate(Activity.java)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java)
... 12 more
Caused by: java.lang.NullPointerException: Can't inject null value into class myroboguice.teo.ram.css.myroboguicetest.MainActivity.animatorSet when field is not @Nullable
at roboguice.inject.ResourceListener$ResourceMembersInjector.injectMembers(ResourceListener.java:118)
at com.google.inject.internal.MembersInjectorImpl.injectMembers(MembersInjectorImpl.java:120)
at com.google.inject.internal.MembersInjectorImpl$1.call(MembersInjectorImpl.java:75)
at com.google.inject.internal.MembersInjectorImpl$1.call(MembersInjectorImpl.java:73)
at com.google.inject.internal.InjectorImpl.callInContext(InjectorImpl.java:1024)
at com.google.inject.internal.MembersInjectorImpl.injectAndNotify(MembersInjectorImpl.java:73)
at com.google.inject.internal.MembersInjectorImpl.injectMembers(MembersInjectorImpl.java:60)
... 18 more
I found the source code of TypeListener of Roboguice here, and review the code on line 178, I found the exception:
if (view == null && Nullable.notNullable(field))
throw new NullPointerException(String.format("Can't inject null value into %s.%s when field is not @Nullable", field.getDeclaringClass(), field.getName()));
On definition class of Nullable:
public class Nullable {
private Nullable() {
}
public static boolean notNullable( Field field ) {
return !isNullable( field );
}
public static boolean isNullable(Field field) {
for( Annotation a : field.getAnnotations() )
if( Strings.equals("Nullable",a.annotationType().getSimpleName()))
return true;
return false;
}
}
Just annotate your field with
@javax.annotation.Nullable
annotation and the exception dissapear.
Anyway the animator variable will remain null, because Animator resources are not loaded on injectMembers method on Roboguice:
public void injectMembers(T instance) {
Object value = null;
try {
final Resources resources = application.getResources();
final int id = getId(resources,annotation);
final Class<?> t = field.getType();
if (String.class.isAssignableFrom(t)) {
value = resources.getString(id);
} else if (boolean.class.isAssignableFrom(t) || Boolean.class.isAssignableFrom(t)) {
value = resources.getBoolean(id);
} else if (ColorStateList.class.isAssignableFrom(t) ) {
value = resources.getColorStateList(id);
} else if (int.class.isAssignableFrom(t) || Integer.class.isAssignableFrom(t)) {
value = resources.getInteger(id);
} else if (Drawable.class.isAssignableFrom(t)) {
value = resources.getDrawable(id);
} else if (String[].class.isAssignableFrom(t)) {
value = resources.getStringArray(id);
} else if (int[].class.isAssignableFrom(t) || Integer[].class.isAssignableFrom(t)) {
value = resources.getIntArray(id);
} else if (Animation.class.isAssignableFrom(t)) {
value = AnimationUtils.loadAnimation(application, id);
} else if (Movie.class.isAssignableFrom(t) ) {
value = resources.getMovie(id);
}
if (value == null && Nullable.notNullable(field) ) {
throw new NullPointerException(String.format("Can't inject null value into %s.%s when field is not @Nullable", field.getDeclaringClass(), field
.getName()));
}
field.setAccessible(true);
field.set(instance, value);
} catch (IllegalAccessException e) {
throw new RuntimeException(e);
} catch (IllegalArgumentException f) {
throw new IllegalArgumentException(String.format("Can't assign %s value %s to %s field %s", value != null ? value.getClass() : "(null)", value,
field.getType(), field.getName()));
}
}
on ResourceListener class on Roboguice.
And I don´t know really why, beacause is just add another condition:
else if (Animator.class.isAssignableFrom(t)) {
value = //read animator from resources
}
Unable to use Roboguice is not end of the world. You can load the animator as indicate the documentation.
Modify your code:
public void secondButton(View v) {
animatorSet = (AnimatorSet) AnimatorInflater.loadAnimator(this, R.anim.button_anim);
animatorSet .setTarget(button2);
animatorSet .start();
}