RoboGuice注入了AnimatorSet

时间:2014-08-25 20:37:51

标签: android roboguice

第一次尝试RoboGuice。到目前为止,注入视图的所有内容都能顺利运行。

因为我在tutorial看到我可以注入资源 我尝试添加AnimatorSet和获取错误:

 Reason: java.lang.NullPointerException: Can't inject null value into class myroboguice.teo.ram.css.myroboguicetest.MainActivity.animatorSet when field is not @Nullable

我的代码是:

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

1 个答案:

答案 0 :(得分:2)

我找到了Roboguice here的TypeListener的源代码,并查看了第178行的代码,我发现了异常:

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()));

关于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;
    }
}

使用

注释您的字段
@javax.annotation.Nullable

注释和异常消失。

无论如何,动画变量将保持为空,因为 Roboguice 上的 injectMembers 方法未加载 Animator 资源:

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()));
        }
    }

在Roboguice上的 ResourceListener 类。

我不知道为什么,因为只是增加了另一个条件:

else if (Animator.class.isAssignableFrom(t)) {
    value = //read animator from resources
}

无法使用Roboguice不是世界末日。您可以加载动画制作者以指示documentation

修改您的代码:

    public void secondButton(View v) {
        animatorSet = (AnimatorSet) AnimatorInflater.loadAnimator(this, R.anim.button_anim);
        animatorSet .setTarget(button2);
        animatorSet .start();
}