java - Issues in Recycler view when retrive the data from using json -


this question has answer here:

my issue when creating recycler view using json shows exception 1 can solve issue u r brilliance please me solve issue , appreciate solve error?

public class mainactivity extends activity { button b; textview tv; listview lists; recyclerview recyclerview; list<your> yourss;  @override protected void oncreate(bundle savedinstancestate) {     super.oncreate(savedinstancestate);     setcontentview(r.layout.activity_main);     recyclerview = (recyclerview) findviewbyid(r.id.recycle);     linearlayoutmanager ll = new linearlayoutmanager(mainactivity.this);     recyclerview.sethasfixedsize(true);     recyclerview.setlayoutmanager(ll);     new jsontask().execute("http://yoursubshop.com/webservices/categories.php"); }  public class jsontask extends asynctask<string, string, list<your>> {     @override     protected list<your> doinbackground(string... params) {         httpurlconnection httpurlconnection = null;         bufferedreader bufferedreader = null;         try {             url url = new url(params[0]);             httpurlconnection = (httpurlconnection) url.openconnection();             inputstreamreader inputstreamreader = new inputstreamreader(httpurlconnection.getinputstream());             bufferedreader = new bufferedreader(inputstreamreader);             stringbuffer sb = new stringbuffer();             string line = "";             while ((line = bufferedreader.readline()) != null) {                 sb.append(line);             }             string finaljson = sb.tostring();             jsonobject firstobject = new jsonobject(finaljson);             jsonarray firstarray = firstobject.getjsonarray("data");                           list<your> yo = new arraylist<>();             (int = 0; < firstarray.length(); i++) {                 jsonobject finalobject = firstarray.getjsonobject(i);                 yos = new your();                 yos.category_id = finalobject.getstring("category_id");                 yos.category_name = finalobject.getstring("category_name");                 yo.add(yos);             }             return yo;         } catch (malformedurlexception e) {             e.printstacktrace();         } catch (ioexception e) {             e.printstacktrace();         } catch (jsonexception e) {             e.printstacktrace();         } {             if (httpurlconnection != null) {                 httpurlconnection.disconnect();             }             try {                 if (bufferedreader != null) {                      bufferedreader.close();                 }             } catch (ioexception e) {                 e.printstacktrace();             }         }         return null;     }      @override     protected void onpostexecute(list<your> result) {         super.onpostexecute(result);         rvadapter listadapter = new rvadapter(mainactivity.this, yourss);         recyclerview.setadapter(listadapter);     } }  public class rvadapter extends recyclerview.adapter<rvadapter.myadapter> {     list<your> yours = collections.emptylist();      context c;     public class myadapter extends recyclerview.viewholder {         textview id;         textview name;         cardview cv;         public myadapter(view itemview) {             super(itemview);             id = (textview) itemview.findviewbyid(r.id.texts);             name = (textview) itemview.findviewbyid(r.id.textsa);             cv = (cardview)itemview.findviewbyid(r.id.card);         }     }     public rvadapter(context c, list<your> yours) {         this.yours = yours;         this.c = c;       }     @override     public myadapter oncreateviewholder(viewgroup parent, int viewtype) {         view v = layoutinflater.from(parent.getcontext()).inflate(r.layout.lists, parent, false);         myadapter m = new myadapter(v);         return m;     }      @override     public void onbindviewholder(myadapter holder, int position) {          y = yours.get(position);         holder.id.settext(y.category_id);         holder.name.settext(y.category_name);     }      @override     public int getitemcount() {         return yours.size();     }      @override     public void onattachedtorecyclerview(recyclerview recyclerview) {         super.onattachedtorecyclerview(recyclerview);     } } 

eception in programme

06-28 17:03:34.078 802-802/com.example.anilkumar.json e/androidruntime: fatal exception: main java.lang.nullpointerexception     @ com.example.anilkumar.json.mainactivity$rvadapter.getitemcount(mainactivity.java:154)     @ android.support.v7.widget.recyclerview.dispatchlayoutstep1(recyclerview.java:3170)     @ android.support.v7.widget.recyclerview.onmeasure(recyclerview.java:2782)     @ android.view.view.measure(view.java:12723)     @ android.view.viewgroup.measurechildwithmargins(viewgroup.java:4698)     @ android.widget.linearlayout.measurechildbeforelayout(linearlayout.java:1369)     @ android.widget.linearlayout.measurevertical(linearlayout.java:660)     @ android.widget.linearlayout.onmeasure(linearlayout.java:553)     @ android.view.view.measure(view.java:12723)     @ android.view.viewgroup.measurechildwithmargins(viewgroup.java:4698)     @ android.widget.framelayout.onmeasure(framelayout.java:293)     @ android.view.view.measure(view.java:12723)     @ android.view.viewgroup.measurechildwithmargins(viewgroup.java:4698)     @ android.widget.linearlayout.measurechildbeforelayout(linearlayout.java:1369)     @ android.widget.linearlayout.measurevertical(linearlayout.java:660)     @ android.widget.linearlayout.onmeasure(linearlayout.java:553)     @ android.view.view.measure(view.java:12723)     @ android.view.viewgroup.measurechildwithmargins(viewgroup.java:4698)     @ android.widget.framelayout.onmeasure(framelayout.java:293)     @ com.android.internal.policy.impl.phonewindow$decorview.onmeasure(phonewindow.java:2092)     @ android.view.view.measure(view.java:12723)     @ android.view.viewrootimpl.performtraversals(viewrootimpl.java:1064)     @ android.view.viewrootimpl.handlemessage(viewrootimpl.java:2442)     @ android.os.handler.dispatchmessage(handler.java:99)     @ android.os.looper.loop(looper.java:137)     @ android.app.activitythread.main(activitythread.java:4424)     @ java.lang.reflect.method.invokenative(native method)     @ java.lang.reflect.method.invoke(method.java:511)     @ com.android.internal.os.zygoteinit$methodandargscaller.run(zygoteinit.java:784)     @ com.android.internal.os.zygoteinit.main(zygoteinit.java:551) 

lists.xml

<?xml version="1.0" encoding="utf-8"?> <linearlayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="match_parent" android:layout_height="match_parent"> <android.support.v7.widget.cardview     android:layout_width="300dp"     android:layout_height="80dp"     android:id="@+id/card"> <textview android:layout_width="wrap_content" android:layout_height="wrap_content" android:id="@+id/texts" android:text="id" android:padding="20dp" android:layout_margin="20sp" android:textsize="20sp"/> <textview     android:layout_width="wrap_content"     android:layout_height="wrap_content"     android:id="@+id/textsa"     android:layout_margin="20sp"     android:text="resturants"     android:textsize="20sp"/> </android.support.v7.widget.cardview> 

the problem don't have initialized array when trying count. try changing to.

@override public int getitemcount() {     return yours == null ? 0 : yours.size(); } 

Comments

Popular posts from this blog

sql - invalid in the select list because it is not contained in either an aggregate function -

Angularjs unit testing - ng-disabled not working when adding text to textarea -

How to start daemon on android by adb -