2015-04-19 90 views
0

我有一個回收查看適配器這樣的事情 -RecyclerVIew適配器 - 值傳遞適配器 - onClickListener

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,並堅持在這一點上。先謝謝你。

回答

0

請仔細檢查您的資源文件ID是否與佈局文件中的相匹配。錯過這個小東西是非常普遍的,因此得到一個nullpointerexception。

如果您可以發佈從logcat中給出nullpointerexception的確切行,它會很方便。

+0

我已經添加了空指針異常的問題。 –

0

嘗試改變的佈局管理器的上下文中

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

 LinearLayoutManager layoutManager = new LinearLayoutManager(getActivity()); 

好像佈局管理器的上下文中不與回收視圖的上下文匹配。進行更改並讓我知道。