android - NullPointerException on ViewPager with Recyclerview -


we have on our app viewpager fragmentpageradapter contains 3 fragments. 2 of these fragments composed recyclerview each one.

the first page (the fragment without viewpager) displayed correctly. however, when viewpager tries pre-load next page (a recyclerview), app crashes because of nullpointerexception following log :

 java.lang.nullpointerexception: attempt invoke virtual method 'boolean android.support.v7.widget.recyclerview$viewholder.shouldignore()' on null object reference          @ android.support.v7.widget.recyclerview.findminmaxchildlayoutpositions(recyclerview.java:2839)          @ android.support.v7.widget.recyclerview.dispatchlayout(recyclerview.java:2626)          @ android.support.v7.widget.recyclerview.onlayout(recyclerview.java:3011)          @ android.view.view.layout(view.java:15684)          @ android.view.viewgroup.layout(viewgroup.java:4981)          @ android.support.v4.view.viewpager.onlayout(viewpager.java:1626)          @ android.view.view.layout(view.java:15684)          @ android.view.viewgroup.layout(viewgroup.java:4981)          @ android.widget.framelayout.layoutchildren(framelayout.java:573)          @ android.widget.framelayout.onlayout(framelayout.java:508)          @ android.view.view.layout(view.java:15684)          @ android.view.viewgroup.layout(viewgroup.java:4981)          @ android.widget.linearlayout.setchildframe(linearlayout.java:1703)          @ android.widget.linearlayout.layoutvertical(linearlayout.java:1557)          @ android.widget.linearlayout.onlayout(linearlayout.java:1466)          @ android.view.view.layout(view.java:15684)          @ android.view.viewgroup.layout(viewgroup.java:4981)          @ android.support.design.widget.coordinatorlayout.layoutchild(coordinatorlayout.java:1000)          @ android.support.design.widget.coordinatorlayout.onlayoutchild(coordinatorlayout.java:710)          @ android.support.design.widget.coordinatorlayout.onlayout(coordinatorlayout.java:724)          @ android.view.view.layout(view.java:15684)          @ android.view.viewgroup.layout(viewgroup.java:4981)          @ android.support.v4.widget.drawerlayout.onlayout(drawerlayout.java:907)          @ android.view.view.layout(view.java:15684)          @ android.view.viewgroup.layout(viewgroup.java:4981)          @ android.widget.framelayout.layoutchildren(framelayout.java:573)          @ android.widget.framelayout.onlayout(framelayout.java:508)          @ android.view.view.layout(view.java:15684)          @ android.view.viewgroup.layout(viewgroup.java:4981)          @ android.widget.linearlayout.setchildframe(linearlayout.java:1703)          @ android.widget.linearlayout.layoutvertical(linearlayout.java:1557)          @ android.widget.linearlayout.onlayout(linearlayout.java:1466)          @ android.view.view.layout(view.java:15684)          @ android.view.viewgroup.layout(viewgroup.java:4981)          @ android.widget.framelayout.layoutchildren(framelayout.java:573)          @ android.widget.framelayout.onlayout(framelayout.java:508)          @ android.view.view.layout(view.java:15684)          @ android.view.viewgroup.layout(viewgroup.java:4981)          @ android.widget.linearlayout.setchildframe(linearlayout.java:1703)          @ android.widget.linearlayout.layoutvertical(linearlayout.java:1557)          @ android.widget.linearlayout.onlayout(linearlayout.java:1466)          @ android.view.view.layout(view.java:15684)          @ android.view.viewgroup.layout(viewgroup.java:4981)          @ android.widget.framelayout.layoutchildren(framelayout.java:573)          @ android.widget.framelayout.onlayout(framelayout.java:508)          @ android.view.view.layout(view.java:15684)          @ android.view.viewgroup.layout(viewgroup.java:4981)          @ android.view.viewrootimpl.performlayout(viewrootimpl.java:2186)          @ android.view.viewrootimpl.performtraversals(viewrootimpl.java:1920)          @ android.view.viewrootimpl.dotraversal(viewrootimpl.java:1106)          @ android.view.viewrootimpl$traversalrunnable.run(viewrootimpl.java:6018)          @ android.view.choreographer$callbackrecord.run(choreographer.java:792)          @ android.view.choreographer.docallbacks(choreographer.java:596)          @ android.view.choreographer.doframe(choreographer.java:557)          @ android.view.choreographer$framedisplayeventreceiver.run(choreographer.java:778)          @ android.os.handler.handlecallback(handler.java:739)          @ android.os.handler.dispatchmessage(handler.java:95)          @ android.os.looper.loop(looper.java:155)          @ android.app.activitythread.main(activitythread.java:5696)          @ java.lang.reflect.method.invoke(native method)          @ java.lang.reflect.method.invoke(method.java:372) 

here's how viewpager declared :

viewpager viewpager = (viewpager) findviewbyid(r.id.viewpager); viewpageradapter adapter = new viewpageradapter(getsupportfragmentmanager()); adapter.addfrag(fragment1, "fragment1"); adapter.addfrag(fragment2, "fragment2"); adapter.addfrag(fragment3, "fragment3"); viewpager.setadapter(adapter); 

and adapter :

    private class viewpageradapter extends fragmentpageradapter {     private final list<fragment> mfragmentlist = new arraylist<>();     private final list<string> mfragmenttitlelist = new arraylist<>();     public viewpageradapter(fragmentmanager manager) {         super(manager);     }     @override     public fragment getitem(int position) {         return mfragmentlist.get(position);     }     @override     public int getcount() {         return mfragmentlist.size();     }     public void addfrag(fragment fragment, string title) {         mfragmentlist.add(fragment);         mfragmenttitlelist.add(title);     }     @override     public charsequence getpagetitle(int position) {         return mfragmenttitlelist.get(position);     } } 

