匕首多重绑定抛出NullPointerException

时间:2018-10-03 22:32:43

标签: android dagger-2 dagger

我正在使用Google的AutoFactory库生成ViewHolder工厂类,并使用Dagger的多重绑定来初始化RecyclerAdapter中的工厂对象。

@Singleton
@Component(modules = AppModules.class)
public interface AppComponents {

...
    Map<Integer, Provider<BooksFactory>> bookTitleMap();
}

提供地图对象的方法。

@Module
public class AppModules {
...

    @Provides
    Map<Integer, Provider<BooksFactory>> providesMap(){
        return new HashMap<>();
    }

}

负责将ViewHolder工厂绑定到Map中的类。

@Module
public class ActivityModules {
    @Provides
    @IntoMap
    @IntKey(BookTitleAdapter.VIEW_CONTENT)
    BookViewHolderFactory provideBookViewHolder(){
        return new BookViewHolderFactory();
    }

    @Provides
    @IntoMap
    @IntKey(BookTitleAdapter.VIEW_TITLE)
    TitleViewHolderFactory provideBookTitleViewHolder(){
        return new TitleViewHolderFactory();
    }
}

我的适配器

    public class BookTitleAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {

        Map<Integer, Provider<BooksFactory>> booksFactories;


        public BookTitleAdapter(Map<Integer, Provider<BooksFactory>> providesMap){
            booksFactories = providesMap;
            titleList = new ArrayList<>();
        }
        @NonNull
        @Override
        public RecyclerView.ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
            context = parent.getContext();
            return booksFactories.get(viewType).get().createViewHolder(parent);
        }
}

当我运行该应用程序时,它崩溃并抛出NullPointerException

    java.lang.NullPointerException: Attempt to invoke interface method 'java.lang.Object javax.inject.Provider.get()' on a null object reference
        at ***.***.com.***.customviews.BookTitleAdapter.onCreateViewHolder(BookTitleAdapter.java:53)
        at android.support.v7.widget.RecyclerView$Adapter.createViewHolder(RecyclerView.java:6725)
        at android.support.v7.widget.RecyclerView$Recycler.tryGetViewHolderForPositionByDeadline(RecyclerView.java:5906)
        at android.support.v7.widget.RecyclerView$Recycler.getViewForPosition(RecyclerView.java:5789)
        at android.support.v7.widget.RecyclerView$Recycler.getViewForPosition(RecyclerView.java:5785)
        at android.support.v7.widget.LinearLayoutManager$LayoutState.next(LinearLayoutManager.java:2233)
        at android.support.v7.widget.LinearLayoutManager.layoutChunk(LinearLayoutManager.java:1560)
        at android.support.v7.widget.LinearLayoutManager.fill(LinearLayoutManager.java:1520)
        at android.support.v7.widget.LinearLayoutManager.onLayoutChildren(LinearLayoutManager.java:615)
        at android.support.v7.widget.RecyclerView.dispatchLayoutStep2(RecyclerView.java:3859)
        at android.support.v7.widget.RecyclerView.dispatchLayout(RecyclerView.java:3576)
        at android.support.v7.widget.RecyclerView.onLayout(RecyclerView.java:4129)
        at android.view.View.layout(View.java:16916)
        at android.view.ViewGroup.layout(ViewGroup.java:5405)
        at android.widget.LinearLayout.setChildFrame(LinearLayout.java:1702)
        at android.widget.LinearLayout.layoutHorizontal(LinearLayout.java:1691)
        at android.widget.LinearLayout.onLayout(LinearLayout.java:1467)
        at android.view.View.layout(View.java:16916)
        at android.view.ViewGroup.layout(ViewGroup.java:5405)
        at android.widget.FrameLayout.layoutChildren(FrameLayout.java:579)
        at android.widget.FrameLayout.onLayout(FrameLayout.java:514)
        at android.view.View.layout(View.java:16916)
        at android.view.ViewGroup.layout(ViewGroup.java:5405)

请,我在做什么错了?

0 个答案:

没有答案