android - 无法将空值注入(inject)类

标签 android android-fragments dependency-injection roboguice

我有一个要重复使用的 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/

相关文章:

android - 将 LinearLayout 内的按钮高度与水平方向相匹配

android - 切换 android map fragment -- 崩溃

android - onAttach() 从未在 DialogFragment 中调用

dependency-injection - 我可以在 quartz 调度程序作业中使用 CDI 注入(inject)吗?

java - 如何在 Spring Cloud Config 中定义共享数据源

android - Exoplayer 为 android 中的大型 mp4 文件抛出解码器初始化异常

java - Android 什么时候可以释放端口?

android - 谷歌地图崩溃 null LatLng

android - IllegalArgumentException : No view found for id , 从另一个 fragment 返回并调用 onResume 时

c# - 如何在 UWP 应用程序中使用依赖注入(inject)?