我有一个要重复使用的 fragment 。它的功能是一样的,只是布局发生了变化
我正在使用 roboguice 通过 id 将 View 注入(inject)变量
例如,我添加了这个 View :
@Nullable
@InjectView(R.id.edtEventLocationAddress)
private EditText edtEventLocationAddress;
现在这个 View 可能会或可能不会出现在我在 onCreateView 方法中提供的给定布局中
这就是为什么我把 @Nullable
放在上面的原因
但是,当我运行应用程序时,布局没有这个 View ,我得到
java.lang.NullPointerException: Can't inject null value into class
com.myapp.CreateEventPageFragment.edtEventLocationAddress when field is not @Nullable
我需要做什么才能让 roboguice 允许我重用 fragment ,并且只改变它们的 View ?
最佳答案
一个迟到的答案,但以防万一其他人偶然发现了这个问题。
您可能正在使用 android.support.annotation.Nullable
或具有 @Retention(RetentionPolicy.CLASS)
(或 RetentionPolicy.SOURCE
)。但是,您需要使用在运行时保留的 @Nullable
注解,以便 RoboGuice 找到它。例如。这个:
import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
@Target({ElementType.FIELD, ElementType.METHOD,
ElementType.PARAMETER,
ElementType.LOCAL_VARIABLE})
@Retention(RetentionPolicy.RUNTIME)
public @interface Nullable {}
关于android - 无法将空值注入(inject)类,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25831211/