android - NullPointerException when trying to access TextView in CustomSpinner -


i trying use custom spinner calender view. have written own custom adapter. view loads fine try click on 1 of items in spinner dropdown app crashes.

following activity code :

public class calendareventsactivity  extends activity {  protected activityheader mheader; private spinner mmonthdropdown;  @override protected void beforemaincontentcreated(bundle savedinstancestate) {  }  public void oncreatemaincontent(bundle savedinstancestate) {     super.oncreatemaincontent(savedinstancestate);     setrequestedorientationbyresourceid(r.integer.allow_landscape_calendar_events_activity);     getwindow().setbackgrounddrawable(null);      setcontentview(r.layout.activity_calendar_events);      mheader = (activityheader) findviewbyid(r.id.header);      getslidingmenu().setslidingenabled(false);      setupmonthspinner(); }  private void setupmonthspinner() {     mmonthdropdown = (spinner) findviewbyid(r.id.month_spinner);     string[] items = new string[] { "january", "february", "march",             "april", "may", "june", "july", "august", "september",             "october", "november", "december" };     mmonthdropdown.setadapter(new myadapter(this, r.layout.calendar_month_item, items));      mmonthdropdown.setonitemselectedlistener(new onitemselectedlistener() {          @override         public void onitemselected(adapterview<?> parent, view view,                 int position, long id) {             system.out.println("you have clicked......."+((textview)parent.getchildat(position)).gettext().tostring());          }          @override         public void onnothingselected(adapterview<?> parent) {             // todo auto-generated method stub          }     }); }  public class myadapter extends arrayadapter<string> {      string[] calendar_months;      public myadapter(context context, int textviewresourceid,             string[] objects) {         super(context, textviewresourceid, objects);         this.calendar_months = objects;     }      @override     public view getdropdownview(int position, view convertview,             viewgroup parent) {         return getcustomview(position, convertview, parent);     }      @override     public view getview(int position, view convertview, viewgroup parent) {         return getcustomview(position, convertview, parent);     }      public view getcustomview(int position, view convertview,             viewgroup parent) {          layoutinflater inflater = getlayoutinflater();         view row = inflater.inflate(r.layout.calendar_month_item, parent, false);         textview label = (textview) row.findviewbyid(r.id.calendar_month);         label.settext(calendar_months[position]);          return row;     } } } 

this xml defining layout activity (activity_calendar_events.xml) :

<?xml version="1.0" encoding="utf-8"?> 

<com.android.ui.activityheader     android:id="@+id/header"     android:layout_width="match_parent"     android:layout_height="wrap_content"     android:layout_alignparenttop="true"     android:gravity="center"     app:actionbuttondrawable="@drawable/icon_add_member_inset"     app:actionbuttondrawableheight="35dp"     app:actionbuttondrawablewidth="35dp"     app:headerlabel="@string/calendar_events_header"     app:menubuttondrawable="@drawable/icon_navbar_back_inset"     app:menubuttondrawableheight="35dp"     app:menubuttondrawablewidth="35dp"     app:showmenubutton="true" />  <relativelayout     android:layout_width="match_parent"     android:layout_height="wrap_content"     android:background="#f4f4f4"     android:orientation="horizontal"     android:layout_below="@+id/header" >      <spinner         android:id="@+id/month_spinner"         android:layout_width="100dp"         android:layout_height="50dp"         android:layout_alignparentleft="true"         android:layout_marginleft="20dp"         android:background="#f4f4f4"         android:spinnermode="dropdown"         android:popupbackground="#f4f4f4"/>      <textview         android:id="@+id/calendar_year"         android:layout_width="wrap_content"         android:layout_height="wrap_content"         android:layout_alignparentright="true"         android:layout_marginright="20dp"         android:text="2016" /> </relativelayout> 

here xml defining layout individual rows of dropdown (calendar_month_item.xml) :

<?xml version="1.0" encoding="utf-8"?> <textview xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res/com.android.calendar" android:id="@+id/calendar_month" android:layout_width="wrap_content" android:layout_height="wrap_content" android:paddingbottom="8dp" android:paddingleft="15dp" android:paddingtop="8dp" android:textcolor="#181512" android:textsize="15sp" android:textstyle="normal" /> 

and logcat error :

04-19 14:27:06.073: e/thislife(13008): java.lang.nullpointerexception: attempt invoke virtual method 'java.lang.charsequence android.widget.textview.gettext()' on null object reference 04-19 14:27:06.073: e/thislife(13008):  @ com.android.calendar.calendareventsactivity$1.onitemselected(calendareventsactivity.java:78) 04-19 14:27:06.073: e/thislife(13008):  @ android.widget.adapterview.fireonselected(adapterview.java:914) 04-19 14:27:06.073: e/thislife(13008):  @ android.widget.adapterview.dispatchonitemselected(adapterview.java:903) 04-19 14:27:06.073: e/thislife(13008):  @ android.widget.adapterview.access$300(adapterview.java:48) 04-19 14:27:06.073: e/thislife(13008):  @ android.widget.adapterview$selectionnotifier.run(adapterview.java:873) 04-19 14:27:06.073: e/thislife(13008):  @ android.os.handler.handlecallback(handler.java:739) 04-19 14:27:06.073: e/thislife(13008):  @ android.os.handler.dispatchmessage(handler.java:95) 04-19 14:27:06.073: e/thislife(13008):  @ android.os.looper.loop(looper.java:135) 04-19 14:27:06.073: e/thislife(13008):  @ android.app.activitythread.main(activitythread.java:5343) 04-19 14:27:06.073: e/thislife(13008):  @ java.lang.reflect.method.invoke(native method) 04-19 14:27:06.073: e/thislife(13008):  @ java.lang.reflect.method.invoke(method.java:372) 04-19 14:27:06.073: e/thislife(13008):  @ com.android.internal.os.zygoteinit$methodandargscaller.run(zygoteinit.java:905) 04-19 14:27:06.073: e/thislife(13008):  @ com.android.internal.os.zygoteinit.main(zygoteinit.java:700) 

i believe pretty standard way writing custom spinners. tried debugging breakpoints , data points @ specific line causing npe seem not null. not sure whats going on here. suggestions helpful.

thanks.

change line

system.out.println("you have clicked......."+((textview)parent.getchildat(position)).gettext().tostring()); 

to

string selectedmonth    = ((textview) view.findviewbyid(r.id.calendar_month)).gettext().tostring(); system.out.println("you have clicked......."+selectedmonth); 

remember according adapter, getcustomview function returns "inflated" r.layout.calendar_month_item whenever view requested. look-up r.id.calendar_month on view - should contain value of selected month. please check , confirm if works. hope does.


Comments

Popular posts from this blog

java - nested exception is org.hibernate.exception.SQLGrammarException: could not extract ResultSet Hibernate+SpringMVC -

sql - Postgresql tables exists, but getting "relation does not exist" when querying -

asp.net mvc - breakpoint on javascript in CSHTML? -