string - BaseAdapter class wont setAdapter inside Asynctask - Android -


i have asynctask gathers usernames, comments, , numbers. places them strings , suppose call baseadapter class, create adapter, , set adapter class. code doesn't work, crashes app, here code

   public class dashboardactivity extends listactivity {     string comments[];     string usernames[];     string numbers[];     listview lstcomments;        class createcommentlists extends baseadapter{             context ctx_invitation;             string[] listcomments;             string[] listnumbers;             string[] listusernames;               public createcommentlists(string[] comments, string[] usernames, string[] numbers)             {                 super();                  listcomments = comments;                 listnumbers = usernames;                 listusernames = numbers;             }              @override             public int getcount() {                 if(null == listcomments)                 {                 return 0;                 }                     // todo auto-generated method stub                 return listcomments.length;             }              @override             public object getitem(int position) {                 // todo auto-generated method stub                 return listcomments[position];             }              @override             public long getitemid(int position) {                 // todo auto-generated method stub                 return 0;             }              @override             public view getview(int position, view convertview, viewgroup parent) {                 // todo auto-generated method stub                 view v = null;                 try                 {                     string inflater = context.layout_inflater_service;                     layoutinflater li = (layoutinflater)ctx_invitation.getsystemservice(inflater);                     v = li.inflate(r.layout.list_item, null);                      textview commentview = (textview)v.findviewbyid(r.id.listcomment);                     textview numbersview = (textview)v.findviewbyid(r.id.listnumber);                     textview usernamesview = (textview)v.findviewbyid(r.id.listpostedby);                       commentview.settext(listcomments[position]);                     numbersview.settext(listnumbers[position]);                     usernamesview.settext(listusernames[position]);                 }                 catch(exception e)                 {                     e.printstacktrace();                 }                 return v;             }            }             class loadcomments extends asynctask<jsonobject, string, jsonobject> {              @override             protected void onpreexecute() {                 super.onpreexecute();               }               @override             protected void onprogressupdate(string... values) {                 super.onprogressupdate(values);              }               protected jsonobject doinbackground(jsonobject... params) {                   jsonobject json2 = collectcomments.collectcomments(usernameforcomments, offsetnumber);                       return json2;                }              @override             protected void onpostexecute(jsonobject json2) {                 try {                       if (json2.getstring(key_success) != null) {                          registererrormsg.settext("");                         string res2 = json2.getstring(key_success);                         if(integer.parseint(res2) == 1){                                 jsonarray commentarray = json2.getjsonarray(key_comment);                             string comments[] = new string[commentarray.length()];                             ( int i=0; i<commentarray.length(); i++ ) {                                 comments[i] = commentarray.getstring(i);                             }                             jsonarray numbersarray = json2.getjsonarray(key_number);                             string numbers[] = new string[numbersarray.length()];                             ( int i=0; i<numbersarray.length(); i++ ) {                                 numbers[i] = numbersarray.getstring(i);                             }                             jsonarray usernamearray = json2.getjsonarray(key_username);                             string usernames[] = new string[usernamearray.length()];                             ( int i=0; i<usernamearray.length(); i++ ) {                                 usernames[i] = usernamearray.getstring(i);                             }                              createcommentlists mycmlist = new createcommentlists(comments, usernames, numbers);                             lstcomments = (listview)findviewbyid(android.r.id.list);                              lstcomments.setadapter(mycmlist);                                     }//end if key == 1                         else{                             // error in registration                             registererrormsg.settext(json2.getstring(key_error_msg));                         }//end else                     }//end if                 } //end try                  catch (jsonexception e) {                      e.printstacktrace();                 }//end catch                 }          }           new loadcomments().execute();    } 

here logcat

    07-17 17:07:13.436: e/androidruntime(23880): fatal exception: main 07-17 17:07:13.436: e/androidruntime(23880): java.lang.nullpointerexception 07-17 17:07:13.436: e/androidruntime(23880):    @ android.widget.listview.measurescrapchild(listview.java:1163) 07-17 17:07:13.436: e/androidruntime(23880):    @ android.widget.listview.measureheightofchildren(listview.java:1294) 07-17 17:07:13.436: e/androidruntime(23880):    @ android.widget.listview.onmeasure(listview.java:1155) 07-17 17:07:13.436: e/androidruntime(23880):    @ android.view.view.measure(view.java:13011) 07-17 17:07:13.436: e/androidruntime(23880):    @ android.view.viewgroup.measurechildwithmargins(viewgroup.java:4706) 07-17 17:07:13.436: e/androidruntime(23880):    @ android.widget.linearlayout.measurechildbeforelayout(linearlayout.java:1369) 07-17 17:07:13.436: e/androidruntime(23880):    @ android.widget.linearlayout.measurevertical(linearlayout.java:660) 07-17 17:07:13.436: e/androidruntime(23880):    @ android.widget.linearlayout.onmeasure(linearlayout.java:553) 07-17 17:07:13.436: e/androidruntime(23880):    @ android.view.view.measure(view.java:13011) 07-17 17:07:13.436: e/androidruntime(23880):    @ android.widget.relativelayout.measurechild(relativelayout.java:579) 07-17 17:07:13.436: e/androidruntime(23880):    @ android.widget.relativelayout.onmeasure(relativelayout.java:392) 07-17 17:07:13.436: e/androidruntime(23880):    @ android.view.view.measure(view.java:13011) 07-17 17:07:13.436: e/androidruntime(23880):    @ android.view.viewgroup.measurechildwithmargins(viewgroup.java:4706) 07-17 17:07:13.436: e/androidruntime(23880):    @ android.widget.linearlayout.measurechildbeforelayout(linearlayout.java:1369) 07-17 17:07:13.436: e/androidruntime(23880):    @ android.widget.linearlayout.measurehorizontal(linearlayout.java:1017) 07-17 17:07:13.436: e/androidruntime(23880):    @ android.widget.linearlayout.onmeasure(linearlayout.java:555) 07-17 17:07:13.436: e/androidruntime(23880):    @ android.view.view.measure(view.java:13011) 07-17 17:07:13.436: e/androidruntime(23880):    @ android.view.viewgroup.measurechildwithmargins(viewgroup.java:4706) 07-17 17:07:13.436: e/androidruntime(23880):    @ android.widget.framelayout.onmeasure(framelayout.java:293) 07-17 17:07:13.436: e/androidruntime(23880):    @ android.view.view.measure(view.java:13011) 07-17 17:07:13.436: e/androidruntime(23880):    @ android.widget.linearlayout.measurevertical(linearlayout.java:812) 07-17 17:07:13.436: e/androidruntime(23880):    @ android.widget.linearlayout.onmeasure(linearlayout.java:553) 07-17 17:07:13.436: e/androidruntime(23880):    @ android.view.view.measure(view.java:13011) 07-17 17:07:13.436: e/androidruntime(23880):    @ android.view.viewgroup.measurechildwithmargins(viewgroup.java:4706) 07-17 17:07:13.436: e/androidruntime(23880):    @ android.widget.framelayout.onmeasure(framelayout.java:293) 07-17 17:07:13.436: e/androidruntime(23880):    @ android.view.view.measure(view.java:13011) 07-17 17:07:13.436: e/androidruntime(23880):    @ android.view.viewgroup.measurechildwithmargins(viewgroup.java:4706) 07-17 17:07:13.436: e/androidruntime(23880):    @ android.widget.framelayout.onmeasure(framelayout.java:293) 07-17 17:07:13.436: e/androidruntime(23880):    @ com.navdrawer.simplesidedrawer.onmeasure(simplesidedrawer.java:443) 07-17 17:07:13.436: e/androidruntime(23880):    @ android.view.view.measure(view.java:13011) 07-17 17:07:13.436: e/androidruntime(23880):    @ android.view.viewgroup.measurechildwithmargins(viewgroup.java:4706) 07-17 17:07:13.436: e/androidruntime(23880):    @ android.widget.framelayout.onmeasure(framelayout.java:293) 07-17 17:07:13.436: e/androidruntime(23880):    @ com.android.internal.policy.impl.phonewindow$decorview.onmeasure(phonewindow.java:2162) 07-17 17:07:13.436: e/androidruntime(23880):    @ android.view.view.measure(view.java:13011) 07-17 17:07:13.436: e/androidruntime(23880):    @ android.view.viewrootimpl.performtraversals(viewrootimpl.java:1091) 07-17 17:07:13.436: e/androidruntime(23880):    @ android.view.viewrootimpl.handlemessage(viewrootimpl.java:2505) 07-17 17:07:13.436: e/androidruntime(23880):    @ android.os.handler.dispatchmessage(handler.java:99) 07-17 17:07:13.436: e/androidruntime(23880):    @ android.os.looper.loop(looper.java:154) 07-17 17:07:13.436: e/androidruntime(23880):    @ android.app.activitythread.main(activitythread.java:4945) 07-17 17:07:13.436: e/androidruntime(23880):    @ java.lang.reflect.method.invokenative(native method) 07-17 17:07:13.436: e/androidruntime(23880):    @ java.lang.reflect.method.invoke(method.java:511) 07-17 17:07:13.436: e/androidruntime(23880):    @ com.android.internal.os.zygoteinit$methodandargscaller.run(zygoteinit.java:784) 07-17 17:07:13.436: e/androidruntime(23880):    @ com.android.internal.os.zygoteinit.main(zygoteinit.java:551) 07-17 17:07:13.436: e/androidruntime(23880):    @ dalvik.system.nativestart.main(native method) 

have checked if view v return getview() method null? catching exception catches exceptions. not helpful in opinion. what's happening here have not initiated context ctx_invitation. should in constructor. now, since ctx_invitation null, causes nullpointerexception caught catch block. , view v remains null.

change constructor createcommentlists:

public createcommentlists(string[] comments, string[] usernames, string[] numbers, dashboardactivity context)         {             super();              listcomments = comments;             listnumbers = usernames;             listusernames = numbers;             ctx_invitation = context;         } 

change following well:

createcommentlists mycmlist = new createcommentlists(comments, usernames, numbers, dashboardactivity.this); 

Comments

Popular posts from this blog

php - Calling a template part from a post -

Firefox SVG shape not printing when it has stroke -

How to mention the localhost in android -