as code both of recyclerview long , different each page don't know part relevant won't give sample. not hesitate ask specific part if think can helpful fix issue.

one thing can tell if want work, have comment call each of setadapter both of recylerview.

edit : here's code second page.

public class myfragment extends fragment {      recyclerview recyclerview;     gridadapter gridadapter;      public gridadapter getgridadapter() {         return gridadapter;     }      @override     public view oncreateview(layoutinflater inflater, viewgroup container, bundle savedinstancestate) {         final view v = inflater.inflate(r.layout.our_layout, container, false);         recyclerview = (recyclerview) v.findviewbyid(r.id.recycler_view);         gridlayoutmanager.setsmoothscrollbarenabled(true);         recyclerview.setlayoutmanager(gridlayoutmanager);          recyclerview.sethasfixedsize(true);          return v;     }      @override     public void onactivitycreated(bundle savedinstancestate) {         super.onactivitycreated(savedinstancestate);         arraylist<model> model = getarguments().getparcelablearraylist("extra");         if (model != null && model.size() != 0) {             gridadapter = new gridadapter(model);             recyclerview.setadapter(gridadapter);         }     }      @override     public void setuservisiblehint(boolean isvisibletouser) {         super.setuservisiblehint(isvisibletouser);         if (isvisibletouser && isresumed()){             onresume();         }     }      @override     public void onresume() {         super.onresume();         if (!getuservisiblehint())             return;     }      public class gridspacingitemdecoration extends recyclerview.itemdecoration {          private int spancount;         private int spacingleft;         private int spacingright;         private int spacingtop;         private int spacingbottom;         private boolean includeedge;          public gridspacingitemdecoration(int spancount, int spacingleft, int spacingtop, int spacingright, int spacingbottom, boolean includeedge) {             this.spancount = spancount;             this.spacingleft = spacingleft;             this.spacingright = spacingright;             this.spacingtop = spacingtop;             this.spacingbottom = spacingbottom;             this.includeedge = includeedge;         }          @override         public void getitemoffsets(rect outrect, view view, recyclerview parent, recyclerview.state state) {             int position = parent.getchildadapterposition(view); // item position             int column = position % spancount; // item column              if (includeedge) {                 outrect.left = spacingleft - column * spacingleft / spancount; // spacing - column * ((1f / spancount) * spacing)                 outrect.right = (column + 1) * spacingright / spancount; // (column + 1) * ((1f / spancount) * spacing)                  if (position < spancount) { // top edge                     outrect.top = spacingtop;                 }                 outrect.bottom = spacingbottom; // item bottom             } else {                 outrect.left = column * spacingleft / spancount; // column * ((1f / spancount) * spacing)                 outrect.right = spacingright - (column + 1) * spacingright / spancount; // spacing - (column + 1) * ((1f /    spancount) * spacing)                 if (position >= spancount) {                     outrect.top = spacingtop; // item top                 }             }         }     }      public class gridadapter extends recyclerview.adapter<gridadapter.viewholder> {          private arraylist<model> model;          public gridadapter(arraylist<model> offer) {             super();             model = offer;         }          @override         public viewholder oncreateviewholder(final viewgroup parent, int viewtype) {             final view view = layoutinflater.from(parent.getcontext()).inflate(r.layout.layout_item, parent, false);             final viewholder holder = new viewholder(view);             return holder;         }          @override         public void onbindviewholder(final viewholder holder, final int position) {             final model currentoffer = model.get(position);              holder.category.getviewtreeobserver().addongloballayoutlistener(new viewtreeobserver.ongloballayoutlistener() {                 @suppresslint("newapi")                 @suppresswarnings("deprecation")                 @override                 public void ongloballayout() {                     int width = holder.category.getwidth();                     viewgroup.layoutparams params = holder.appicon.getlayoutparams();                     params.width = width;                     params.height = width;                      holder.appicon.setlayoutparams(params);                      if (android.os.build.version.sdk_int >= android.os.build.version_codes.jelly_bean)                         holder.itemview.getviewtreeobserver().removeongloballayoutlistener(this);                     else                         holder.itemview.getviewtreeobserver().removeglobalonlayoutlistener(this);                 }             });              picasso.with(getactivity().getapplicationcontext()).                     load(currentoffer.getapp_logo()).fit().centercrop().into(holder.appicon);             holder.appname.settext(currentoffer.getapp_name());             holder.category.settext(currentoffer.getapp_category());              holder.itemview.setonclicklistener(new view.onclicklistener() {                 @override                 public void onclick(view v) {                     string marketurl = androidtools.getplaystoreurl(currentoffer.getapp_store_id(), true);                      uitools.launchurl(getactivity(), marketurl);                  }             });          }          @override         public int getitemcount() {             return model.size();         }          class viewholder extends recyclerview.viewholder {             private imageview appicon;             private textview appname;             private textview category;              public viewholder(view itemview) {                 super(itemview);                 appicon = (imageview)itemview.findviewbyid(r.id.item_icon);                 appname = (textview)itemview.findviewbyid(r.id.item_app_name);                 category = (textview)itemview.findviewbyid(r.id.item_category);             }         }     } } 

any appreciated.

i've got error during 1 of development. have checked recyclerview in xml files correctly wrapped layout framelayout?

if not, crash on viewpager , not on single fragment view.


Comments

Popular posts from this blog

java - Date formats difference between yyyy-MM-dd'T'HH:mm:ss and yyyy-MM-dd'T'HH:mm:ssXXX -

c# - Get rid of xmlns attribute when adding node to existing xml -