Migrate RecyclerView.Adapter from Java to Kotlin
Java Coeds:
public class ArticlesAdapter extends RecyclerView.Adapter<ArticleItemViewHolder> {
private List<ArticleItem> mArticles;
@Inject
ImageLoader mImageLoader;
@Inject
public ArticlesAdapter() {
}
}
Auto-converted Kotlin Codes:
class ArticlesAdapter @Inject
constructor() : RecyclerView.Adapter<ArticleItemViewHolder>() {
private var mArticles: List<ArticleItem>? = null
@Inject
internal var mImageLoader: ImageLoader? = null
}
Kotlin compiler error:
ArticlesAdapter.java:25: error: Dagger does not support injection into private fields
private me.li2.android.architecture.utils.ImageLoader mImageLoader;
^
AppComponent.java:40: error: dagger.internal.codegen.ComponentProcessor was unable to process this interface because not all of its dependencies could be resolved. Check for compilation errors or a circular dependency with generated code.
public interface AppComponent extends AndroidInjector<DaggerApplication> {
^
解决办法:当使用 @Inject、@BindView 等注解标记变量时,需要把关键字从 internal
修改为 lateinit
:
@Inject
- internal var mImageLoader: ImageLoader? = null
+ lateinit var mImageLoader: ImageLoader