RecyclerVIew适配器 - 在适配器中传递值 - onClickListener

时间:2015-04-19 23:59:30

标签: android android-recyclerview recycler-adapter

我有一个像这样的回收器View适配器 -

private FetchDonorListData[] itemsData;
    private OnItemClickListener mListener;


    public RecyclerViewAdapter(FetchDonorListData[] itemsData, OnItemClickListener onItemClickListener)
    {
        this.itemsData = itemsData;
        mListener = onItemClickListener;
    }

我正在从主要活动设置此适配器,像这样 -

    donorsListView = (RecyclerView) findViewById(R.id.recyclerDonorsList);
            donorsListView.setHasFixedSize(true);
            LinearLayoutManager dListlayoutManager = new LinearLayoutManager(getApplication());
            donorsListView.setLayoutManager(dListlayoutManager);
   // dListAdapter = new RecyclerViewAdapter(itemsData, );
            donorsListView.setAdapter(dListAdapter);
            donorsListView.setItemAnimator(new DefaultItemAnimator());

我真的想知道如何调用这个适配器?

我已经阅读了所有可能的方法来制作回收者视图但最终得到错误和空指针异常。

空指针方法的异常 - OnMeasure()。

以下是错误 -

04-19 16:40:12.926    5318-5318/? E/AndroidRuntime﹕ FATAL EXCEPTION: main
    Process: com.nyu.blife_app, PID: 5318
    java.lang.NullPointerException: Attempt to invoke virtual method 'void android.support.v7.widget.RecyclerView$LayoutManager.onMeasure(android.support.v7.widget.RecyclerView$Recycler, android.support.v7.widget.RecyclerView$State, int, int)' on a null object reference
            at android.support.v7.widget.RecyclerView.onMeasure(RecyclerView.java:1764)
            at android.view.View.measure(View.java:17430)
            at android.widget.RelativeLayout.measureChildHorizontal(RelativeLayout.java:727)
            at android.widget.RelativeLayout.onMeasure(RelativeLayout.java:463)
            at android.view.View.measure(View.java:17430)
            at android.widget.ScrollView.measureChildWithMargins(ScrollView.java:1260)
            at android.widget.FrameLayout.onMeasure(FrameLayout.java:430)
            at android.widget.ScrollView.onMeasure(ScrollView.java:337)
            at android.view.View.measure(View.java:17430)
            at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5463)
            at android.widget.FrameLayout.onMeasure(FrameLayout.java:430)
            at android.view.View.measure(View.java:17430)
            at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5463)
            at android.support.v7.internal.widget.ActionBarOverlayLayout.onMeasure(ActionBarOverlayLayout.java:453)
            at android.view.View.measure(View.java:17430)
            at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5463)
            at android.widget.FrameLayout.onMeasure(FrameLayout.java:430)
            at android.view.View.measure(View.java:17430)
            at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5463)
            at android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:1436)
            at android.widget.LinearLayout.measureVertical(LinearLayout.java:722)
            at android.widget.LinearLayout.onMeasure(LinearLayout.java:613)
            at android.view.View.measure(View.java:17430)
            at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5463)
            at android.widget.FrameLayout.onMeasure(FrameLayout.java:430)
            at com.android.internal.policy.impl.PhoneWindow$DecorView.onMeasure(PhoneWindow.java:2560)
            at android.view.View.measure(View.java:17430)
            at android.view.ViewRootImpl.performMeasure(ViewRootImpl.java:2001)
            at android.view.ViewRootImpl.measureHierarchy(ViewRootImpl.java:1166)
            at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:1372)
            at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:1054)
            at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:5779)
            at android.view.Choreographer$CallbackRecord.run(Choreographer.java:767)
            at android.view.Choreographer.doCallbacks(Choreographer.java:580)
            at android.view.Choreographer.doFrame(Choreographer.java:550)
            at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:753)
            at android.os.Handler.handleCallback(Handler.java:739)
            at android.os.Handler.dispatchMessage(Handler.java:95)
            at android.os.Looper.loop(Looper.java:135)
            at android.app.ActivityThread.main(ActivityThread.java:5221)
            at java.lang.reflect.Method.invoke(Native Method)
            at java.lang.reflect.Method.invoke(Method.java:372)
            at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:899)
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:694)
    --------- beginning of system

我为这个错误读了一个页面,没有做过建议 - 为回收站视图设置布局管理器 - 仍然收到错误。

如果这个问题看起来很傻,我很抱歉,但我是android的新手,并坚持到这一步。提前谢谢。

2 个答案:

答案 0 :(得分:0)

请仔细检查您的资源文件ID是否与布局文件中的资源文件ID匹配。很容易错过这个小东西,从而得到一个nullpointerexception。

如果您可以发布从logcat发出nullpointerexception的确切行,那将会很方便。

答案 1 :(得分:0)

尝试将布局管理器的上下文更改为

    donorListView.setLayoutManager(new LinearLayoutManager((Activity)this));

     LinearLayoutManager layoutManager = new LinearLayoutManager(getActivity());

似乎布局管理器的上下文与回收器视图的上下文不匹配。进行更改并告诉我。