首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >RoboGuice注射AnimatorSet

RoboGuice注射AnimatorSet
EN

Stack Overflow用户
提问于 2014-08-25 20:37:51
回答 1查看 457关注 0票数 1

第一次尝试RoboGuice。到目前为止,注入视图的一切工作都很顺利。

由于我在教程中看到了我可以注入参考资料,所以我尝试添加一个AnimatorSet和一个get错误:

代码语言:javascript
复制
 Reason: java.lang.NullPointerException: Can't inject null value into class myroboguice.teo.ram.css.myroboguicetest.MainActivity.animatorSet when field is not @Nullable

我的代码是:

代码语言:javascript
复制
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

代码语言:javascript
复制
 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
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2014-08-26 00:31:47

我找到了Roboguice TypeListener 这里的源代码,并查看了第178行的代码,发现了例外情况:

代码语言:javascript
复制
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()));

关于可空类的定义:

代码语言:javascript
复制
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;
    }
}

只是注释您的字段

代码语言:javascript
复制
@javax.annotation.Nullable

注释和异常取消。

无论如何,动画器变量将保持为空,因为动画器资源没有加载在Roboguice上的injectMembers方法上。

代码语言:javascript
复制
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课上。

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

代码语言:javascript
复制
else if (Animator.class.isAssignableFrom(t)) {
    value = //read animator from resources
}

不能使用Roboguice并不是世界末日。您可以按指示文档的方式加载动画器。

修改您的代码:

代码语言:javascript
复制
    public void secondButton(View v) {
        animatorSet = (AnimatorSet) AnimatorInflater.loadAnimator(this, R.anim.button_anim);
        animatorSet .setTarget(button2);
        animatorSet .start();
}
票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/25493996

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档