java - Getting null pointer exception while loading -


i getting nullpointer exception when load class offline_day. have tried solve problem unable @ root cause why nullpointer exception occurring. i have edited code after useful advice. still getting npeeception. class this: offline_day.java:-

    public class offline_day extends dialogfragment implements ondatechangedlistener, ontimechangedlistener {     public final static int date_picker = 1;     public final static int time_picker = 2;     public final static int date_time_picker = 3;     private datepicker datepicker;     private timepicker timepicker1;     private timepicker timepicker2;     private calendar mcalendar;     private activity activity;     private int dialogtype;     private view mview;      @override public view oncreateview(layoutinflater inflater, viewgroup container,bundle savedinstancestate)  {     //activity activity;     view mview = inflater.inflate(r.layout.offline_day, container, false);     mcalendar = calendar.getinstance();     datepicker = (datepicker) mview.findviewbyid(r.id.datepicker1);    // datepicker.init(mcalendar.get(calendar.year), mcalendar.get(calendar.month), mcalendar.get(calendar.day_of_month), this);     timepicker1 = (timepicker) mview.findviewbyid(r.id.timepicker1);     timepicker2 = (timepicker) mview.findviewbyid(r.id.timepicker2);     button=(button)mview.findviewbyid(r.id.button1);     //setis24hourview(true);     //setcalendarviewshown(false);     //ondatechanged(datepicker,2015,6,5);     //ontimechanged(timepicker1,9,00);     //ontimechanged(timepicker2,19,30);             //datepicker.setvisibility(view.gone);     //timepicker1.setvisibility(view.gone);     //timepicker2.setvisibility(view.gone);          return mview; }  @override public boolean onoptionsitemselected(menuitem item)  {     // handle action bar item clicks here. action bar     // automatically handle clicks on home/up button, long     // specify parent activity in androidmanifest.xml.     int id = item.getitemid();     if (id == r.id.action_settings)          return true;     return super.onoptionsitemselected(item); } @override public void onactivitycreated(bundle savedinstancestate) {     super.onactivitycreated(savedinstancestate);     //timepicker1.setontimechangedlistener(this);     //timepicker2.setontimechangedlistener(this); } /*public int get(final int field)  {     return mcalendar.get(field); } public long getdatetimemillis()  {     return mcalendar.gettimeinmillis(); } public void setis24hourview(boolean is24hourview) {     timepicker1.setis24hourview(is24hourview1());     timepicker2.setis24hourview(is24hourview2()); } public boolean is24hourview1()  {     return timepicker1.is24hourview(); } public boolean is24hourview2()  {     return timepicker2.is24hourview(); } public void setcalendarviewshown(boolean calendarview)  {     datepicker.setcalendarviewshown(calendarview); } public boolean calendarviewshown()  {     return datepicker.getcalendarviewshown(); } public void updatedate(int year, int monthofyear, int dayofmonth)  {     datepicker.updatedate(year, monthofyear, dayofmonth); } public void updatetime(int currenthour, int currentminute)  {     timepicker1.setcurrenthour(currenthour);     timepicker1.setcurrentminute(currentminute);     timepicker2.setcurrenthour(currenthour);     timepicker2.setcurrentminute(currentminute); } public string getdatetime()  {     simpledateformat sdf = new simpledateformat("dd-mm-yyyy hh:mm:ss", locale.getdefault());     return sdf.format(mcalendar.gettime()); }*/ @override public void ondatechanged(datepicker view, int year, int monthofyear, int dayofmonth) {     mcalendar.set(mcalendar.get(calendar.year), mcalendar.get(calendar.month), mcalendar.get(calendar.day_of_month)); } @override public void ontimechanged(timepicker view, int hourofday, int minute) {     //mcalendar.set(mcalendar.get(calendar.year), mcalendar.get(calendar.month), mcalendar.get(calendar.day_of_month), hourofday, minute);     mcalendar.set(mcalendar.get(calendar.hour_of_day), mcalendar.get(calendar.minute)); } /*private datepickerdialog.ondatesetlistener datepickerlistener=new datepickerdialog.ondatesetlistener() {     public void ondateset(datepicker view, int selectedyear,int selectedmonth, int selectedday)      {} }; datepickerdialog dialog = new datepickerdialog(getactivity(), datepickerlistener, 2000, 1,1);   */ } 

this class.

now when load application following errors:

    06-11 16:30:54.229: e/androidruntime(1592): fatal exception: main 06-11 16:30:54.229: e/androidruntime(1592): process: com.example.mobidoot pid: 1592 06-11 16:30:54.229: e/androidruntime(1592): java.lang.nullpointerexception: attempt invoke virtual method 'android.content.res.resources$theme android.content.context.gettheme()' on null object reference 06-11 16:30:54.229: e/androidruntime(1592):     @ android.app.datepickerdialog.resolvedialogtheme(datepickerdialog.java:88) 06-11 16:30:54.229: e/androidruntime(1592):     @ android.app.datepickerdialog.<init>(datepickerdialog.java:105) 06-11 16:30:54.229: e/androidruntime(1592):     @ android.app.datepickerdialog.<init>(datepickerdialog.java:82) 06-11 16:30:54.229: e/androidruntime(1592):     @ android.view.view.performclick(view.java:4780) 06-11 16:30:54.229: e/androidruntime(1592):     @ android.view.view$performclick.run(view.java:19866) 06-11 16:30:54.229: e/androidruntime(1592):     @ android.os.handler.handlecallback(handler.java:739) 06-11 16:30:54.229: e/androidruntime(1592):     @ android.os.handler.dispatchmessage(handler.java:95) 06-11 16:30:54.229: e/androidruntime(1592):     @ android.os.looper.loop(looper.java:135) 06-11 16:30:54.229: e/androidruntime(1592):     @ android.app.activitythread.main(activitythread.java:5257) 06-11 16:30:54.229: e/androidruntime(1592):     @ java.lang.reflect.method.invoke(native method) 06-11 16:30:54.229: e/androidruntime(1592):     @ java.lang.reflect.method.invoke(method.java:372) 06-11 16:30:54.229: e/androidruntime(1592):     @ com.android.internal.os.zygoteinit$methodandargscaller.run(zygoteinit.java:903) 06-11 16:30:54.229: e/androidruntime(1592):     @ com.android.internal.os.zygoteinit.main(zygoteinit.java:698) 

i still getting npexception. highly appreciated.

datepickerdialog dialog = new datepickerdialog(getactivity(), datepickerlistener, 2000, 1,1);    

getactivity() return null when you're initializing fragment. you'll have wait until onattach() in fragment lifecycle return non-null.

that's reason npe.

overall, don't have non-default constructors in fragments. set fragment view in oncreateview() generic fragments, or oncreatedialog() dialogfragments.


Comments

Popular posts from this blog

javascript - gulp-nodemon - nodejs restart after file change - Error: listen EADDRINUSE events.js:85 -

Fatal Python error: Py_Initialize: unable to load the file system codec. ImportError: No module named 'encodings' -

javascript - oscilloscope of speaker input stops rendering after a few seconds -