gpt4 book ai didi

android - RoboGuice 注入(inject) AnimatorSet

转载 作者:行者123 更新时间:2023-11-29 21:02:48 29 4
gpt4 key购买 nike

第一次尝试 RoboGuice。到目前为止,注入(inject) View 的一切都运行顺利。

自从我在 tutorial 中看到我可以注入(inject)资源我试图添加一个 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

最佳答案

我找到了Roboguice的TypeListener的源码here ,查看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

注释和异常消失。

无论如何,animator 变量将保持为空,因为 Animator 资源未加载到 RoboguiceinjectMembers 方法中:

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

关于android - RoboGuice 注入(inject) AnimatorSet,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25493996/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com